8051 Microcontroller port programming

There are four ports P0, P1, P2  and  P3 each  use  8 pins,  making  them 8-bit  ports. All the ports upon  RESET are configured as output, ready to be used as output ports. To use any of these ports as an input port, it must be programmed.

Pin configuration of 8051/8031 microcontroller.

Pin configuration of 8951
Pin configuration of 8951

Port 0: Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or output. To use the pins of port 0 as both input and output ports, each pin must be connected externally to a 10K ohm pull-up resistor. This is due to the fact that P0 is an open drain, unlike P1, P2, and P3.Open drain  is a term used for MOS chips in the same way that open collector is used for TTL chips. With external pull-up resistors connected upon reset, port 0 is configured as an output port. For example, the following code will continuously send out to port 0 the alternating values 55H and AAH

MOV A,#55H
BACK:  MOV P0,A
ACALL DELAY
CPL A
SJMP BACK

Port 0 as Input : With resistors connected to port 0, in order to make it an input, the port must be programmed by writing 1 to all the bits. In the following code, port 0 is configured first as an input port by writing 1’s to it, and then data is received from the port and sent to P1.

8051 I/O Ports
8051 I/O Ports

MOV A,#0FFH         ; A = FF hex
MOV P0,A                ; make P0 an input port
BACK: MOV A,P0               ;get data from P0
MOV P1,A                ;send it to port 1
SJMP BACK

Dual role of port 0: Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data. When connecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and data through port 0 to save pins. ALE indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE =1 it has address and data with the help of a 74LS373 latch.

Port 1: Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. In contrast to port 0, this port does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset, Port 1 is configured as an output port. For example, the following code will continuously send out to port1 the alternating values 55h  & AAh

MOV A,#55H                    ; A = 55 hex
BACK: MOV P1,A                        ;send it to Port 1
ACALL DELAY                  ;call delay routine
CPL A                               ;make A=0
SJMP BACK

Port 1 as input: To make port1 an input port, it must programmed as such by writing 1 to all its bits. In the following code port1 is configured first as an input port by writing 1’s to it, then data is received from the port and saved in R7 ,R6 & R5.

MOV A,#0FFH   ;A=FF HEX
MOV P1,A         ;make P1 an input port by writing all 1’s to it
MOV A,P1         ;get data from P1
MOV R7,A         ;save it in register R7
ACALL DELAY   ;wait
MOV  A,P1        ;get another data from P1
MOV R6,A         ;save it in register R6
ACALL DELAY   ;wait
MOV  A,P1        ;get another data from P1
MOV R5,A         ;save it in register R5

Port 2 : Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output. Just like P1, P2 does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset,Port 2 is configured as an output port. For example, the following code will send out continuously to port 2 the alternating values 55h and AAH. That is all the bits of port 2 toggle continuously.

MOV A,#55H                    ; A = 55 hex
BACK:  MOV P2,A                        ;send it to Port 2
ACALL DELAY                  ;call delay routine
CPL A                               ;make A=0
SJMP BACK

Port 2 as input : To make port 2 an input, it must programmed as such by writing 1 to all its bits. In the following code, port 2 is configured first as an input port by writing 1’s to it. Then data is received from that port and is sent to P1 continuously.

MOV A,#0FFH         ;A=FF hex
MOV P2,A                ;make P2 an input port by writing all 1’s to it
BACK: MOV A,P2     ;get data from P2
MOV P1,A                ;send it to Port1
SJMP BACK             ;keep doing that

Dual role of port 2 : In systems based on the 8751, 8951, and DS5000, P2 is used as simple I/O. However, in 8031-based systems, port 2 must be used along with P0 to provide the 16-bit address for the external memory. As shown in pin configuration 8051, port 2 is also designed as A8-A15, indicating the dual function. Since an 8031 is capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. While P0 provides the lower 8 bits via A0-A7, it is the job of P2 to provide bits A8-A15 of the address. In other words, when 8031 is connected to external memory, P2 is used for the upper 8 bits of the 16 bit address, and it cannot be used for I/O.

