KEYBOARD INTERFACING WITH 8051
Keypad is a widely used input device with lots of application in our everyday life. Keypad is used to take input from the user for further processing. In this article we are interfacing keypad with the 8051 . The program to interface keypad with controller is written in C language which is very easy to understand.
Keypad is organized as a matrix of switches in rows and column.The circuit diagram shows the connection of keypad with the controller.
The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number . Hence every time a key is pressed the number is identified by detecting the row and column number of the key pressed.
Initially all the rows are set to zero by the controller and the columns are scanned to check if any key is pressed. In case no key is pressed the output of all the columns will be high.
Whenever a key is pressed the row and column corresponding to the key will get short, resulting in the output of the corresponding column goes to go low (since we have made all the rows zero). This gives the column number of the pressed key.
Once the column number is detected, the controller set’s all the rows to high. Now one by one each row is set to zero by controller and the earlier detected column is checked if it becomes zero. The row corresponding to which the column gets zero is the row number of the digit.
The above process is very fast and even if the switch is pressed for a very small duration of time the controller can detect the key which is pressed. The controller displays the number corresponding to the row and column on the LCD.
Program for Keyboard Interfacing
The columns are connected to port 2 and Rows are connected to port 1.
This program detects the key and send the ASCII code serially to other devices.
Serial begin and serial send functions are stored in msk.h header file.
void delay(unsigned int );
sbit c1=P2^0;
sbit c2=P2^1;
sbit c3=P2^2;
#define MSK_SERIAL
#include<msk.h>
void main (void)
{
unsigned char msk[4][3]={'1','2','3',
'4','5','6',
'7','8','9',
' ','0',' '; };
unsigned char str[]="Keyboard Detected";
unsigned char a,b,i;
b=0xff;
serialbegin();
for(i=0;i<=16;i++)
{
serialsend(str[i]);
}
delay(500);
serialsend(';');
while(1)
{
P2=0x0ff;
P1=0x00;
while(P2==b);
delay(20);
if(P2!=b){
P1=0x0fe;
if(P2!=b)
{
a=0;
goto col;
}
P1=0x0fd;
if(P2!=b)
{
a=1;
goto col;
}
P1=0x0fb;
if(P2!=b)
{
a=2;
goto col;
}
P1=0x0f7;
if(P2!=b)
{
a=3;
goto col;
}
col:
if(c1==0)
{
serialsend(msk[a][0]);
}
else if(c2==0)
{
serialsend(msk[a][1]);
}
else if(c3==0)
{
serialsend(msk[a][2]);
}
while(P2!=b);
}
}
}
Comments
Post a Comment