How to use input output ports 8051 microcontroller|LED blinking

How to use input output ports 8051 microcontroller. 8051 microcontroller has 40 pins comprising of four I/O ports. 8 pins are used for specific purposes and 32 pins are used as input/output pins to connect the microcontroller with the peripheral devices. Each of PORT is 8-bit, which can be configured as input or output port. In this article we will learn how to use I/O ports of 8051 microcontroller so that they can be used for read and write purpose. Before reading this article further you should know how to program 8051 microcontroller with Keil.

A port is usually a set of eight pins and we call them IO because we can configure them either input or output. 8051 have four GPIO ports, and each port have eight pins, while each pin may have one or more than one functions. We will see them one by one.

PIN DIAGRAM of  input output ports 8051 microcontrollerinput output ports of 8051 microcontroller

PORTS DESCRIPTION of  input output ports 8051 microcontroller

PORT 0 of 8051 microcontroller: 

Now let’s have a look on IO Port 0 Port 0 is 8 open drain bi-directional IO port. Port 0 is 8 open drain bi-directional IO port, Open drain simply means a transistor that connects to Gnd. Bi-directional means this PORT may be configured as input or output. P0 register is used for accessing PORT0.  This register is bit accessible, if some register is bit accessible. That means we can access individual bits of that register. writing 1 to some bit of P0 register will configure corresponding pin as input while writing 0 will configure that as output. By default, every pin is configured as input on power up.

P0 includes pins (32-39). It is an I/O port with some alternative functions.

  • When the external memory is used with microcontroller, then the lower address byte (addresses A0-A7) is applied on P0. If not using external memory, all bits of P0 are configured for I/O purposes.
  • P0doesn’t contain built-in pull-up resistor.
    • If any pin of this port is configured as an input, then it act as it “floats” and input has unlimited input resistance and undetermined potential.
    • When any pin of this port is configured as an output, it acts as an “open drain”. If logic 0 is given to port bit, the pin will be connected to ground (0V). If logic 1 is given to port bit, the external output will keep on “floating”. So, to apply logic 1 (5V) on this output pin, it is necessary to connect an external pull-up resistor.

Let’s write some simple code for testing Port 0, I’ll configure lower four bits of PORT0 as input and upper four bits as output and whatever input I’ll apply on lower four bits, same should appear on upper four bits.  let’s configure lower four bits of Port 0 as input and upper four bits as output As I’ve mentioned earlier, for configuring some pin of P0 as input, we write 1 to corresponding bit of P0 register. As now we have to configure lower four bits of P0 as input we write 1 to power four bits. And for configuring upper four pins of P0 as outputs, we write 0 to all upper four bits of P0 register, in hexadecimal, it will become 0x0F.

P0=0X0F;

PORT 1 OF 8051 Microcontroller

P1 includes pins (1-8). It is an I/O port with no alternative functions and configured only as general I/O purposes. P1 contains built-in pull-up resistor and is compatible with TTL circuits.8051 microcontroller input output ports

PORT 2 of 8051 

P2 includes pins (21-28). It is an I/O port with alternative functions as port 0 except P1 when the external memory is used with microcontroller, then the higher address byte (addresses A8-A15) is applied on P2. When no external memory is used, all bits of P0 are configured as I/O purposes.

And now we are going to see Port 1 and port 2 with interesting and simple examples. Both ports are 8 bit wide, bi-directional and have internal pull-ups.  But there is no need for any pull up resistors for both Port 1 and Port 2 as both ports have already internal pull ups Writing 1 to each bit of a port configures that port as input while writing zero configures that as output. Writing 1 to each bit of a port configures that port as input while writing zero configures that as output. By default, both ports are configured as input on power up.

Simple program to configure port 1 as a input and port 2 as output

I’ll make a simple program that will configure Port 1 as input and Port 2 as output and whatever input I’ll apply to Port 1 will be sent to Port 2. Now let’s try to code this example, we can write 0xFF to Port 1 for configuring it as input and we also skip this step. Now let’s try to code this example, we can write 0xFF to Port 1 for configuring it as input and we also skip this step as Port 1 is already configured as input on power up by default, we can configure Port 2 as output by writing 0 to it as Port 1 is already configured as input on power up by default, we can configure Port 2 as output by writing 0 to it.

