4.0 Module IV: Programming the 8051
4.1 Introduction to 8051 I/O Programming
Input/Output (I/O) operations are fundamental to the purpose of an embedded system. I/O ports serve as the microcontroller’s interface to the physical world, allowing it to sense inputs (like a button press) and control outputs (like turning on an LED). Mastering I/O programming is essential for creating any meaningful embedded application.
The standard 8051 is housed in a 40-pin Dual Inline Package (DIP). Of these 40 pins, 32 are dedicated to I/O and are organized into four 8-bit ports: P0, P1, P2, and P3. The remaining 8 pins are used for power and control signals, including Vcc (power supply), GND (ground), XTAL1/XTAL2 (oscillator connection), RST (reset), EA (external access), ALE (address latch enable), and PSEN (program store enable).
4.2 Detailed Analysis of I/O Ports
Each of the four 8-bit ports can be used for general-purpose input or output. Upon reset, all ports are configured as inputs. To configure a port as an output, a program must write a 0 to it. To reconfigure it as an input, a 1 must be sent to the port.
Port 0 (Pins 32-39)
- Function: Port 0 can be used as a standard 8-bit I/O port.
- Characteristics: It is an open-drain port, which means that when used for output, it can pull the pin to a low state (0V) but cannot drive it high (+5V). Therefore, to function correctly as an output port, Port 0 requires external pull-up resistors (typically 10KΩ) connected between each pin and Vcc.
- Dual Role: Port 0 serves a dual role as AD0-AD7, a multiplexed address and data bus. When the 8051 needs to access external memory (as with the 8031), this port carries the lower 8 bits of the address and the 8 bits of data. The ALE (Address Latch Enable) signal is used to de-multiplex this bus, indicating whether the pins are currently carrying an address or data.
Port 1 (Pins 1-8)
- Function: Port 1 is a standard 8-bit I/O port.
- Characteristics: Unlike Port 0, Port 1 has internal pull-up resistors. This simplifies its use as a general-purpose I/O port, as no external components are needed.
- Example Usage: To configure Port 1 as an output, you write data to it. To reconfigure it as an input, you must first write all 1s (FFH) to the port latch.
Port 2 (Pins 21-28)
- Function: Port 2 operates as a standard I/O port with internal pull-up resistors, similar to Port 1.
- Dual Role: Port 2 also has a dual role as the high-order address bus (A8-A15). When the 8051 is accessing external memory that requires a 16-bit address, Port 2 is used to output the upper 8 bits of that address. In this mode, it cannot be used for general I/O.
Port 3 (Pins 10-17)
- Function: Port 3 can be used as a standard I/O port.
- Characteristics: Each pin of Port 3 has a special alternate function related to the 8051’s internal peripherals. This makes it the most versatile of the four ports.
| P3 Bit | Function | Pin | Description |
| P3.0 | RxD | 10 | Serial Data Receive |
| P3.1 | TxD | 11 | Serial Data Transmit |
| P3.2 | Complement of INT0 | 12 | External Interrupt 0 (Active Low) |
| P3.3 | INT1 | 13 | External Interrupt 1 |
| P3.4 | T0 | 14 | Timer 0 External Input |
| P3.5 | T1 | 15 | Timer 1 External Input |
| P3.6 | WR | 16 | External Memory Write Strobe |
| P3.7 | Complement of RD | 17 | External Memory Read Strobe (Active Low) |
4.3 Hardware Connection and Control Pins
Proper connection of the non-I/O pins is crucial for the microcontroller’s operation.
- Vcc (Pin 40) and Gnd (Pin 20): These pins provide the +5V power supply and ground reference for the chip.
- XTAL1, XTAL2 (Pins 18, 19): The 8051 has an on-chip oscillator but requires an external timing source. A quartz crystal oscillator is connected across these two pins, along with two small capacitors (typically 30pF) to ground, to generate the system clock.
- RST (Pin 9): This is an active-high reset pin. Applying a high pulse to this pin will reset the microcontroller, terminating all activities and setting the Program Counter to 0000. This is known as a Power-On Reset.
- EA (External Access, Pin 31): This input pin determines where the processor fetches its program code. For microcontrollers with on-chip ROM (like the 8051), this pin should be connected to Vcc. For ROM-less versions (like the 8031), it must be connected to Gnd to indicate that the program code is stored externally.
- PSEN (Program Store Enable, Pin 29): This is an active-low output pin. It is used as a read strobe to enable external ROM when the 8051 is fetching program code.
- ALE (Address Latch Enable, Pin 30): This is an active-high output pin used to de-multiplex the address and data bus on Port 0. When ALE is high, Port 0 is outputting an address; when it is low, Port 0 is used for data transfer.
4.4 Bit Addressability and Single-Bit Instructions
A powerful feature of the 8051 is its ability to access individual bits of the I/O ports without affecting the other bits. The syntax for this is SETB X.Y, where X is the port number (0-3) and Y is the bit number (0-7).
For example, the following code will cause the bit at P1.2 to toggle on and off continuously:
AGAIN: SETB P1.2 ; Set bit 2 of Port 1 to high (1)
ACALL DELAY
CLR P1.2 ; Clear bit 2 of Port 1 to low (0)
ACALL DELAY
SJMP AGAIN
The 8051 provides a specific set of instructions for manipulating single bits.
| Instruction | Function |
| SETB bit | Set the specified bit to 1. |
| CLR bit | Clear the specified bit to 0. |
| CPL bit | Complement (invert) the specified bit. |
| JB bit, target | Jump to the target address if the specified bit is 1. |
| JNB bit, target | Jump to the target address if the specified bit is 0. |
| JBC bit, target | Jump to the target address if the specified bit is 1, and then clear the bit. |
Having examined the hardware-level control of I/O, we will now turn to the software constructs—assembly language and the instruction set—used to implement program logic.