EDN Access--12.04.97 RS-232C port scans remote keypad

-December 04, 1997

December 4, 1997

RS-232C port scans remote keypad

SK Shenoy, NPOL, Kochi, India

In many embedded or control applications, you might prefer to use a small, customized keypad with your PC-based system rather than a full-fledged alphanumeric keyboard. You may also need to locate the keypad some distance from the system hardware. If you have no parallel ports available for this purpose, you could use a standard RS-232C port. With the method shown here, you need only some wire and a few passive components to connect a keypad with as many as 15 keys to the RS-232C port, at distances to 50 ft. Figure 1a shows the basic connection; Figure 1b shows the connection of the keypad switches in the matrix.

A full RS-232C port has two modem-control output lines (RTS and DTR) and four modem-status lines (CTS, DSR, CD, and RI), in addition to the RxD and TxD data lines. You can make the DTR and RTS lines active or inactive under software control by writing to the modem-control register of the UART. However, many people are unaware that you can similarly control the TxD line if the UART supports the break-generation utility. Similarly, you can sample the modem-status input line at any time by reading the UART's modem-status register. Thus, you can use these RS-232C input and output lines as a key-scanning matrix.

According to the RS-232C standard, a receiver reads any input above 3V as active and below -3V as inactive. To avoid transients from floating inputs, ORing diodes (with broken lines in Figure 1) pull the inputs to the inactive (-12V) state through 22-kiloohm resistors connected to the RTS and DTR modem-control lines (at least one of which is inactive while scanning). The pulldown resistors provide added insurance; in most cases, the circuit works perfectly without them. You can connect diodes at the key matrix crosspoints for short-circuit protection if you simultaneously press an active key and an inactive key. If your RS-232C drivers have built-in short-circuit protection, as most do, these diodes are unnecessary.

A Borland Version 2.0 C program illustrates the scanning of a 3x5 keypad connected to a PC's serial port based on an 8250 UART. Click here to download listing DI-SIG, #2128. The idea works with any PC system and UART with similar I/O lines. The lines you need are the TxD, DTR, RTS, DSR, RI, CTS, and RxD. The first three lines are outputs, and they activate cyclically (12V) one at a time. The active period is typically a few milliseconds, but this parameter depends on how fast you want to scan, the debouncing aspects, and other considerations.

Depending on what key you press, one of the input pins becomes active; the UART's modem-status register (for CTS, DSR, CD, and RI) reads it. Unlike the modem-status lines, however, the UART cannot sample the RxD line. However, depending on how long you press the key and on the overlap of the key-press time with the transmission of the Null character (12V on DTR/RTS), either a full Null character or only a fragment transfers to the RxD line. In any case, the Rx data-ready flag sets if the duration of the space (12V) on the RxD line exceeds the start-bit time. Only the data read (which is immaterial) varies, depending on the aforementioned factors.

If the duration of the space on the receive line is longer than a character time, including start and stop bits, the break-detected status line also sets. Note that, in the worst case, the Rx data-ready flag sets eight times after the output line deactivates. Therefore, to avoid ambiguity, the activation of the next output line occurs only after waiting for this delay and then checking the Rx data-ready flag. You can effect key debouncing by verifying that the same key is active over a few repeated scans before signaling the press as active. If the UART does not support the break-generation utility, you need only transmit a Null (00 hex) character with the baud rate set to obtain a space of the required duration on the TxD line. (DI #2128)

Figure 1
Some wire, a few passive components, and an RS-232C port (a) are all you need to replace a full-featured keyboard with a small keypad. The keypad switches insert a diode at the associated matrix crosspoint (b).

| EDN Access | Feedback | Table of Contents |

Copyright c 1997 EDN Magazine, EDN Access . EDN is a registered trademark of Reed Properties Inc, used under license. EDN is published by Cahners Publishing Company , a unit of Reed Elsevier Inc.

Loading comments...

Write a Comment

To comment please Log In