Wednesday 16 October 2013

HOW TO INTERFACE A 4X3 MATRIX KEYPAD TO MICROCONTROLLER 8051(AT89C51) WITH C PROGRAM


Matrix Keyboard interfacing with Micro controller 8051  


      

Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this tutorial the keyboard fundamentals, along with key press and key detection mechanisms was discussed. Interfacing a keyboard with microcontroller 8051 also described here. Keypad is a widely used input device with lots of application in our everyday life. From a simple telephone to keyboard of a computer, ATM, electronic lock, etc., 

keypad is used to take input from the user for further processing. In this article we are interfacing keypad with the MCU AT89C51 and displaying the corresponding number on LCD.  This module can be further used in a number of systems to interfaced keypad with microcontroller and other processors to get desired output. 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 article uses a 4X3 matrix keypad and a 16x2 LCD for displaying the output of keypad. The circuit diagram shows the connection of keypad with the controller.Port P1 of the microcontroller is used to send the data for displaying on the LCD. P2^1, P2^2, P2^3 pins of microcontroller is connected to RS, RW, EN pins of LCD respectively. Port P3 is used to scan input from the keypad (refer circuit diagram for connection).


      Fig.1 Interfacing matrix keypad with microcontroller 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 column through ports; therefore, with two 8-bit ports, an 8*8 matrix of keys can be connected to a microprocessor. When a key pressed, a row and column make a connect; otherwise, there is no connection between row and column. In IBM PC keyboards, a single micro controller (consisting of microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of software and hardware interfacing of keyboard. In such systems it is the function of programs stored in the EPROM of micro controller 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:      
       
The following Figure shows a 4*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 1s 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 micro controller to scan the keyboard continuously to detect and identify the key pressed. How it is done is explained next.

The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number (n(R, C) for example 6 (2, 3)). 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.

There are IC chips such as National Semiconductors MM74C923 that incorporate keyboard scanning and decoding all in one chip. Such chips use combinations of counters and logic gates (No micro controller).

 Keypad interfacing Embedded C Program:

#include<reg51.h>
#define port P2
#define dataport P1  // Dataport for lcd
#define sec 100
sbit rs = port^1;
sbit rw = port^2;
sbit en = port^3;

sbit col1=P3^4;
sbit col2=P3^5;
sbit col3=P3^6;
sbit row1=P3^0;
sbit row2=P3^1;
sbit row3=P3^2;
sbit row4=P3^3;
void delay(unsigned int msec) //Time delay function
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char item) //Function to send command to LCD
{
dataport = item;
en=1;
rs= 0;
rw=0;
delay(1);
en=0;

}
void lcd_data(unsigned char disp) //Funtion to send data on LCD
{
dataport = disp;
en=1;
rs= 1;
rw=0;
delay(1);
en=0;

}

lcd_string(unsigned char *disp) // Function to send string on LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);       
}
}
void lcd_ini()                  //Function to initialize the LCD
  {
    lcd_cmd(0x38);         
    delay(5);
    lcd_cmd(0x0F);       
    delay(5);
          lcd_cmd(0x01);  //clear screen
          delay(5);
    lcd_cmd(0x80);

    delay(5);
   }
void display(int a) //Display functon for LCD

{

 lcd_ini();
switch(a)
{
case 1:lcd_string("one 1");
break;
case 2:lcd_string("two 2");
break;
case 3:lcd_string("three 3");
break;
case 4:lcd_string("four 4");
break;
case 5:lcd_string("five 5");
break;
case 6:lcd_string("six 6");
break;
case 7:lcd_string("seven 7");
break;
case 8:lcd_string("eight 8");
break;
case 9:lcd_string("nine 9");
break;
case 10:lcd_string("zero 0");
break;
case 11:lcd_string("*");
break;
case 12:lcd_string("#");
break;
}
}

void check_col1() //Function for checking column one
{
row1=row2=row3=row4=1;
row1=0;
if(col1==0)           
display(1);
row1=1;
row2=0;
if(col1==0)
display(4);
row2=1;
row3=0;
if(col1==0)
display(7);
row3=1;
row4=0;
if(col1==0)
display(11);
row4=1;
}

void check_col2() //Function for checking column two
{
row1=row2=row3=row4=1;
row1=0;
if(col2==0)
display(2);
row1=1;
row2=0;
if(col2==0)
display(5);
row2=1;
row3=0;
if(col2==0)
display(8);
row3=1;
row4=0;
if(col2==0)
display(0);
row4=1;
}

void check_col3() //Function for checking column three
{
row1=row2=row3=row4=1;
row1=0;
if(col3==0)
display(3);
row1=1;
row2=0;
if(col3==0)
display(6);
row2=1;
row3=0;
if(col3==0)
display(9);
row3=1;
row4=0;
if(col3==0)
display(12);  //For #
row4=1;
}          
void keypad()
{
          row1=row2=row3=row4=0;
          if(col1==0)
          check_col1();
          if(col2==0)
          check_col2();
          if(col3==0)
          check_col3();
}

void main()
{
col1=col2=col3=1;
while(1)
{                

keypad();
 }      
}


   

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...