8.0 Interfacing with the World: I/O Programming
The primary function of most embedded systems is to interact with the physical world by sensing inputs and controlling outputs. The 8051 accomplishes this through its four 8-bit input/output (I/O) ports. This section provides a practical guide to the 8051’s pinout and how to configure and use its ports to control external devices.
8.1 Pin Configuration and Hardware Connections
The standard 8051 comes in a 40-pin Plastic Dual Inline Package (PDIP). While 32 of these pins are dedicated to the four I/O ports, several others are essential for the chip’s basic operation.
- Vcc (Pin 40): The +5V power supply pin that provides power to the chip.
- GND (Pin 20): The ground reference pin, completing the electrical circuit.
- XTAL1 / XTAL2 (Pins 19 / 18): Connections for an external quartz crystal. This provides the stable system clock signal that drives all operations.
- RST (Pin 9): The reset pin. A high pulse on this pin resets the microcontroller, terminating all activities and setting the Program Counter to 0000H.
- EA (Pin 31): External Access. This pin is critical; tying it high (+5V) tells the chip to execute code from its internal ROM, which is the standard mode of operation. Tying it low (GND) is only done for ROM-less models like the 8031 that fetch code from external memory.
- PSEN (Pin 29): Program Store Enable. This active-low output pin is used as the read strobe for external program memory.
- ALE (Pin 30): Address Latch Enable. This active-high output pin is used when accessing external memory to demultiplex the address and data lines on Port 0.
8.2 The Four I/O Ports
Upon reset, all four 8-bit ports (P0, P1, P2, P3) are configured as inputs. To use a port pin as an output, you must write a 0 to that bit. To configure it back as an input, you must write a 1 to the bit. This can seem backward, but think of it this way: writing a ‘1’ effectively disconnects the output driver, allowing the pin to float and act as a high-impedance input.
- Port 0 (Pins 32-39): A dual-purpose 8-bit port. For general I/O, it is an open-drain port and requires external pull-up resistors. It also serves as a multiplexed address/data bus (AD0-AD7) when connecting to external memory.
- Port 1 (Pins 1-8): A dedicated 8-bit I/O port with internal pull-up resistors. It is the simplest port to use for basic input and output.
- Port 2 (Pins 21-28): A dual-purpose 8-bit port with internal pull-up resistors. When accessing external memory, it provides the upper byte of the 16-bit address (A8-A15).
- Port 3 (Pins 10-17): An 8-bit port with internal pull-up resistors. In addition to general I/O, each pin of Port 3 has a special alternate function related to interrupts, timers, and serial communication.
| Port 3 Pin | Alternate Function | Description |
| P3.0 | RxD | Serial Receive Data |
| P3.1 | TxD | Serial Transmit Data |
| P3.2 | INT0 | External Interrupt 0 (Active Low) |
| P3.3 | INT1 | External Interrupt 1 (Active Low) |
| P3.4 | T0 | Timer 0 External Input |
| P3.5 | T1 | Timer 1 External Input |
| P3.6 | WR | External Data Memory Write (Active Low) |
| P3.7 | RD | External Data Memory Read (Active Low) |
8.3 Bit Addressability
One of the most powerful features of the 8051 is bit addressability. This allows a programmer to change the state of a single bit on a port without affecting the other seven bits. The syntax is SETB X.Y, where X is the port number (0-3) and Y is the bit number (0-7).
For example, the instruction SETB P1.5 will set bit 5 of Port 1 to high (1) while leaving bits P1.0-P1.4 and P1.6-P1.7 unchanged.
The 8051 provides a set of instructions specifically for single-bit manipulation.
| 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 target address if the bit is 1. |
| JNB bit, target | Jump to target address if the bit is 0. |
| JBC bit, target | Jump if bit is 1, then clear the bit. |
Direct I/O control is powerful, but many applications require more sophisticated, time-based operations managed by the 8051’s internal timers.