All 8051 microcontrollers have 4 input-output (IO) ports each of 8-bits which can be configured as inputs or outputs. The four ports are port 0, port 1, port 2 and port 3. Let’s understand the port structure of 8051 microcontroller for each port.
Input/Output Ports in 8051 Microcontroller
- In order to configure a microcontroller pin as an output, it is necessary to apply a logic zero (0) to appropriate I/O port bit. In this case, voltage level on appropriate pin will be 0.
- Similarly, in order to configure a microcontroller pin as an input, it is necessary to apply a logic one (1) to appropriate port. In this case, voltage level on appropriate pin will be 5V (as is the case with any TTL input).
- By default, the port is defined as output port.
Port 0 Structure of 8051
- Port 0 has dual function as I/O port and Address/Data bus for external memory. When external memory is not used, port 0 is used as simple I/O port.
- When external memory is used, port 0 is used as address bus and data bus. Lower byte address and data are multiplexed in this port.
- ALE signal is used to demultiplex the address from this port. ALE goes high, when this port contains address. After addressing ALE goes low and the port become data bus.
- To read the data from external memory logic 1 is written to all port SFR latches.
- The Special Function Register (SFR) for each port is made up of eight latches, which can be address for that port. The port latches should not be confused with the port pins.
- The data in the latches does not have to be the same as that on port pins. The data in latch buffer and the data of ports pins can be read independently.
Port 1 Structure of 8051
- Port 1 -only port in 8051 -used exclusively for input and output operation.
- The output of the port latch is connected to the port pin through a transistor driver with internal pull up resistor. The port can be operated as an input after writing 1 to all the bits of port 1 latch.
- 8051 ports are organized such that most instructions read the data from the pin for read operation and some instructions read the data from the latch.
- So, the input buffer consists of the select logic and the related control signals – ‘Read Latch’, ‘Read Pin’ for discriminating this.
Port 2 Structure of 8051
- Port 2 is similar to port 0 having dual function. In addition to I/O operation it can address external memory also.
- When 64K external memory is connected, 16-bit address is required. Lower byte address is carried by port 0 and higher byte address is carried by port 2. Since data is 8-bit, only address is carried in this port.
- I/O operation of this port is like port 0. By default, this port is an output port. To make this port as input port the port SFR latch buffer should be set to all 1s.
Port 3 Structure of 8051
- In terms of hardware, this port is similar to P0, with the difference that its pins have a pull-up resistor built-in.
- Port 3 is different from the other ports in the aspect that individual port pins can be programmed for input and output operation.
- Each pin of port 3 can be programmed – for input, output operation or alternate functions.
- All the Port3 pins serve an alternate function according to the hardware signals and interfacing.
- The alternate functions can be activated only if the port 3 bits are written with 1s in their position.
- Alternate functions for port 3 are as follows.
PORT 3 Pin
Serial input data
Serial output data
External interrupt 0
External interrupt 1
External timer 0 clock
External timer 1 clock
External memory write enable
External memory read enable
Now let’s have a look over each port in a single diagram.