void main() 

{
P1=0xFF;
P2=0x00;
while(1){
P2=P1;
}
}

PORT 3 of 8051

P3 includes pins (10-17). It is an I/O port with different function. For using the alternative functions, a logic one (1) must be applied to appropriate bit of the P3 register. In hardware terms, this port is similar to P0 but it contains built-in pull-up resistor.

PIN CONFIGURATION SETTINGS of input output ports 8051 microcontroller

For input:        Pin is configured as 1 for input.

For output:      Pin is configured as 0 for output.

Current limitations:

  • When pin is are configured as an output, single pin can receive a current of 10mA.When pin is configured as input, then built-in pull-up resistors provide very weak current, but can handle up to 4 TTL inputs.
  • If a port’s all 8 bits are active, then the total current must be limited to 15mA. But for port 0, it can be up to 26mA (port P0: 26mA).
  • If all 4 ports (32 bits) are active, then the total current must be limited to 71mA.

LED blinking with input output ports 8051 microcontroller

In this example I will explain how to use I/O ports of 8051 microcontroller. Input to the microcontroller can be given through push button and output can be seen through LED. Firstly all the port pins are defined. Port 2 is used as output port and port 3 is used as input port.  These Ports are initialized with 0 (to write) and 1 (to read) logic respectively. Delay function is used to give the delay in showing output. Frequency of crystal should be set to 11.059MHz. Program is written in C language using “keil” software and hex file isloaded in the microcontroller.

WORKING of input output ports 8051 microcontroller:

When we run the simulation, if first push button is pressed, first led will glow. It will keep glowing for 1 second and then it goes off. When 2nd push button is pressed, 2nd led will glow. Any LED can be ON according to the respective input of the button. If we want to show the output for more time then delay call is passed large integer value.

PROTEUS SIMULATIONS of LED blinking with  input output ports 8051 microcontrollerinput output ports 8051 microcontroller

Video lecture on input output ports of 8051 microcontroller

CODE of input output ports 8051 microcontroller

#include<reg51.h>
sbit Led1 = P2^0;          //Defining LED Pins

sbit Led2 = P2^1;

sbit Led3 = P2^2;

sbit Led4 = P2^3;

sbit Button1 = P3^0;      //Defining Button Pins

sbit Button2 = P3^1;

sbit Button3 = P3^2;

sbit Button4 = P3^3;

void Delay(int);     //Delay function declaration

int main ()

{

  P2 = 0x00;                                          //used as output port

  P3 = 0xFF;                                          //used as input port

do

{

if(Button1 == 0 )                               //If 1stswitch pressed

    {

      Led1 = 1;                                        //1st LED ON

Delay(1000);      

      Led1 = 0;                                        //LED OFF

    }

                if(Button2 == 0 )               //If 2ndswitch pressed

    {

      Led2 = 1;                                        //2nd LED ON

Delay(1000);

      Led2 = 0;

    }

                if(Button3 == 0 )               //If 3rdswitch pressed

    {

      Led3 = 1; //LED ON                    //3rd LED ON

Delay(1000); //Delay

      Led3 = 0; //LED OFF

    }

                if(Button4 == 0 )               //If 4thswitch pressed

    {

      Led4 = 1; //LED ON                    //4th LED ON

Delay(1000); //Delay

      Led4 = 0; //LED OFF

    }

}

                while(1);

}




void Delay(int k)

{

int j;

inti;

for(i=0;i<k;i++)

  {

for(j=0;j<100;j++)

    {

    }

  }

}

So this is all about how to use input output of 8051 microcontroller.  Input port of 8051 microcontroller can be used to get input from user or external world like one want to connect switch with 8051 microcontroller and similarly these ports can also be used as a output to show indication. For example for LED matrix, dc motor driver, seven segment display and LCD. There are hundreds of others examples of input output ports.

3 thoughts on “How to use input output ports 8051 microcontroller|LED blinking”

  1. What has to change in code if we want to use only 3 buttons and 4 LEDs
    so the 4th LED will be on only if Button2 & Button3 are both pressed???

    Reply

Leave a Comment