AT89S51 is an electronic device falling under the category of a microcomputer. It is basically an 8-bit CMOS device capable of performing high at low-power and comes with flash programmable and erasable read only memory (PEROM) of 4K byte size. AT89S51 is compatible with the industry-standard MCS-51 instruction set as well as pinout and the device are manufactured using Atmel’s high-density nonvolatile memory technology. Being a highly powerful microcomputer, AT89S51 provides effective solutions to numerous embedded control applications. 4×4 matrix keypads are commonly used keypads in embedded applications. Such keypads are seen in telephones and other commonly seen applications. Here in this chapter, we discuss in detail about the process of keypad interfacing with an AT89S51 microcontroller.
Matrix Keypad Interfacing
The advantage of a matrix keypad is that the use of it will allow the programmer to reduce the number of pins to be used. In a 4×4 matrix keypad, there are four rows and four columns connected to 16 push button switches. It may look like one needs 16 pins for the microcontroller to be connected to the matrix keypad but practically 16 inputs of keypad interface are possible with the 8 pins of a microcontroller port. All 8 lines can be connected to the same port or different ports based on the application requirements. In fact, 8 port pins of a microcontroller are sufficient for a 4×4 keypad interface using row & column matrix connection technique by saving other 8 bits of the port.
Matrix keypad interfacing and key press identification can be explained in a step by step manner which involves a software. First, we give a HIGH on row pins. When a key is pressed, the corresponding row and column get shorted. In the second step, a software scans the pins connected to the columns. If it detects a HIGH on any particular column, then it is found that the key press has been made of a key in that column. The third step is to figure out which key is pressed exactly. For this, the software writes logic high on row pins sequentially. The pin of the column on which the pressed key is situated will become high.
Principle of Working
The above diagram shows a basic 4×4 matrix keypad.
Procedure of Working
Connect four column pins and four-row pins to the microcontroller port. Either row or column should be configured as input and other as output. We obtain the key pressed by checking the voltage at the row/column configured as an input with each column/row pin made low one after the other.
#include <reg51.h> #include "delay.h" //delay header #include "lcd.h"
The header files should include LCD library and delay library. The corresponding key which is pressed can be displayed on the LCD. Each column should be configured low one after the other. All of the pins which are connected with rows should be scanned after making a column low. Thus we can find the row and column which had a key press and obtain the corresponding key.
/*1st column made low keeping other columns high and scan for the rows*/ P0=0XFE; if(P0_4==0) LCD_write_char_at(2,1,'1'); if(P0_5==0) LCD_write_char_at(2,1,'4'); if(P0_6==0) LCD_write_char_at(2,1,'7'); if(P0_7==0) LCD_write_char_at(2,1,'*');
The first column is made low with P0_0 (Port 0,0th pin) voltage at low. Since the rows are connected at the most significant four bits, the row pins P0_4-P0_7 are scanned for the low input. If any input pin is found at low voltage we can conclude that a key in the 1st column is pressed and it will correspond to row with low voltage. (It should be noted that all the row pins are initially high since they are connected with pull up resistors). If no port pins are identified low, the same scanning process should continue with column 2 being made low.