Port 3 : Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. P3 does not need any pull-up resistors, the same as P1 and P2 did not. Although port 3 is configured as an output port upon reset. Port 3 has the additional function of providing some extremely important signals such as interrupts. This information applies both 8051 and 8031 chips.

Functions of port 3
Functions of port 3

P3.0 and P3.1 are used for the RxD and TxD serial communications signals. Bits P3.2 and P3.3 are set aside for external interrupts. Bits P3.4 and P3.5 are used for timers 0 and 1. Finally P3.6 and P3.7 are used to provide the WR and RD signals of external memories connected in 8031 based systems.

Read-modify-write feature : The ports in the 8051 can be accessed by the read-modify-write technique. This feature saves many lines of code by combining in a single instruction all three action of (1) reading the port, (2) modifying it, and (3) writing to the port. The following code first places 01010101 (binary) into port 1. Next, the instruction “XLR P1,#0FFH” performs an XOR logic operation on P1 with 1111 1111 (binary), and then writes the result back into P1.

MOV P1,#55H      ;P1=01010101
AGAIN: XLR P1,#0FFH     ;EX-OR P1 with 1111 1111
ACALL DELAY
SJMP AGAIN

Notice that XOR of 55H and FFH gives AAH. Likewise, the XOR of AAH and FFH gives 55H.

Single bit  addressability of ports:  There are times that we need to access only 1 or 2 bits of the port instead of the entire 8 bits. A powerful feature of 8051 I/O ports is their capability to access individual bits of the port without altering the rest of the bits in that port.

For example, the following code toggles the bit p1.2 continuously.

BACK:  CPL P1.2                   ;complement p1.2 only
ACALL DELAY
SJMP BACK

Notice that P1.2 is the third bit of P1, since the first bit is P1.0, the second bit is P1.1, and so on. Notices in example of those unused portions of port1 are undisturbed. Table bellow shows the bits of 8051 I/O ports. This single bit addressability of I/O ports is one of the features of the 8051 microcontroller.

Addressing bits

