The switch is probably the simplest of all input devices, and one of the most useful. Hardware interfacing is quite simple, and for CPUs that have internal pull-ups like the 8051, all that need be done is connect the switch between
the pin and ground. As can be seen from Figure 8-7, the input will be a logic one when the switch is open, and logic zero when the switch is closed.
Unfortunately switch contacts bounce when they are closed and sometimes when they are broken. This causes
the output to oscillate briefly between one and zero until the contacts stop bouncing, usually after several milli-seconds or more. As a result, the program reading the switch state must
“de-bounce” the switch operation, meaning that the switch transitions must be ignored for some time after the first transition between off and on.
Matrix Keyboard Input
The next step up in input complexity is the matrix keypad or keyboard. These switch arrays are usually organized into a number of rows and columns, like the 4-by-3 array of 12 buttons on a telephone. These matrix-connected devices can be multiplexed to reduce the number of I/O lines required to sense the keys.
If a 4-row-by-4-column keypad were implemented using separate inputs, one per switch, a total of 16 input pins would be required. Since I/O pins are almost always at a premium, this is not the best approach.
By arranging the switch contacts to short the row and column lines corre-sponding to their position in the matrix, the number of lines can be reduced.
By selecting one column at a time and looking for activity on any of the row inputs, the program can determine which key has been depressed. One row output can be driven low at a time, and the column input bits are read to see if any of them are low. A low column input indicates that the switch belonging to the corresponding row and column is closed. Multiplexing allows the rows and columns to be scanned for activity under software control. In the case of sixteen keys, only four columns and four rows would be required, or a total of eight I/O pins, compared to 16 for the simple one input per switch approach.
For the processors like the 8051 with built-in pull-ups, the only thing that is required is the key switch matrix. A key switch matrix like this can be imple-mented very inexpensively by using a standard matrix keypad, or by attaching steel switch domes to a PC board with row and column contacts, encapsulated
“1”
Figure 8-7: Simple switch used as input.
under an adhesive plastic sheet. An adhesive backed label can be printed using a standard printer and covered with another layer of clear plastic.
The resulting keyboard will have a custom graphic legend and be rela-tively impervious to contamination.
The cost of this type of “click dome membrane keypad” is also very low, whether you make it yourself or buy one from a manufacturer that special-izes in these keyboards. Figure 8-8 shows the schematic of a multi-plexed keyboard matrix.
Even fewer I/O lines can be used if the rows are decoded using a 2-to-4 line decoder, and the columns are
encoded using a 4-to-2 line priority encoder. This approach will require only four I/O pins. Using a 3-to-8 line decoder and an 8-to-3 line priority encoder, it is possible to scan 64 keys using only six I/O lines.
A multiplexed keyboard can also be scanned using a dedicated matrix keyboard IC, such as the 74HC922, which provides hardware controlled scanning auto-matically as well as a separate interrupt output. This device can be mapped into the external memory or I/O address space of a processor.
Matrix Display Devices
Simple output indicators, such as the simple LED indicators presented previ-ously, can be very useful, but similar problems arise when using multiple LEDs each driven by a single I/O pin. Once again, the LEDs can be arranged in a matrix, and driven by multiplexing rows and columns of devices. As long as the LEDs are scanned quickly enough, at least 15 or 20 times per second, the LEDs appear to be on continuously. This works because of a perceptive charac-teristic of human vision, known as persistence of vision (POV). Many devices, including television and computer CRT displays depend on this characteristic of human vision. Many LED display alarm clocks use multiplexing, as do
+5V +5V +5V +5V
Row 1 Output
Row 2 Output
Row 3 Output
Row 4 Output
Column 1 Input
Column 2 Input
Column 3 Input
Column 4 Input
Figure 8-8: Matrix keypad multiplexing.
Switches short row and column together.
many other types of displays, such as most LCDs. In each case, the flicker of the display is normally not apparent to the observer. You can see the strobe-like effect by waving your fingers quickly in front of a multiplexed display.
An array of LEDs or seven segment numeric LED displays can be illuminated this way, using many fewer I/O pins than would be required by using one pin per LED, as shown in Figure 8-9.
The display is scanned, or refreshed, by activating the column, and then the row bits that correspond to the LEDs in that column which should be lit. The display is left on for a short period, then switched to the next column and row, and so on. As long as the display is refreshed frequently enough, there is no visible flicker.
Another type of display is the LCD. The simplest of these is just a glass panel with extremely thin metalized connections to the segments. These are rather complex to drive directly from most microcontrollers, but there are two ways that they can be connected without much effort. The simplest, but more expensive approach, is to use an intelligent LCD module complete with the drive electronics. Most of these devices use a standard controller, and can be driven using either a 4-bit bus or an 8-bit bus. Serial input devices are also available, which can be driven directly from a standard RS-232 serial port. They are available in text-only display versions, ranging in size from one row of 16
+5 V
Row (Segment) Drivers
LEDs Digit (Column) Drivers
Figure 8-9: Multiplexed LED display.
characters to four rows of 40 characters. Graphic display versions of these modules are also available, allowing flexible text and graphic display formats.
Another method of driving small glass displays directly is through special LCD display driver chips, which are designed to drive a relatively simple display (such as one containing simple 7-segment numeric digits, for example).
These peripherals are available from several vendors, and the LCD display peripheral driver hardware is even incorporated in some microcontrollers.
Many other types of I/O can be added externally using the processor’s bus interface. The 82C55 chip is a commonly used parallel interface with two 8-bit ports and two 4-bit ports which can be programmed as inputs or outputs.
Connecting an 82C55 to the 8051 bus using memory mapping is an example of a program controlled I/O interface.