13
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API what-when-how In Depth Tutorials and Information 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 V rt , GND, XTAL1, XTAL2. RST, EA, ALE/PROG and PSEN are discussed in Chapter 8. SUBSCRIBE

What When How Com 8051 Microcontroller 8051 Io Programming

Embed Size (px)

Citation preview

Page 1: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

what-when-howIn Depth Tutorials and Information

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 PROGRAMMINGIn 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 setaside 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.

S UB S CRIB E

Page 2: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

Figure 4-1. 8051 Pin DiagramI/O port pins and their functionsThe 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 tobe 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 theport. To use any of these ports as an input port, it must be programmed, as we will explain throughout this section. First, we describeeach port.

Page 3: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

Port 0Port 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 thesame 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.

PCB Assemblyscreamingcircuits.com

One Quote For All - Instant Online. Quick TurnPCB Assembly Manufacture

Download Update

Power MOSFETs

Page 4: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

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 sendout 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 agiven port continuously, we toggle all the bits of that port.Port 0 as inputWith resistors connected to port 0, in order to make it an input, the port must be programmed by writing 1 to all thebits. In the following code, port 0 is configured first as an input port by writing Is to it, and then data is received fromthat port and sent to P1.

Dual role of port 0As shown in Figure 4-1, port 0 is also designated as ADO – AD7, allowing it to be used for both address and data. Whenconnecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and

Page 5: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

connecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051 multiplexes address anddata through port 0 to save pins. We discuss that in Chapter 14.Port 1Port 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 notneed any pull-up resistors since it already has pull-up resistors internally. Upon reset, port 1 is configured as an input port. Thefollowing code will continuously send out to port 1 the alternating values 55H and AAH.

Port 1 as inputIf 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 allits bits. The reason for this is discussed in Appendix C.2. In the following code, port 1 is configured first as an input port bywriting Is to it, then data is received from that port and saved in R7, R6, and R5.

Port 2Port 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 anypull-up resistors since it already has pull-up resistors internally. Upon reset, port 2 is configured as an input port. The following

Page 6: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

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 inputTo make port 2 an input, it must programmed as such by writing 1 to all its bits. In the following code, port 2 isconfigured first as an input port by writing 1 s to it. Then data is received from that port and is sent to PIcontinuously.

Page 7: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

Dual role of port 2In 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 toprovide the 16-bit address for external memory. As shown in Figure 4-1, port 2 is also designated as A8 – A15, indicating its dualfunction. Since an 8051/31 is capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. WhilePO 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 isconnected 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 indetail 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 wellas other signals, as we will see next.D^.4 1 Table 4-1: Port 3 Alternaterun oPort 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 PIand 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 theadditional 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 and8031 chips.

Functions

Page 8: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

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

Page 10: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

Different ways of accessing the entire 8 bitsIn 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. Nowwe can rewrite the above code in a more efficient manner by accessing the port directly without going through theaccumulator. This is shown next.

Page 11: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

We can write another variation of Table 4-2: Reset Value of Somethe 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 resetUpon reset all ports have value FFH on them as shown in Table 4-2. This makes them input ports upon reset.

Page 12: What When How Com 8051 Microcontroller 8051 Io Programming

pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

R E L A T E D L I N K S

8051 Microcontroller

8051 MICROCONTROLLERS

MICROCONTROLLERS AND EMBEDDED PROCESSORS

OVERVIEW OF THE 8051 FAMILY

8051 ASSEMBLY LANGUAGE PROGRAMMING

INSIDE THE 8051

: : SE A R C H WWH : :

–>

–>

N E X T P O S T : 8051 I/O BIT MANIPULATION PROGRAMMING

P R E V I O U S P O S T : I/O PORT PROGRAMMING

0

► Programming Code► Circuit Diagram► Input Output Interface

AdChoices

► Programming 8051► In Circuit Programming► Interface Programming

AdChoices