I/O Programming
Dr. Basab Chatterjee, Dept. of AEIE, Academy of Technology
8051 Pin Diagram
The four 8-bit I/O ports
P0, P1, P2 andP3 each uses
8 pins
All the ports upon RESET
are configured as output,
ready to be used as output
ports
When the first 0 is written
to a port, it becomes an
output
To reconfigure it as an
input, a 1 must be sent to
the port
To use any of these ports
as an input port, it must be
programmed.
Port 0
Port 0 can be used for input or
output, 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
Port 0 as Input and Output
The following code will Port 0 is configured first as an
continuously send out to port 0 input port by writing 1s to it,
the alternating value 55H and and then data is received from
AAH that port and sent to P1
BACK: MOV A,#55H MOV A,#0FFH ;A=FF hex
MOV P0,A MOV P0,A ;make P0 an i/p port
ACALL DELAY ;by writing it all 1s
MOV A,#0AAH BACK: MOV A,P0 ;get data from P0
MOV P0,A MOV P1,A ;send it to port 1
ACALL DELAY SJMP BACK ;keep doing it
SJMP BACK
Port 1 as Input and Output
In contrast to port 0, this port To make port 1 an input port, it
does not need any pull-up must be programmed as such
resistors since it already has by writing 1 to all its bits
pull-up resistors internally Port 1 is configured first as an
Upon reset, port 1 is configured input port by writing 1s to it,
as an output port then data is received from that
The following code will port and saved in R7 and R5
continuously send out to port 0
the alternating value 55H and MOV A,#0FFH ;A=FF hex
AAH MOV P1,A ;make P1 an input port
;by writing it all 1s
MOV A,#55H
MOV A,P1 ;get data from P1
BACK: MOV P1,A MOV R7,A ;save it to in reg R7
ACALL DELAY ACALL DELAY ;wait
CPL A MOV A,P1 ;another data from P1
SJMP BACK MOV R5,A ;save it to in reg R5
Port 2 as Input and Output
Port 2 can be used as input or To make port 2 an input port, it
output must be programmed as such by
Just like P1, port 2 does not writing 1 to all its bits
need any pull-up resistors since In the following example, Port
it already has pull-up resistors 2 is configured first as an input
internally port by writing 1s to it, and
Upon reset, port 2 is configured then data is received from that
as an output port port and sent to P1
MOV A,#0FFH ;A=FF hex
MOV A,#55H MOV P2,A ;make P2 an i/p port
BACK: MOV P2,A ;by writing it all 1s
ACALL DELAY BACK: MOV A,P2 ;get data from P2
CPL A MOV P1,A ;send it to port 1
SJMP BACK SJMP BACK ;keep doing it
Port 3
Port 3 does not
need any pull-up
resistors
Port 3 is
configured as an
output port upon
reset, this is not
the way it is most
commonly used
Port 3 has the
additional
function of
providing some
extremely
important signals
Different ways of accessing 8 bits of I/O Ports
The entire 8 bits of Port 1 are Rewrite the code in a more
accessed (The following code efficient manner by accessing
will continuously send out to the port directly without going
port 0 the alternating value through the accumulator
55H and AAH) BACK: MOV P1,#55H
BACK: MOV A,#55H ACALL DELAY
MOV P1,A MOV P1,#0AAH
ACALL DELAY ACALL DELAY
MOV A,#0AAH SJMP BACK
MOV P1,A Another way of doing the same
ACALL DELAY thing
SJMP BACK MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
I/O Ports and bit addressability
Sometimes we need to access only 1 or 2 bits of the port
BACK: CPL P1.2 ;complement P1.2
ACALL DELAY
SJMP BACK
;another variation of the above program
AGAIN: SETB P1.2 ;set only P1.2
ACALL DELAY
CLR P1.2 ;clear only P1.2
ACALL DELAY Instructions that are used for signal-
SJMP AGAIN bit operations are as following