Port 1: Port 1 occupies a total of 8 pins (pins 1 through
. 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.
Port 1: Port 1 occupies a total of 8 pins (pins 1 through
. 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.
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.