ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 2092
Скачиваний: 0
Inputs and Outputs
If you wish, you can wire U14’s inputs and U12’s outputs to headers for easy access when you want to connect clip-on jumpers, probe leads, or ribbon cables to the inputs and outputs.
Basic Tests
What can you do with these new inputs and outputs? First, some simple tests. You read and write to the ports exactly as you read and write to external memory. This BASIC-52 statement will display the value of the byte at U12’s inputs:
PRINT XBY(0E000h)
Or, you may prefer a hexadecimal display:
PH0. XBY(0E000h)
To verify that you’re reading all bits correctly, jumper each input in turn to ground and +5V and read the results. Open inputs are undefined and may read as high or low.
To test U14’s outputs, use this BASIC-52 statement:
XBY(0E400h)=xx
where xx is the value you want to write to the chip. To set all bits, write 255 or 0FFh. To clear them, write 0. To verify that all bits are responding, connect a logic probe, voltmeter, or oscilloscope to each of the outputs of U14 in turn and verify that the bits respond correctly to your commands.
Input Examples
Figure 6-3 shows some inputs that you can interface to U12.
(A)TTL or CMOS logic outputs powered at 5 volts can directly drive U12’s inputs.
(B)To translate lower voltages to 5-volt logic, use a 74HCT03 or similar open-drain NAND gate with a pullup resistor to 5 volts.
(C)To translate higher voltages to 5-volt logic, use a 74HC4050 buffer or 74HC4049 inverter, powered at 5 volts. These chips can safely accept inputs up to 15 volts.
(D)You can also detect the state (open or closed) of a toggle or slide switch at a port pin.
(E)An optocoupler is another way to interface different voltage levels, and it also electrically isolates the input from the microcontroller circuit.
The Microcontroller Idea Book |
93 |
Chapter 6
Figure 6-3. Input interfaces for the 74LS244 buffer.
94 |
The Microcontroller Idea Book |
Inputs and Outputs
Figure 6-4. Output interfaces to the 73LS374 octal latch.
The Microcontroller Idea Book |
95 |
Chapter 6
Output Examples
Figure 6-4 shows some basic outputs that you can connect to U14:
(A)Outputs can directly drive any TTL or HCTMOS logic input powered at 5 volts.
(B)To interface to 5-volt HCMOS or 4000-series CMOS devices, add a pull-up resistor to ensure that high outputs are greater than 3.5 volts.
(C)To translate an output to a lower voltage, use a 74HC4050 buffer or 74HC4049 inverter powered at the lower voltage.
(D)To translate an output to a higher voltage, use a 74LS26 NAND or similar high-voltage, open-collector gate, with a pullup resistor to the higher voltage.
(E)For high-current outputs, you can use Texas Instruments’ 7545X series of peripheral drivers. The 75452 NAND gate can sink up to 300 milliamperes at 0.5V.
(F)U14 can directly drive an LED. Since the 74LS374 can sink more current than it can source, connect the LED so that it turns on when the output is low. With a 220-ohm current-limiting resistor, the LED’s forward current is around 13 milliamperes.
(G)As with inputs, an optocoupler is another way to interface different voltages, and to electrically isolate an output from the microcontroller circuit.
Reading and Controlling Individual Bits
Unlike assembly language for the 8052, BASIC-52 has no logical operators for setting and clearing individual bits in a byte. But you can use the .AND. and .OR. operators to accomplish the same thing.
Listing |
6-1. Displays the value of each bit at input port E000h. |
|
10 |
A=XBY(0E000H) |
|
20 |
PRINT “Bit 0 = ”,(A.AND.1) |
|
30 |
PRINT “Bit 1 = ”,(A.AND.2)/2 |
|
40 |
PRINT “Bit 2 = ”,(A.AND.4)/4 |
|
50 |
PRINT “Bit 3 = ”,(A.AND.8)/8 |
|
60 |
PRINT “Bit 4 = ”,(A.AND.10H)/10H |
|
70 |
PRINT “Bit 5 |
= ”,(A.AND.20H)/20H |
80 |
PRINT “Bit 6 |
= ”,(A.AND.40H)/40H |
90 |
PRINT “Bit 7 |
= ”,(A.AND.80H)/80H |
100 |
END |
|
96 |
The Microcontroller Idea Book |
Inputs and Outputs
Listing 6-2. Sets or clears a bit at output port E400h.
5 |
REM variable A contains the last data written to 0E400h |
10 |
INPUT “Enter a bit to set or clear (0-7) :”,X |
20 |
INPUT “Enter 1 to set, 0 to clear :”,Y |
30 |
IF Y=1 THEN A=A.OR.2**X |
40 |
IF Y=0 THEN A=A.AND.0FFH-2**X |
50 |
XBY(0E400H)=A |
60 |
END |
Listing 6-1 is a program that reads the buffer and displays the value of each bit. The program is similar to Listing 3-1, which displays the bit values at Port 1.
Line 20 finds the logic state of bit 0 by logically ANDing the byte with a mask byte that is all 0’s except for bit 0: 00000001. The result is 1 if bit 0 is 1, and 0 if bit 0 is 0. Lines 30-90 are similar, except that each time a different bit in the mask byte is 1. In each case, the program divides the result by 2 raised to the power of the bit number. Since (2**0 equals 1, line 20 leaves out this step.) Each PRINT statement shows the logic state of one of the bits.
At the output port, if you want to change just one bit in the byte, you have to know the current value of the byte. The simplest way to accomplish this is to save the last value you wrote. Or, you could wire an input buffer at the same address, with each input bit connecting to the corresponding output bit, and read the input when you need to know the current value.
Listing 6-2 prompts you for a bit to set and clear, then does so. It assumes that you’ve stored the current value of E400h in variable A.
To set a bit, line 30 logically ORs the current value with a mask byte that is all 0s except for the bit to be set. For example, to set bit 4, the mask byte is 0001 0000, or 10h, which leaves bits 0-3 and 5-7 unchanged, but forces bit 4 to be 1.
To clear a bit, line 40 logically ANDs the byte with a mask byte that is all 1s except for the bit or bits to be cleared. For example, to clear bit 3, the mask byte is 1111 0111, or F7h. The result is that bits 0-2 and 4-7 are unchanged, but bit 3 must be 0.
Line 50 writes the new byte to the port.
The Microcontroller Idea Book |
97 |
Chapter 6
Table 6-1. Popular peripheral interface chips.
8253/4Programmable Interval Timer
Three independent 16-bit counters, 6 modes, up to 10 Mhz
8255 Programmable Peripheral Interface
Three 8-bit I/O pins, 3 modes, direct bit set/reset ability
8256 Multifunction Microprocessor Support Controller
Asynchronous serial interface, baud rate generator, five 8-bit timer/counters, two 8-bit I/O ports, 8-level interrupt controller, programmable system clock
8259 Programmable Interrupt Controller
Eight-level priority controller, programmable interrupt modes
8279 Programmable Keyboard/Display Interface
Scanned interfaces to 64-contact key matrix and 16-character display.
The 8255 Programmable Peripheral Interface
In addition to the inputs and outputs provided by U12 and U14, there are specialized peripheral-interface chips that you can add to your system. Table 6-1 lists several examples.
One of the most popular of these is the 8255 programmable peripheral interface, or PPI. Figure 6-5 shows the pinout, and Table 6-2 shows the pin functions. The chip adds 24 bits of I/O, plus the option to use special control and handshaking signals to communicate with peripherals.
Intel originally introduced the 8255 as a peripheral for its 8085 microprocessor, but it remains
a popular chip for use with 8052s and other computer chips. Manufacturers of compatible chips include AMD, OKI, Toshiba, and NEC, which calls its chip the PD71055.
If you use the 8255, you’ll want a copy of its data sheet, which more fully explains its abilities and configuration options.
8255 Variants
You have a choice of the original NMOS 8255 or the CMOS 82C55. The CMOS version usually costs a little more, but has some advantages. First, it has lower power consumption, with supply currents of 10 milliamperes (10 microamperes in standby mode with CS=high), compared to 120 milliamperes for the 8255.
98 |
The Microcontroller Idea Book |
Inputs and Outputs
The 82C55 also has CMOS-compatible outputs, which means that they can drive either LSTTL or CMOS inputs. When driving CMOS inputs, the NMOS 8255’s outputs should have pull-up resistors to ensure that high outputs are at least 3.5 volts.
A third advantage to the 82C55 is greater current-sourcing ability, which can be important if you want to directly drive a transistor or source more than a fraction of a milliampere. Intel’s 82C55 can source 2.5 milliamperes at 3 volts, compared to just 0.4 milliamperes at 2.4 volts for the 8255. However, NEC’s CMOS 71055 has the same current-sourcing ability as the NMOS 8255, so it depends on the manufacturer. All can sink 2.5 milliamperes at 0.45 volts. The 74LS374 latch (U14) has greater output drive ability than any of the 8255s.
Speed Ratings
The 8255 is also available with different speed ratings, including 3 Mhz and 5 Mhz. The 5-Mhz part is sometimes called the 8255-5. From the ratings, it may seem that the 8255 is too slow to interface to a 12-Mhz 8052-BASIC. But what does the speed rating actually refer to? Since the 8255 was developed for the 8085, I suspect that it refers to the maximum
Figure 6-5. Pinout of the 8255 Programmable Peripheral Interface.
The Microcontroller Idea Book |
99 |
Chapter 6
Table |
6-2. Pin functions for the 8255 Programmable Peripheral Interface. |
||||||||
Pin |
Symbol |
Input/ |
Function |
||||||
Output |
|||||||||
1 |
PA3 |
I/O |
Port A, bit 3 |
||||||
2 |
PA2 |
I/O |
Port A, bit 2 |
||||||
3 |
PA1 |
I/O |
Port A, bit 1 |
||||||
4 |
PA0 |
I/O |
Port A, bit 0 |
||||||
5 |
I |
Read |
|||||||
RD |
|||||||||
6 |
I |
Chip select |
|||||||
CS |
|||||||||
7 |
GND |
I |
Signal ground |
||||||
8 |
A1 |
I |
Port select 1 |
||||||
9 |
A0 |
I |
Port select 0 |
||||||
10 |
PC7 |
I/O |
Port C, bit 7 |
||||||
OBFA |
O |
Port A output buffer full |
|||||||
11 |
PC6 |
I/O |
Port C, bit 6 |
||||||
ACKA |
I |
Port A acknowledge |
|||||||
12 |
PC5 |
I/O |
Port C, bit 5 |
||||||
IBFA |
O |
Port A input buffer full |
|||||||
13 |
PC4 |
I/O |
Port C, bit 4 |
||||||
STBA |
I |
Port A strobe |
|||||||
14 |
PC0 |
I/O |
Port C, bit 0 |
||||||
INTRB |
O |
Port B interrupt request |
|||||||
15 |
PC1 |
I/O |
Port C, bit 1 |
||||||
IBFB |
O |
Port B input buffer full |
|||||||
OBFB |
O |
Port B output buffer full |
|||||||
16 |
PC2 |
I/O |
Port C, bit 2 |
||||||
STBB |
I |
Port B strobe |
|||||||
ACKB |
I |
Port B acknowledge |
|||||||
17 |
PC3 |
I/O |
Port C, bit 3 |
||||||
INTRA |
O |
Port A interrupt request |
|||||||
18 |
PB0 |
I/O |
Port B, bit 0 |
||||||
19 |
PB1 |
I/O |
Port B, bit 1 |
||||||
20 |
PB2 |
I/O |
Port B, bit 2 |
||||||
100 |
The Microcontroller Idea Book |
Inputs and Outputs
Pin |
Symbol |
Input/ |
Function |
|
Output |
||||
21 |
PB3 |
I/O |
Port B, bit 3 |
|
22 |
PB4 |
I/O |
Port B, bit 4 |
|
23 |
PB5 |
I/O |
Port B, bit 5 |
|
24 |
PB6 |
I/O |
Port B, bit 6 |
|
25 |
PB7 |
I/O |
Port B, bit 7 |
|
26 |
Vcc |
I |
Power supply (+5V) |
|
27 |
D7 |
I/O |
Data bit 7 |
|
28 |
D6 |
I/O |
Data bit 6 |
|
29 |
D5 |
I/O |
Data bit 5 |
|
30 |
D4 |
I/O |
Data bit 4 |
|
31 |
D3 |
I/O |
Data bit 3 |
|
32 |
D2 |
I/O |
Data bit 2 |
|
33 |
D1 |
I/O |
Data bit 1 |
|
34 |
D0 |
I/O |
Data bit 0 |
|
35 |
RESET |
I |
Reset ports to input; clear control register |
|
36 |
I |
Write |
||
WR |
||||
37 |
PA7 |
I/O |
Port A, bit 7 |
|
38 |
PA6 |
I/O |
Port A, bit 6 |
|
39 |
PA5 |
I/O |
Port A, bit 5 |
|
40 |
PA4 |
I/O |
Port A, bit 4 |
|
The Microcontroller Idea Book |
101 |