Akhil Jain
Keypads are a part of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Martix keypads are well known for their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 or 2x2 matrix keypad with AVR and 8051 microcontroller. Also we will see how to program then in Assembly and C.
Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this section, we first discuss keyboard fundamentals, along with key press and key detection mechanisms. Then we show how a keyboard is interfaced to an 8051.Interfacing the keyboard to the 8051 At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports; therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller (consisting of a microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of hardware and software interfacing
of the keyboard. In such systems, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. In this section we look at the mechanism by which the 8051 scans and identifies the key. Scanning and identifying the key Figure 12-6 shows a 4 x 4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1 s for all columns since they are all connected to high (Vcc). If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed. How it is done is explained next.
To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns is D1 -- DO = 11, no key has been pressed and the process continues until a key press is detected. However,
if one of the column bits has a zero, this means that a key press has occurred. For example, if D1 -- DO = 01, this means that a key in the Dl column has been pressed. After a key press is detected, the microcontroller will go through the process of identifying the
key. Starting with the top row, the microcontroller grounds it by providing a low to row DO only; then it reads the columns. If the data read is all Is, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified.To make sure that the preceding key has been released, Os are output to all rows at once, and the columns are read and checked repeatedly until all the columns are high. When all columns are found to be high, the program waits for a short amount of time before it goes to the next stage of waiting for a key to be pressed.To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. Remember that the output latches connected to rows still have their initial zeros (provided in stage 1), making themgrounded. After the key press detection, the microcontroller waits 20 ms for
the bounce and then scans the columns again. This serves two functions: (a) it ensures that the first key press detection was not an erroneous one due to a spike noise, and (b) the 20-ms delay prevents the same key press from being interpreted as a multiple key press. If after the 20Tins delay the key is still pressed, it goes to the next stage to detect which row it belongs to; otherwise, it goes back into the loop to detect a real key press.
To detect which row the key press belongs to, the microcontroller grounds one row at a time, reading the columns each time. If it finds that all columns are high, this means that the key press cannot belong to that row; therefore, it grounds the next row and continues until it finds the row the key press belongs to. Upon finding the row that the key press belongs to, it sets up the starting address for the look-up table holding the scan codes (or the ASCII value) for that row and goes to the next stage to identify the key.
**************************************** CODE 'C' ****************************]
#include<reg52.h>
sbit col1=P1^2;
sbit col2=P1^3;
sbit row1=P1^0;
sbit row2=P1^1; //define pins for easier reading
sbit out1=P2^0;
sbit out2=P2^1;
sbit out3=P2^2;
sbit out4=P2^3;
void main ()
{
unsigned char j;
col1=col2=1; //Input Port
while(1) //repeat forever
{
do
{
row1=row2=0; //Initialising Row With 0
}while(col1==0|col2==0); //check until all keys are released
while(1)
{
row2=1;
row1=0; //grounding row1
if (col1==0|col2==0) //column detected
{
j=0; //save row location
break; //exit while loop
}
row1=1;
row2=0; //grounding row2
if (col1==0|col2==0)
{
j=1;
break;
}
}
out1=out2=out3=out4=1;//To Switch off previous LED,When another is pressed
//check column and send result to output pins
if(col1==0&col2==1)
{
if(j==0)
out1=0; //if 1st key is pressed
else
out3=0;
}
if(col1==1&col2==0)
{
if(j==0)
out2=0; //if 2nd key is pressed
else
out4=0;
}
} //// Time for Proteus Simulator :)
}
**********************************************************************************
**************************************** Hex file ***********************************
:030000000200A853
:0C00A800787FE4F6D8FD758107020003A4
:10000300D293D292C291C2902093047E0180027E49
:10001300002092047D0180027D00ED4E70E6D291B6
:10002300C2902093047E0180027E002092047D0111
:1000330080027D00ED4E6004E4FF801CD290C291EB
:100043002093047E0180027E002092047D018002C1
:100053007D00ED4E60C87F01D2A3D2A2D2A1D2A06F
:100063003093047E0180027E002092047D01800291
:100073007D00ED5E6009EF7004C2A08002C2A22081
:1000830093047E0180027E003092047D0180027D14
:1000930000ED5E7003020007EF7005C2A1020007C6
:0500A300C2A3020007EA
:00000001FF
**********************************************************************************
No comments:
Post a Comment