View Course Path

Ports of 8051 – Functions and specifications of the four I/O ports

 Updated for 2025: This guide on the ports of the 8051 microcontroller remains just as relevant today for students, hobbyists, and professionals working with embedded systems. To improve clarity, we have refined the structure, enhanced code readability, and made the explanations more concise. Whether you’re learning about I/O ports in 8051, understanding their functions and configurations, or looking for practical code examples, this article will help you grasp the core concepts effectively. 

The 8051 microcontroller features four I/O portsPort 0, Port 1, Port 2, and Port 3—each serving distinct roles in data transfer and external interfacing. These input-output ports are crucial for communication with peripherals, handling both digital input and output functions. Understanding their structure, functions, and special features is key to efficient 8051 programming and circuit design. This guide breaks down each port’s role, helping you maximize the microcontroller’s capabilities.

What are the features of the four ports of 8051 Microcontroller?

  • Each port has 8 pins. Thus the four ports jointly comprise 32 pins.
  • All ports are bidirectional, they can function as both input and output.
  • They are constructed with a D type output latch. They have output drivers and built-in input buffers.
  • We can modify their functions using software instructions and external hardware connections.
  • All the ports are configured as input ports on Reset.
  • To configure ports as an input port, 1 must be written to that port
  • To configure it as an output port, 0 must be written to it.
Ports of 8051 - Construction pin schematic Port 0, 1, 2, 3
Refer to this schematic showing the construction of the Ports of 8051 pins

What are the features and functions of Port 0 in 8051?

Features of Port 0 in 8051

  • Address: 80H
  • Construction: Port 0consists of a D-type latch, a unidirectional buffer, and two FETs at each pin.
  • No Internal Pull-up Resistor: Unlike other ports, Port 0 requires an external pull-up resistor (typically 10kΩ) when used as an output.
  • Port 0 of the 8051 has two main functions: To be used as a simple input-output port and to access external memory in conjunction with Port 2.

Functions of Port 0 in 8051

  1. Simple I/O port:

    When we use Port 0 as an input port, the internal latch should know that it’s being used for input, and thus, a digital 1 (FFH) is written at the port address of 80H. This turns off the transistors causing the pin to float in high impedance state connecting it to the input buffer. We can read data from ‘Read Pin Data’/’Read Latch Bit.

    When we use Port 0 as an output port, the latch programmed to 0 will turn on. Consequently, the FET will connect to GND. We will require an external pull up resistor(10k Ohm) here to give a logic ‘1’ for using Port 0 as an output port.

  2. Access external memory:

    When the 8051 wants to access external memory, the address for the memory generates due to Port 0 and Port 2. We get the lower half of the address from Port 0 and the upper half from Port 2. This is done using ALE pulses, which help to latch the address to the external bus. Once done, the Port 0 goes back to being an input port to read data from that memory.

Working of Port 0 in 8051

As mentioned above port one has a lot up its sleeve, from reading data to addresses it does a lot of things for the microcontroller. Therefore it is imperative for us to get a deeper understanding of the workings of this port.

To configure port 0 as an input port the internal bus writes 1 to the D flip flop and the control pin is set to 0(Upper FET is OFF). The mux is connected to Q'(0) of the D flip flop as the control pin is 0. Due to this, the pin is connected to the input buffer which can be read to get the input data.

To use the port as an output port 0 is written to the D flip flop with the control signal being set to 0. This enables the lower FET and disables the upper FET due to this the pin gets connected to the ground and a zero is written to the output device. To write a 1 to the external device the microcontroller writes 1 to the D flip flop which drives the pin to a high impedance state as it is not connected to either VCC or ground. To solve this problem a pull-up resistor is connected to the output pin which pulls the value to 5v or logic 1.

For reading Addresses or data from external memory the Control bit is set to set to 1 which connects the Mux to Data/address pin. The ALE pin is used to latch the address and once that is done the port is used for data transfer.

What are the features and functions of Port 1 in 8051?

Features of Port 1 in 8051:

  • Address is 90H
  • Construction: Port 1 has one D latch, two unidirectional buffers, 1 FET, and one internal pull-up resistor at each pin.
  • It has only one function – to act as an Input-Output port.

The function of Port 1 – I/O port:

When Port 1 is functioning in the capacity of an input port, a digital ‘1’ (FFH) is written to the latch. At 90H. This turns off the transistor, and the pin floats in a high impedance state. Consequently, it connects to the input buffer.

When Port 1 is functioning in the capacity of an output port, the latch is given a ‘LOW’ signal (00H). This turns the FER (Field Effect Transistor) o. The pull-up resistor is OFF, and the port is used as an output port.

What are the features and functions of Port 2 in 8051?

Features of Port 2 in 8051

  • Address is 10H
  • Construction: Port 2 has a D type latch, 1 FET, an internal pull-up resistor, two unidirectional buffers, and a Control Logic block.
  • Its main functions are kind of similar to those of Port 0. It can be used as an input-output port. And can access external memory in conjunction with Port 0.

Functions of Port 2 in 8051

I/O port:

Quite similar to Port 0. The only difference here is that in Port 2, we use one FET with an internal pull-up resistor instead of the two FETs we saw in Port 0.

Memory Access:

Port 2 is used in conjunction with Port 0 to generate the upper address of the external memory location that needs to be accessed. However, one key difference is that it doesn’t need to turnaround and get a 1 in the latch immediately for input as in Port 0. It can remain stable.

