8051 I/O PROGRAMMING

This chapter describes the I/O port programming of the 8051 with many examples. In Section 4.1, we describe I/O access using byte-size data, and in Section 4.2, bit manipulation of the I/O ports is discussed in detail.
SECTION 4.1: 8051 I/O PROGRAMMING
In the 8051 there are a total of four ports for I/O operations. Examining Figure 4-1, note that of the 40 pins, a total of 32 pins are set aside for the four ports PO, PL P2, and P3, where each port takes 8 pins. The rest of the pins are designated as Vrt, GND, XTAL1, XTAL2. RST, EA, ALE/PROG and PSEN are discussed in Chapter 8.


Figure 4-1. 8051 Pin Diagram
I/O port pins and their functions

The four ports PO, Pi, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports upon RESET 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 the port. To use any of these ports as an input port, it must be programmed, as we will explain throughout this section. First, we describe each port.





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 lOK-ohm pull-up resistor. This is due to the fact that PO is an open drain,
unlike PI, P2, and P3, as Figure 4-2. Port 0 with Pull-Up Resistors we will soon see. Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. In any system using the 8051/52 chip, we normally connect PO to pull-up resistors. See Figure 4-2. In this way we take advantage of port 0 for both input and output. For example, the following code will continuously send out to port 0 the alternating values of 55H and AAH.


It must be noted that complementing 55H (01010101) turns it into AAH (10101010). By sending 55H and AAH to a given port continuously, we toggle all the bits of that port.
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 Is to it, and then data is received from that port and sent to P1.




Dual role of port 0
As shown in Figure 4-1, port 0 is also designated as ADO – 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. We discuss that in Chapter 14.
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 input port. The following code will continuously send out to port 1 the alternating values 55H and AAH.


Port 1 as input
If port 1 has been configured as an output port, to make it an input port again, it must programmed as such by writing 1 to all its bits. The reason for this is discussed in Appendix C.2. In the following code, port 1 is configured first as an input port by writing Is to it, then data is received from that port and saved in R7, R6, and R5.

Port 2
Port 2 occupies a total of 8 pins (pins 21 through 28). It can be used as input or output. Just like PI, port 2 does not need any pull-up resistors since it already has pull-up resistors internally. Upon reset, port 2 is configured as an input port. The following code will send out continuously to port 2 the alternating values 55H and AAH. That is, all the bits of P2 toggle continuously.









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 PI continuously.





Dual role of port 2
In many systems based on the 8051, P2 is used as simple I/O. However, in 8031-based systems, port 2 must be used along with PO to provide the 16-bit address for external memory. As shown in Figure 4-1, port 2 is also designated as A8 – A15, indicating its dual function. Since an 8051/31 is capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. While PO provides the lower 8 bits via AO – A7, it is the job of P2 to provide bits A8 -A15 of the address. In other words, when the 8051/31 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. This is discussed in detail in Chapter 14.
From the discussion so far, we conclude that in systems based on 8751, 89C51, or DS589C4xO microcontrollers, we have three ports, PO, PI, and P2, for I/O operations. This should be enough for most microcontroller applications. That leaves port 3 for interrupts as well as other signals, as we will see next.
D^.4 1 Table 4-1: Port 3 Alternate
run o
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, just as PI and P2 did not. Although port 3 is configured as an input 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 such as interrupts. Table 4-1 provides these alternate functions of P3. This information applies to both 8051 and
8031 chips.


Functions
















P3.0 and P3.1 are used for the RxD and TxD serial communications signals. See Chapter 10 to see how they are connected. Bits P3.2 and P3.3 are set aside for external interrupts, and are discussed in Chapter 11. Bits P3.4 and P3.5 are used for timers 0 and 1, and are discussed in Chapter 9 where timers are discussed. Finally, P3.6 and P3.7 are used to provide the WR and RD signals of external memories connected in 8031-based systems. Chapter 14 discusses how they are used in 8031-based systems. In systems based on the 8751, 89C51, or DS89C4xO, pins 3.6 and 3.7 are used for I/O while the rest of the pins in port 3 are normally used in the alternate function role.








Different ways of accessing the entire 8 bits
In the following code, as in many previous I/O examples, the entire 8 bits of port 1 are accessed.

The above code toggles every bit of PI continuously. We have seen a variation of the above program before. Now we can rewrite the above code in a more efficient manner by accessing the port directly without going through the accumulator. This is shown next.





We can write another variation of Table 4-2: Reset Value of Some
the above code by using a technique called onci porj-s


read-modify-write. This is shown at the end of this chapter.
Ports status upon reset
Upon reset all ports have value FFH on them as shown in Table 4-2. This makes them input ports upon reset.



–>


–>

Next post:

Previous post: