IO Ports

There are 4 IO ports in standard 8051 microcontroller of 8 pins each. These IO ports are used to communicate to external world (devices). We can read and write on all pins of all ports. Some port pins have more than one function. All ports pins are set for reading upon microcontroller reset. These port names are

  1. Port 0
  2. Port 1
  3. Port 2
  4. Port 3

But, in programming these ports are termed as P0, P1, P2 and P3.

Port 0

This is an 8 pin IO port from pin 32 to 39. It is an open drain port and requires external pullup resistors of usually 10K Ohm. To configure a port pin as output we write 0 to this pin. Below is the C code to configure pin 3 of P0 as output pin.

P0_3 = 0; // Configure pin 3 of P0 as output.

To set it again for reading we write 1 to this pin and C code for doing this for pin 3 will be

P0_3 = 1; // Configure pin 3 of P0 as input.

Alternate Functions of Port 0

P0 pins are also called a AD0-AD7. These are used for address bus and data bus when we connect external memory to 8051 microcontroller. Address and data are multiplexed on this port to save pins for other purposes.

Port 1

This is also 8 pin port from pin 1 to 8. It has internal pullups and there is no need to attach external pullups. We can attach devices like keypad or LCD with it for generic IO. We can configure it’s any pin for input or output in the same way as for P0. The code to configure a pin of P1 for output is

P1_5 = 0; // Configure pin 3 of P1 as output.

Similarly, we can configure P1 pin as input

P0_5 = 1; // Configure pin 3 of P1 as input.

Port 2

Port 2 or P2 has pins from 21-28. This also has internal pullups. This port can also be used for general purpose IO. This port pins are configured in the same way as others.

Alternate Functions of Port 2

This port takes part as address bus high byte when we connect external memory to 8051 microcontroller. It’s alternate pin names are A8-A15. As P0 is used for 8 bit data and low address byte and P2 is used for high address byte so both ports combined become 16 bit wide address bus. In this way 8051 microcontroller accesses 64KB of external memory.

Port 3

Port 3 has pins from 10-17. This port is also internal pullups. This port can also be used for general purpose IO but it has important alternate functions that are discussed next. The method for configuring this port for input/output is same as of other ports discussed above.

Alternate Functions of Port 3

This port has transmit and receive pins for serial port of microcontroller. It has 2 external interrupts and 2 counting inputs for both timers (Timer0 and Timer1). Further it has write enable and read enable pins for external memory (when it is used).

Alternate functions of P3 are

P3 Pin Function Alternate Function Pin Description
P3_0 IO RxD 10 Serial Port Receive
P3_1 IO TxD 11 Serial Port Transmit
P3_2 IO INT0 12 External Interrupt 0
P3_3 IO INT1 13 External Interrupt 1
P3_4 IO T0 14 Timer0 Input
P3_5 IO T1 15 Timer1 Input
P3_6 IO WR 16 Write Enable External RAM
P3_7 IO RD 17 Read Enable External RAM