94 Responses to “8051 Microcontroller port programming”

  1. montu patel

    Sir,
    I am programming on at2051 atmel microcontroller,
    I connect push button switch and eprom24c02
    my pin connection of 2051 is
    pin1 connect via capacitor to eprom pin8
    pin2 connect eprom pin 5(SDA)
    pin3 connect to eprom pin 6(SCL)
    pin4,5 use for oscillator
    pin6 coneect to pushbutten swich
    pin7,8,9 (not connected)
    pin 10 ground
    pin11,12,13 (not connected)
    pin 14 ,15,16,17,18,19 connect to led
    pin 20 supply

    Eprom pin
    pin 1 (not connected)
    pin2,3,4 ground
    pin5 2051 pin 2
    pin6 2051 pin 3
    pin7 ground
    pin8 connect via capacitor to2051 (1) pin

    swich othr terminal is grounded

    my quary is i want to made a program to whaen switch pressed pattern change of led there is 15 pattern in my controller my eprom is not programeble

    Reply
  2. Prabhakar G

    Hello,

    Could you please let me know in detail Y only port 0 is left without pull up resistors in 8051.

    Also let me know in more detail, y we have to send only 0 to any port to configure as output and 1 to configure as input. may be explain in application point of view.

    Reply
  3. Abhijith R Prasad

    Sir,
    The project I am doing is digital tachometer with speed monitoring system using 8051.It accept the working speed limits as input( say 1000- 800,upper limit and lower limit). While entering(Using a keypad) the speeds they are displayed. Then from a magnetic sensor it accepts the speed as input and displays it.If the speed is over the limit(Say 1050)it give an LED alarm. If below the speed(say 700) then it gives another led alarm.Can I get source code for 8051 micro controller for this?????

    Reply
  4. Sanju ambiger

    sir…….. i am doing 8051 project , ie i should interface 4 led’s and display bainary values to that please help me in writing embedded c code ……………..

    Reply
  5. anshu jain

    guys,i m working on a project and i found program for this.
    but am not getting the code can somone tell me
    in which port which switch connected like
    program
    #include // special function register declarations
    // for the intended 8051 derivative
    static int Y;
    sbit x=P3^3;
    void zcr(void);
    void msecdelay2();
    void msecdelay5();
    void main()
    {P1=0x0ff;
    P2=0x00;
    x=1;
    while(1)
    {
    if(P1=0x0fe)
    {
    y=0;
    while(P1==0x0f5)
    {
    if(y==0)
    {
    zcr();
    msecdelay2();
    P0=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==1)
    {
    zcr();
    msecdelay2();
    P2=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==2)
    {
    zcr();
    msecdelay2();
    P2=0xf0;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==3)
    {
    zcr();
    msecdelay2();
    P0=0x0f0;
    msecdelay5();
    P2=0x00;
    y++;
    }
    y=0;
    }
    }
    }
    else if(P1=0xfc)
    {
    y=0;
    while(P1==0xFA)
    {
    if(y==0)
    {
    zcr();
    msecdelay2();
    P2=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==1)
    {
    zcr();
    msecdelay2();
    P2=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==2)
    {
    zcr();
    msecdelay2();
    P2=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==3)
    {
    zcr();
    msecdelay2();
    P0=0xf0;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==4)
    {
    zcr();
    msecdelay2();
    P2=0xf0;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==5)
    {
    zcr();
    msecdelay2();
    P2=0xf0;
    msecdelay5();
    P2=0x00;
    y=0;
    }
    }
    }
    else
    {
    if(y==0)
    {
    zcr();
    msecdelay2();
    P2=0x0f;
    msecdelay5();
    P2=0x00;
    y++;
    }
    if(y==1)
    {
    zcr();
    msecdelay2();
    P2=0xf0;
    msecdelay5();
    P2=0x00;
    y==0;
    }
    }
    }
    }
    void zcr(void)
    {
    while(x==1);
    while(x==0);
    }
    void msecdelay2()
    {
    unsigned int i,j;
    for(i=0;i<1;i++)
    {
    for(j=0;j<50;j++)
    {;}
    }
    }
    void msecdelay5()
    {
    unsigned int m,n;
    for(m=0;m<5;m++)
    {
    for(n=0;n<100;n++)
    {;}
    }
    }

    Reply
  6. help please
    how do i initiate port 3 as an input port in assembly?
    acutely there is push switch connecting to p3.2
    push switch connecting to p3. 3
    push switch connecting to p3.4
    push switch connecting to p3.5
    and i need to call function when the user pressed the push switch for the previous pins (not usedas counter and notas timer… just call a function ( do special thing) when asny push switch is pressed ???????

    Reply
  7. Hello, i am satish. This is my programme. I want to operate p0.1,0.2 pins based on bit on port 1.0. Programme has no errors and warnings. But with code i am not getting required output, but code is executing. I am using keil u vision3.
    Please can any one clear this problem?

    org 00h
    mov a,#0ffh
    mov p1,a ;makes an input port
    start:jnb p1.0,start ; check for bit on p1.0 otherwise go to next instruction
    sjmp loop2 ; go to time delay loop
    mov a,#00h
    mov p0,a ;make p0 as output port
    setb p0.1 ; put 1 on port 0.1
    acall loop2 ;again go to time delay loop
    setb p0.2 ;put 1 on port 0.2
    here:jb p1.0,here
    acall stop
    acall start
    loop2:mov r1,#02h
    loop1:mov r2,#0ffh
    mov tmod,#01h
    mov th0,#0f0h
    mov tl0,#5fh
    setb tr0
    jb tf0,l1
    l1:djnz r2,loop1
    djnz r1,loop2
    ret
    stop:mov a,#00h
    mov p0,a
    ret
    end

    Reply

Leave a Reply