CH#3 - Embedded Systems - IO Programming
CH#3 - Embedded Systems - IO Programming
CHAPTER THREE
Embedded Systems - I/O Programming
Overview
In 8051, I/O operations are done using four ports and 40 pins. The following pin diagram
shows the details of the 40 pins. I/O operation port reserves 32 pins where each port has 8 pins.
The other 8 pins are designated as Vcc, GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG
(bar), and PSEN (bar).
It is a 40 Pin PDIP (Plastic Dual Inline Package)
Note − In a DIP package, you can recognize the first pin and the last pin by the cut at the
middle of the IC. The first pin is on the left of this cut mark and the last pin (i.e. the 40 th pin in
this case) is to the right of the cut mark.
I/O Ports and their Functions
The four ports P0, P1, P2, and P3, each use 8 pins, making them 8-bit ports. Upon RESET, all
the ports are configured as inputs, ready to be used as input 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 a port.
Port 0 (Pin No 32 – Pin No 39)
It has 8 pins (32 to 39). It can be used for input or output. Unlike P1, P2, and P3 ports, we
normally connect P0 to 10K-ohm pull-up resistors to use it as an input or output port being an
open drain.
1
Real Time and Embedded System CH#3 – ES - I/O Programming By: Lecturer Abebe A. (MSC in Computer Science)
It is also designated as AD0-AD7, allowing it to be used as both address and data. In case of
8031 (i.e. ROMless Chip), when we need to access the external ROM, then P0 will be used for
both Address and Data Bus. ALE (Pin no 31) indicates if P0 has address or data. When ALE =
0, it provides data D0-D7, but when ALE = 1, it has address A0-A7. In case no external
memory connection is available, P0 must be connected externally to a 10K-ohm pull-up
resistor.
It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It doesn't require
pull-up resistors because they are already connected internally. Upon reset, Port 1 is configured
as an input port. The following code can be used to send alternating values of 55H and AAH to
Port 1.
2
Real Time and Embedded System CH#3 – ES - I/O Programming By: Lecturer Abebe A. (MSC in Computer Science)
If Port 1 is configured to be used as an output port, then to use it as an input port again,
program it by writing 1 to all of its bits as in the following code.
Port 2 occupies a total of 8 pins (pins 21 through 28) and can be used for both input and output
operations. Just as P1 (Port 1), P2 also doesn't require external Pull-up resistors because they
are already connected internally. It must be used along with P0 to provide the 16-bit address
for the external memory. So it is also designated as (A0–A7), as shown in the pin diagram.
When the 8051 is connected to an external memory, it provides path for upper 8-bits of 16-bits
address, and it cannot be used as I/O. Upon reset, Port 2 is configured as an input port. The
following code can be used to send alternating values of 55H and AAH to port 2.
If Port 2 is configured to be used as an output port, then to use it as an input port again,
program it by writing 1 to all of its bits as in the following code.
3
Real Time and Embedded System CH#3 – ES - I/O Programming By: Lecturer Abebe A. (MSC in Computer Science)
4
Real Time and Embedded System CH#3 – ES - I/O Programming By: Lecturer Abebe A. (MSC in Computer Science)
RST (Pin No. 9) − It is an Input pin and active High pin. Upon applying a high pulse on
this pin, that is 1, the microcontroller will reset and terminate all activities. This process is
known as Power-On Reset. Activating a power-on reset will cause all values in the
register to be lost. It will set a program counter to all 0's. To ensure a valid input of Reset,
the high pulse must be high for a minimum of two machine cycles before it is allowed to go
low, which depends on the capacitor value and the rate at which it charges. (Machine
Cycle is the minimum amount of frequency a single instruction requires in execution).
EA or External Access (Pin No. 31) − It is an input pin. This pin is an active low pin;
upon applying a low pulse, it gets activated. In case of microcontroller (8051/52) having
on-chip ROM, the EA (bar) pin is
connected to Vcc. But in an 8031
microcontroller which does not have
an on-chip ROM, the code is
stored in an external ROM and
then fetched by the microcontroller. In
this case, we must connect the (pin
5
Real Time and Embedded System CH#3 – ES - I/O Programming By: Lecturer Abebe A. (MSC in Computer Science)
Single-Bit Instructions