Introduction. This application note covers the use of matrix-encoded keypads with PIC microcontrollers. It presents an example program in Parallax assembly language for reading a 4 x 4 keypad. Background. In order to use as few input/output (I/O) pins as possible, most keypads of eight or more switches are wired in a matrix arrangement. Instead of interfacingeach set of contacts to an I/O pin, switches are wired to common row and column connections, as shown in the figure. For a given number of switches, this method can save quite a few I/O pins:
No. of Switches 8 12 16 20 Matrix (rows x columns) 2x4 3x4 4x4 5x4 I/O Pins Required 6 7 8 9
The disadvantage of matrix encoding is that it requires some additional programming. The listing shows aprogram that reads a 4 x 4 keypad using an 8-bit I/O port and presents the number of the key pressed on a 4-bit port. How it works. Code at the beginning of the program sets the lower four bits of port RB (connected to the columns of the keypad) to output, and the upper four bits (rows) to input. It also sets port RA, which will drive LEDs indicating the binary number of the key pressed, to output. Theroutine scankeys does the actual work of reading the keypad. It performs the following basic steps: • Assert a “1” on the current column. • Does the “1” appear on the current row? > No: increment key and try the next row. > Yes: exit the subroutine. • Try the next column. If the first key, key 0, is pressed, the routine exits with a 0 in the variable key, because it ends before the first incrementinstruction. If no key is pressed, the variable key is incremented 16 (010h) times. Therefore, this
PIC16Cxx Applications Handbook 1.1 • Parallax, Inc. • (916) 624-8333 • Page 85
Note 1: Interfacing a Matrix Keypad
number serves as a flag to the calling program that no key has been pressed. On the issue of switch debouncing: After a switch is pressed, it may take 20 milliseconds or more forit to settle into its new state. That means that one key press can register as many repeated presses. The easiest way to defeat this problem is to read the switches, and then wait a while before reading them again. That’s the purpose of the :delay loop in the main program.
470 470 470
1 18 LED LED LED LED
You may want to add 1k series resistors in each column (RB0- RB3). This prevents direct shorting if two or more buttons are pressed at the same time.
3 4 5 6 7 8 9
RA3 RTCC MCLR
14 13 12
PIC OSC2 16C54 Vss RC/P Vdd
RB0 RB1 RB2 RB3 RB7 RB6
10k 10k 10k 10k
Modifications. In circumstances where electrical noise might be a problem, Microchip’s data sheet on the PICindicates that it might be wise to move the port I/O assignments to the beginning of scankeys. The reason is that electrostatic discharge (ESD) from the user’s fingertips, or presumably any other strong electrical noise, could corrupt an I/O control register and switch an input pin to output. This would prevent the routine from reading one or more rows of the keypad.
Page 86 • PIC16CxxApplications Handbook 1.1 • Parallax, Inc. • (916) 624-8333
Note 1: Interfacing a Matrix Keypad
A milder zap could conceivably cause a false input to the keypad. Some routines check for this condition by comparing two or more consecutive readings of the keys. Unless several readings match, no data is returned to the main program. Program listing. This program may be downloaded from the Parallax BBS asKEYPAD.SRC. You can reach the BBS at (916) 624-7101.
; PROGRAM: KEYPAD ; This program demonstrates a simple method for scanning a matrix-encoded ; keypad. The 4 columns of the pad are connected to RB.0 through RB.3; 4 rows to ; RB .4 through RB .7. The scanning subroutine returns the code of key pressed ; (0h—0Fh) in the variable key. If no switch is pressed, the out-of-range value 10h is ;...