Lab 18: Matrix keypad interfacing
Matrix keypads are very common input devices in embedded systems. They have simple architecture and are easy to interface. One good thing about them is that they allow you to interface a large number of input keys to a microcontroller with minimum usage of I/O resources. This tutorial describes two different approaches of reading input data from a 4×4 (16 keys) matrix keypad interfaced to a PIC microcontroller. The pressed key information is displayed on a character LCD. The microcontroller used in this experiment is PIC16F1827.
Matrix keypads are simply an extension to the simple tact switch inputs. They consists of keys interconnected in the shape of a matrix. Each key is a simple mechanical switch located at the crossing between the matrix rows and columns. When a key is pressed, its row and column form an electrical contact. The rows and columns can be connected to the pins of microcontroller ports. The big advantage of using a matrix keypad is that it allows to interface a large number of keys with a relatively small number of microcontroller pins. For example, a 16-key keypad requires only 8 (instead of 16, if interfaced individually) I/O pins of the microcontroller if organized into a 4 rows and 4 columns matrix. The circuit diagram for this experimental tutorial is shown below. It interconnects a 4×4 matrix keypad to PORTB of the PIC16F1827 microcontroller. The microcontroller runs at 500 KHz using the internal clock source. The PORTA pins drive a character LCD that displays the pressed key information.
Now lets talk about the different approaches of keypad scanning, which is required to identify which key has been pressed.
Sequential exploration of rows: In this method, the I/O pins connecting the rows are all configured as output and those connecting the columns are defined as input, or vice-versa. The column lines, which are inputs to the microcontroller, are pulled-high using the internal pull-up resistors. Therefore, the default input to these lines is 1. The state for the matrix keypad can be explored by turning the rows low sequentially, one at a time, and reading the columns. For example, set the first row to 0 and read all the columns. If any key has been pressed in that row, the corresponding column line must read as 0. Otherwise, go to the next row and set it to 0, and read the columns again. This process is repeated until a 0 is found in a column. This determines the row and column for the pressed key, thus giving the exploration code for that key.
Simultaneous exploration of rows and columns: In this method, all rows and columns are explored simultaneously in two phases. First, the rows are configured as output and the columns as input. Internal pull-ups are enabled on the column lines. Then all rows are set to 0 and the columns are read. In any key has been pressed, the corresponding column will be read as 0. This detects the column but not its row. The whole process is reversed next. The rows are configured as input and the columns as output. Internal weak pull-ups are now enabled on row lines. All columns are set to 0, and the rows are read. The row that reads 0 contains the pressed key. This gives both the row and column for the pressed key. This approach of exploring the keypad is relatively faster than sequential exploration approach.
In case the microcontroller does not have the internal pull-up feature on its I/O port, the pull-up resistors on the input lines must be connected externally.
I am using my PIC16F1827 development board to demonstrate both the techniques of keypad scanning. The setup for this experiment is shown below.
I wrote the keypad scanning subroutine in mikroC Pro for PIC compiler, for both the techniques described in the theory section. In order to enable the weak pull-up on PORTB pins, the Weak Pull-up Enable (WPUEN) bit of OPTION_REG must be cleared and the corresponding bits of Weak Pull-Up on PORTB (WPUB) register must be set. The two programs (source code and HEX files) can be downloaded from the links provided below. Note that the mikroC Pro for PIC compiler also provides built-in library for Matrix Keypad, but that is not used here.
We discussed two basic approaches of keypad scanning and implemented it with a 16-key keypad interfaced to PIC16F1827 microcontroller. Both the techniques have one drawback, which is the microcontroller must continuously scan the rows and columns to make sure that it will not miss any key hit. This is a waste of time and CPU cycles, and it could burden the microprocessor’s resources and consume excessive amounts of power. There’s an alternative approach for overcoming this drawback: Interrupt driven keypad scanning. Using an interrupt frees the microcontroller to perform other tasks or to switch into a power-down mode while awaiting the next key closure. It also reduce electromagnetic interference produced by continuously scanning the rows and columns of the keypad. With PIC microcontroller’s Interrupt-on-change feature, this is easy to implement. I will describe this technique in another section very soon.