8051 Microcontroller port programming

Wednesday, November 19th, 2008

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

Avatar of Ranjith

Author Name :
Ranjith

Total : 84 Comments


84 Responses to “8051 Microcontroller port programming”

  1. arnab says:

    how can i control traffic signal using 8051 and 8085?

  2. ashwini says:

    why the ports in 8051 are loaded with 0xff instead of 0×00????
    wha tis the value of internal and external pull up in 8051???

  3. vijay parmar says:

    mov a,#ffh
    mov a,p0
    end

    if i want to use port as output port ands end data ffh …so in above instruction it is configure as input port or send ffh to port.here it use same instructions for input configuration and send data to o/p port…………..

  4. nets mertia says:

    kya yr puti mazidi chap rkhi h …………..

  5. deepa says:

    Why pullup resister are used??

  6. HARI KRISHNAN says:

    Sir we needed to PROGRAM and full details of the MULTILEVEL PRODUCTION FOR GAS ACCIDENT LEAKAGE AND DETECTOR BY USING AT89C51 MICROCONTROLLER.

    Sir please send the details for following my address…………

    HARI KRISHNAN………………..

  7. Napster King says:

    Dude, its tym dat u stop copying content from 8051 Microcontroller and Embedded Systems by Mazidi and use ur brain and understanding about 8051 to use this precious web page………………….

  8. taniya says:

    sir will you kindly help me in writind a program that use 8051 ,once some binary 8 bit code is stored and burned in it then anyone can “invalid”? it is very urgent.

  9. tushar thakar says:

    sir pls tell me where microcontroler used and why it is required

  10. taniya says:

    no1 will reply you thakar.
    in is used in RAM and memory design ckts, programming on every scale

  11. Aspen says:

    An inlteligent answer – no BS – which makes a pleasant change

  12. arpana says:

    why u r writing one to make port as input port

  13. paranthaman says:

    sir pls send me embedded c program for eye blink sensor

  14. manpreet says:

    cn u tell me how cn v store 09F in microcontroller??if yes ,how??? pls rply assp………

  15. Mohammad Hakim says:

    Sir,
    I wanna know how to code a micro controller from the beginning…

  16. Sharad Agarwal says:

    Because of the way the 8051 port pin circuitry is designed, to use a port pin as an output requires no initialisation. You simply write to the port. For example, SETB P1.5 will send a logic 1 to P1 bit 5. MOV P0, A will send the data in the accumulator to P0.

    To initialise a port pin as an input we must first write a logic 1 to that pin. For example, to set P3 pin 2 as an input we could execute the code: SETB P3.2

    This code is exactly the same as if you were writing a logic 1 to an output pin. Therefore, simply by examining a line of code, say, MOV P2, #0FFH, there is no way of telling if this is sending all 1s to P2 because P2 is an output port, or sending all 1s to P2 to make all of P2′s pins inputs. Only by examining what is connected to P2 would we be able to decipher this piece of code correctly. For example, if 8 LEDs are connected to P2, then this is an output port (you never read the value of an LED, you either turn it on or off by writing 1s and 0s to it) and the code MOV P2, #0FFH is turning on or off (depending on the way the LEDs are connected) the LEDs.

    If, on the other hand, 8 switches were connected to P2, then these pins must be used as inputs (because we read the value of a switch) and the code MOV P2, #0FFH is initialising each pin of P2 as an input.

  17. B0ynxtd0r says:

    can you kindly please provide/send me schematic/block diagram of a coin-op basketball arcade machine?

  18. prasad says:

    plz send me project of fastest finger

    first using microcontroller…

  19. sivaranjani.k says:

    sir,
    i want code for automatic retrive data from pc to 8051 microcontroller. data contain microsoftword format. pls send the code very soon

  20. Santosh says:

    what are the pull up resistors and why it is used?

  21. roopali says:

    thanks for the help … a trillion thanks … :)

  22. Abhishek Mathur says:

    Hey tushar thakar,

    Microprocessor is a also Known as 8085 IC . This 40 pin IC is use to perform many Arithmetical and logical Operations. This IC is use to interface different IC’s to perform different work , For Example: Providing multiple interrupts using 8259 IC. Similarly we can perform many other operation by interfacing with other IC’s. This type of IC’s are used in Computers and Personal Uses.

    Microcontroller is again an IC of family 8051 series. This is really specialized IC having all the features available with 8085 and in addition to that other features like processor core, memory, and programmable I/O peripherals. This type of IC is self sufficient, can work solely without any help from other IC’s . This Type of IC’s are used in Automobile companies, embedded systems , remote control devices.

    This type of IC is usd to perform function on it’s own , i.e. Automatic control is handled here. This IC is most advance version of microprocessor which can control other process. This IC has its own memory , I/O controls , interrupts Etc.

    First you have to know that what is microprocessor and what is microcontroller.

    Microprocessor is a multipurpose, programmable,register based electronic device which read binary instructions from memory, processes the input data as per instructions and provides output.
    Microcontroller is a device that includes microprocessor, memory and input/output devices on a single chip.

    Comparison of Microcontroller and Microprocessor

    Microcontroller
    1. Microcontroller having inbuilt RAM or ROM and inbuilt timer.
    2. Input and output ports are available.
    3. Inbuilt serial port.
    4. Separate memory to store program and data.
    5. Many functions pins on the IC.
    6. Boolean operation directly possible.
    7. It take few instructions to read and write data from external memory.

    Microprocessor
    1. Do not have inbuilt RAM or ROM and timer.
    2. Input and output ports are not available, requires extra device like 8155
    3. Do not have inbuilt serial port, requires 8250 device.
    4. Program and data are stored in same memory.
    5. Less multifunction pins on IC.
    6. Boolean operation is not possible directly.
    7. It take many instruction to read and write data from external memory.

    Microcontroller are used in automatically controlled products and devices such as automobile engine control, remote controls, toys and traffic control lights etc.

    Regards

    Abhishek Mathur

  23. mashood ssemugenyi says:

    how are you sir, this is Mashood Ssemugegenyi at Makerere University in Uganda. Am currently working on a project of using a phone to control remote electric appliances via sms ( GSM Based Electrical Control System for Smart Home Application). i request to assist me with a code that runs a micro controller for this project. and also request for some tutorials about programing micro controllers. thanks

  24. hetal patel says:

    microcontroller 8051 me jo port he use agar reset karte he to vo port as working in output why?
    why not working in input?
    sir plz reply

  25. Monis says:

    i want to study how to programm the ports
    please refer me book for this topic

  26. montu patel says:

    sir,

  27. montu patel says:

    sir,
    I am working on a atmel 2051 micro controller .i work on a c language programming.

  28. montu patel says:

    sir,
    I am working on a atmel 2051 micro controller .i work on a c language programming.
    my actual probel i will explain it will be prectically,with as output of led..
    if my input is
    P3=0xff all led blinks but isted of
    p3=0×30 ;pin3.4 & 3.5 led are blinks but
    P3=0×03 or P3=0x0c ;my led is not blink pin3.0,3.1,3.2,3.3; not working as req…

    i also use sbit for preticular glow bur pin3.0 to3.3 are not working in it also

  29. sathish says:

    in port 1 which are pheripheral devices i.e. moso miso sck

  30. montu patel says:

    it’simple 1.7 volt led

  31. montu patel says:

    sir ,

  32. montu patel says:

    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

  33. shankar says:

    For the better understanding of PORT 0 and the concept of pull up register, check this out.
    https://www.youtube.com/watch?v=t9bN-2nFNEs

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: