PROGRAMMING THE 8255

As demonstrated in Chapter 14, in the process of connecting the 8031/51 to external memory, two ports, PO and P2, are lost. In this chapter we show how to expand the I/O ports of the 8031/51 by connecting it to an 8255 chip. In Section 15.1, the connection of the 8031/51 to an 8255 is shown. In Section 15.2, the 8255 is programmed as a simple I/O port for connection with devices such as LCDs, stepper motors, and ADCs. In Section 15.3, the 8051 C programming of the 8255 is discussed.
SECTION 15.1: PROGRAMMING THE 8255
In this section we study the 8255 chip, one of the most widely used I/O chips. We first describe its features and then show the connection between the 8031/51 and 8255 chips.



8255 features
The 8255 is a 40-pin DIP chip (see Figure 15-1). It has three separately accessible ports. The ports are each 8-bit, and are named A, B, and C. The individual ports of the 8255 can be programmed to be input or output, and can be changed dynamically. In addition, 8255 ports, have handshaking capability, thereby allowing interface with devices that also have handshaking signals, such as printers. The handshaking capability of the 8255 is not discussed since it is no longer used.
PAO – PA7
The 8-bit port A can be programmed as all input, or as all output, or all bits as bidirectional input/output.
PBO – PB7
The 8-bit port B can be programmed as all input or as all output. Port B cannot be used as a bidirectional port.
PCO-PC7

This 8-bit port C can be all input or all output. It can also be split into two parts, CU (upper bits PC4 – PC7) and CL (lower bits PCO – PC3). Each can be used for input or output.






RD and WR
These two active-low control signals are inputs to the 8255. The RD and WR signals from the 8031/51 are connected to these inputs.
DO – D7 data pin
The data pins of the 8255 are connected to the data pins of the microcontroller allowing it to send data back and forth between the controller and the 8255 chip.
RESET


This is an active-high signal input into the 8255 used to clear the control register. When RESET is activated, all ports are initialized as input ports. In many designs this pin is connected to the RESET output of the system bus or grounded to make it inactive. Like all 1C input pins, it should not be left unconnected.
AO,A1, andCS
While CS (chip select) selects the entire chip, it is AO and Al that select specific ports. These three pins are used to access ports A, B, C, or the control register as shown in Table 15-1. Note that CS is active-low.
Mode selection of the 8255
While ports A, B, and C are used to input or output data, it is the control register that must be programmed to select the operation mode of the three ports. The ports of the 8255 can be programmed in any of the following modes.
  1. Mode 0, simple I/O mode. In this mode, any of the ports A, B, CL, and CU can
    be programmed as input or output. In this mode, all bits are out or all are in. In
    other words, there is no such thing as single-bit control as we have seen in
    PO – P3 of the 8051. Since the vast majority of applications involving the 8255
    use this simple I/O mode, we will concentrate on this mode in this chapter.
  2. Mode 1. In this mode, ports A and B can be used as input or output ports with
    handshaking capabilities. Handshaking signals are provided by the bits of port
    C.
  3. Mode 2. In this mode, port A can be used as a bidirectional I/O port with hand
    shaking capabilities whose signals are provided by port C. Port B can be used
    either in simple I/O mode or handshaking mode 1.
  4. BSR (bit set/reset) mode. In this mode, only the individual bits of port C can
    be programmed.
    Figure 15-3 shows the control word for mode selection of the 8255.



Figure 15-3. 8255 Control Word Format (I/O Mode)
(Reprinted by permission of Intel Corporation, Copyright Intel Corp.. 1983)
Simple I/O programming
Intel calls mode 0 the basic input/output mode. The more commonly used term is simple I/O. In this mode, any of ports A, B, or C can be programmed as input or output. It must be noted that in this mode, a given port cannot be both an input and output port at the same time.
Example 15-1
Find the control word of the 8255 for the following configurations:
  1. All the ports of A, B, and C are output ports (mode 0).
  2. PA = in, PB = out, PCL = out, and PCH = out.
    Solution:
From Figure 15-3 we have:
(a) 1000 0000 = 80H (b) 1001 0000 = 90H
Connecting the 8031/51 to an 8255
The 8255 chip is programmed in any of the 4 modes mentioned earlier by sending a byte (Intel calls it a control word) to the control register of the 8255. We must first find the port addresses assigned to each of ports A, B, C, and the control register. This is called mapping the I/O port.
As can be seen from Figure 15-4, the 8255 is connected to an 8031/51 as if it is RAM memory. Notice the use of RD and WR signals. This method of connecting an I/O chip to a CPU is called memory-mapped I/O, since it is mapped into memory space. In other words, we use memory space to access I/O devices. For this reason we use instructions such as MOVX to access the 8255. In Chapter 14 we used MOVX to access RAM and ROM. For an 8255 connected to the 8031/51 we must also use the MOVX instruction to communicate with it. This is shown in Example 15-2.


Example 15-2
For Figure 15-4,
  1. Find the I/O port addresses assigned to ports A, B, C, and the control register.
  2. Program the 8255 for ports A, B, and C to be output ports.
  3. Write a program to send 55H and AAH to all ports continuously.


Figure 15-4. 8051 Connection to the 8255 for Example 15-2
Solution:




Example 15-3
For Figure 15-5,
  1. Find the I/O port addresses assigned to ports A, B, C, and the control register.
  2. Find the control byte for PA = in, PB = out, PC = out.
  3. Write a program to get data from PA and send it to both ports B and C.


Figure 15-5. 8051 Connection to the 8255 for Example 15-3
Solution:


For the program in Example 15-3, it is recommended that you use the EQU directive for port addresses as shown next.


Notice in both Examples 15-2 and 15-3 that we used the DPTR register since the base address assigned to 8255 was 16-bit. If the base address for the 8255 is 8-bit, we can use the instructions “MOVX A, @RO”and”MOVX @RO,A” where RO (or Rl) holds the 8-bit port address of the port. See Example 15-4. Notice in Example 15-4 that we used a simple logic gate to do the address decoding for the 8255. For multiple 8255s in a system, we can use a 74LS138, as shown in Example 15-5.
Address aliases
In Examples 15-4 and 15-5 we decode the AO – A7 address bits; however, in Examples 15-3 and 15-2 we decoded only a portion of the upper addresses of A8 – A15. This partial address decoding leads to what is called address aliases. In other words, the same physical port has different addresses; thus, the same port is known by different names. In Examples 15-2 and 15-3 we could have changed all x’s to various combinations of Is and Os to come up with different addresses, yet they would all refer to the same physical port. In your hardware reference documentation, make sure that – all address aliases are documented, so that the user knows what addresses are available if he/she wants to expand the system.


Example 15-4
For Figure 15-6,




  1. Find the I/O port addresses assigned to ports A, B, C, and the control register.
  2. Find the control byte for PA = out, PB = in, PCO – PCS = in, and PC4 – PC7 = out.
    1. Write a program to get data from PB and send it to PA. In addition, data from PCL
      is sent out to the PCU.
Figure 15-6. 8051 Connection to the 8255 for Example 15-4


Example 15-5






8031 system with 8255

In the 8031-based system where external program ROM is an absolute must, the use of an 8255 is most welcome. This is due to the fact that in the process of connecting the 8031 to external program ROM, we lose the two ports PO and P2, leaving only PI. Therefore, connecting
an 8255 is the
best way to gain some extra ports. This is shown in Figure 15-8.




Figure 15-8. 8031 Connection to External Program ROM and the 8255

Next post:

Previous post: