MainRegistrationLogin Electro-Hobby Saturday, 21.12.2024, 08:57
  Circuits Welcome Guest | RSS

Search

Statistics

Total online: 1
Guests: 1
Users: 0

 
 Home » Microcontroller » Matrix keypad interfacing
14:16
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.




Theory

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.

Software

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.


Category: Microcontroller | Views: 1762 |
 
 
Advertising

 

Copyright MyCorp © 2024
Free web hostinguCoz