View Course Path

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

The 8051 has four important ports. Port 0, Port 1, Port 2 and Port 3. These ports allow the microcontroller to connect with the outside world. The four ports of 8051 microcontrollers have certain specific functions and corresponding features. In this post, we will take a gander at the purpose of each of these ports.

What are the features of the four ports of 8051?

  • Each port has 8 pins. Thus the four ports jointly comprise 32 pins.
  • All ports are bidirectional.
  • They are constructed with a D type output latch. They have output drivers and input buffers.
  • We can modify their functions using software and hardware that they connect to.
  • 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

  • Address is 80H
  • Construction: Port 0 has a D-type latch, unidirectional buffer, and 2 FETs at each pin. It does not have an internal pull-up resistor. An external pull-up resistor is needed when Port 0 is defined as an output port.
  • 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

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.

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

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:

  • 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

  • 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

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

  • 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

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

Reading data from a port and storing in an internal register

MOV A,#FFHMOV 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 accumulatorMOV R7, A // stores the data in a register

Writing data to a port

MOV A,#00HMOV P1, A // sets the port as an input portSETB 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

Using Read modify write commands to toggle pins of a port

MOV P1,#55H // writes 0101 0101B to port 1AGAIN: XRL P1,#0FFH // performs logical XOR between 1111 1111B and 0101 0101B and writes the result to port 1(AAH)ACALL DELAY //calls delay functionSJMP AGAIN //returns control to againDELAY: MOV R1, 255HERE: DJNZ R1, HERERET

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 highCLR P1.5 // Turns off the buzzerSJMP HERE //Continuously does this again

We hope that reading this article has helped you gain a better understanding of the ports on the 8051 microcontroller. If you have any doubts feel free to contact us through the comments section

 

 

 

Leave a Reply

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