What are the features and functions of Port 3 in 8051?

Features of Port 3 in 8051

  • Address is B0H
  • Construction: The third Port of 8051 has a D-type latch. In addition to that, it has three unidirectional buffers. A FET with an internal pull-up resistor. Additionally, it also has a NAND gate connected to the FET.
  • Port 3 performs two main functions, as we will see below.

Functions of Port 3 in 8051

I/O port

Just like Port 2, Port 3 can function as an input-output port.

Alternate SFR function

The input to SFR 1, we get the output of latch as 1, which turns on the NAND gate, and depending on the value of ‘Alternate Output Pin,’ FET will be wither ON/OFF.

port 3 of 8051 alternate functions uses of the pins

RXD: this is used for a serial input port
TXD: this is used for serial output port
INT0: this used for an external interrupt 0
INT1: this used for external interrupt 1
T0: Timer 0 external input
T1: Timer 1 external input
WR:  external data memory write strobe
RD: external data memory Read strobe

What to read latches or buffers?

If you look at the diagrams of all the ports you will see that each port has input buffers and latches. Bringing up the question of which instructions read the latches and which read the buffers.

Given below is a list of instructions which read the input buffers

Instruction Example Explanation
MOV
MOV A, port Moves data from a given port to the carry flag
MOV A, port Moves data from a given port to the accumulator
JNB JNB port, Address Cheks the value in the input buffer. If the value is zero then it transfers the control to the given address
JB JB port, Address Cheks the value in the input buffer. If the value is not zero then it transfers the control to the given address
CJNE CJNE A,port,Address Cheks the value in the input buffer of a port. Compares it to the value in the accumulator. If the values are not the same then the control is transferred to a given address

The table given below gives all the instruction which read the value from the latch of the port

Instruction Example Explanation
ANL ANL P1, A Performs logical AND between the value stored in the latch of the port and the accumulator. After this, it writes the new value to the latch
ORL ORL P1, A Performs logical OR between the value stored in the latch of the port and the accumulator. After this, it writes the new value to the latch
XRL XRL P1,A Performs logical XOR between the value stored in the latch of the port and the accumulator. After this, it writes the new value to the latch
JBC JBC,port,Address If the value at a given port is 1 then it jumps to a given address and then clears the latch
CPL CPL, port Complements the data in the latch
MOV MOV port, C Reads the value at the latch of a given port and then transfers it to the carry flag
CLR CLR port Clears the value stored at the latch of a port
SETB SETB port Sets the value of a latch

These instructions are known as read modify instructions as they read the values from the latch and then change those values immediately

Examples of Using 8051 Microcontroller Ports

Reading data from a port and storing in an internal register

MOV A,#FFH
MOV P1,A //sets port 1 as input port 
MOV A, P1 //reads the input buffer to get the data from the input port and stores it in the accumulator
MOV R7, A // stores the data in a register
  • FFH (1111 1111B) is written to Port 1, making all pins input.
  • Data is read from Port 1 into the accumulator (A).
  • Accumulator value is then moved to register R7 for further processing.

Writing data to a port

MOV A,#00H
MOV P1, A // sets the port as an input port
SETB P1 // writes 1 to the latch of port1 
CLR P1 //clears the data in the latch of the port writing a 0 to the port
  • 00H is written to Port 1, configuring it as an output port.
  • SETB P1 sets all bits to 1 (logic HIGH/5V).
  • CLR P1 clears all bits to 0 (logic LOW/0V).

Using Read modify write commands to toggle pins of a port

MOV P1,#55H // writes 0101 0101B to port 1
AGAIN: 
   XRL P1,#0FFH // performs logical XOR between 1111 1111B and 0101 0101B and writes the result to port 1(AAH)
   ACALL DELAY //calls delay function
   SJMP AGAIN //returns control to again
DELAY:  
   MOV R1, 255
HERE: 
   DJNZ R1, HERE
   RET
  • 55H (0101 0101B) is written to Port 1.
  • XRL P1, #FFH toggles the bits, reversing the pattern (AAH = 1010 1010B).
  • A delay function ensures visible toggling.

One of the most popular features of the 8051 is the bit addressable nature of the ports on the 8051. This allows the programmer to get data from one pin and then use it for processing. The bit addressable nature is used in a lot of applications. Let us say a switch is connected to P2.3 and a buzzer is connected to port 1.5. Now if you want to sound the buzzer every time the switch is pressed you could use the bit addressable nature of the controller. Given below is a machine-level code to do the same

HERE:
   JNB P2.3,HERE // waits for the switch to be pressed
   SETB P1.5 // makes the pin connected to the buzzer high
   CLR P1.5 // Turns off the buzzer
   SJMP HERE //Continuously does this again

Further Reading…

The 8051 microcontroller ports are highly flexible and can be used for a variety of input and output applications. By understanding basic port operations, you can build efficient microcontroller-based systems for sensor interfacing, LED control, communication, and automation.

  1. NXP 8051 Microcontrollers Reference
    Covers features and technical details of 8051-based MCUs.

  2. Keil 8051 Development Tools
    Keil’s C51 Compiler and development tools for 8051 programming.

  3. Stack Exchange: Electronics Q&A
    Community-driven Q&A forum for troubleshooting 8051-related issues.

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.