Search Your Query

Custom Search

Monday, 30 May 2011

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

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...