4x4 Matrix Keypad Interfacing  
This circuit comprises of a LCD, 4X4 Keyboard and a microcontroller.

In this program we have assigned a character to each key. The microcontroller scans the key value and it will be displayed in the LCD.

The lines DP1-DP8 are the data lines of the LCD. The control lines RS, RW, EN pins are connected to the Port lines P3.5, P3.6 and P3.7 respectively.

The 4X4 Matrix keyboard is connected to the Port2. RA1-RA4 is the row of the keyboard and it is connected to the port lines P2.0-P2.3 and CL1-CL4 is the column of the keyboard and it is connected to the Port lines P2.4-P2.7.

The microcontroller assigns the row value to the lower four bits of Port2 and the column values to the higher four bits of Port2. (For Example if the value in the Port2 is “EE” means the character displayed in the LCD is ‘1’).



When a key is pressed a row and a column make a contact; otherwise there is no connection between rows and columns.

To detect a pressed key, the microcontroller grounds all columns by providing 0 to the port2 higher 4 bits, and then it checks for rows.

If the data read from the rows is F, no key has been pressed and the process continues until a key press is detected. If a 0 is detected in the four bits of the row (lower four bits of port2) then a key press is detected.

After the key press got detected the microcontroller check the column value. If any of the higher four bits in port2 becomes 0 the column value was got detected.

Then based on the port2 value it checks with the program (Key scan function is to detect the key value) and moves the correct value to port1 and it will be displayed in the LCD.








/*******************************************************************
Example program to demonstrate Keypad scanning. This program scans keypress on 4x4 keypad and display the keycode to the LCD *******************************************************************/


#include <stdio.h> //Define I/O Functions
#include <reg51.h> //Define 8051 Registers
#define DATA P1 //Define DATA to Port1
void lcd_init(void); //LCD Initialization
void lcd_cmd(unsigned char); //LCD Command Function
void lcd_display(unsigned char); //LCD Display Function
void Key_Scan(void); //KeyScan Function
void DelayMs(int); //DelayMs Function
sbit RS = P3^5; //Register Select
sbit RW = P3^6; //LCD Read/Write
sbit lcd_e = P3^7; //LCD Enable
unsigned char R,C,ch;
unsigned int i=0;
unsigned char Key[4][4] = {'1','2','3','4',
                           '5','6','7','8',
                           '9','0','A','B',
                           'C','D','E','F',
                           };

code unsigned char msg[] = ("8051 DEVELOPMENT");//Display the Message

code unsigned char msg1[] = (" BOARD ");



//-------------------------------
// Main Program
//-------------------------------


void main()
{
lcd_init();
while(1)
{
Key_Scan();
ch = Key[C][R]; //Assign Key value to ch;
SBUF=ch; //[C][R] denotes Column
DelayMs(35); //and Row Value of Keypad
lcd_display(ch);
lcd_cmd(0x80);
DelayMs(35);
}
}


//-------------------------------
// Key Scan Function
//-------------------------------


void Key_Scan(void)
{
unsigned int i = 0;
//Scanning for Row Value
P2 = 0x0F; //Initialize Port2 to 0Fh
while(P2 == 0x0F);
if(P2 == 0x0E) //Checking for Row0
R = 0;
else if(P2 == 0x0D) //Checking for Row1
R = 1;
else if(P2 == 0x0B) //Checking for Row2
R = 2;
else if(P2 == 0x07) //Checking for Row3
R = 3;
//Scanning for Column Value
P2 = 0xF0; //Initialize Port2 to F0h
while(P2 == 0xF0);
if(P2 == 0xE0) //Checking for Column0
C = 0;
else if(P2 == 0xD0) //Checking for Column1
C = 1;
else if(P2 == 0xB0) //Checking for Column2
C = 2;
else if(P2 == 0x70) //Checking for Column3
C = 3;
DelayMs(50);
}


//-------------------------------
// LCD command Function
//-------------------------------


void lcd_cmd(unsigned char cmnd)
{
DATA = cmnd;
RS = 0; //RS:Register Select
RW = 0; //RW:Read/Write
lcd_e = 1; //LCD Enable
DelayMs(35);
lcd_e = 0;
}



//-------------------------------
// LCD Data Function
//-------------------------------


void lcd_display(unsigned char dat)
{
DATA = dat;
RS = 1; //RS:Register Select
RW = 0; //RW:Read/Write
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}



//-------------------------------
// LCD Initialization
//-------------------------------


void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x38); //2x16 Character 5x7 dot
DelayMs(15); //matrix LCD,8-bit format
lcd_cmd(0x0c); //Display On, cursor off
DelayMs(15);
lcd_cmd(0x06); //Shift Cursor to right
DelayMs(15);
lcd_cmd(0x01); //Clear display screen
DelayMs(15);


//------------------------------------
// First Line Message Display
//------------------------------------

lcd_cmd(0x80); //First Line Initialization
DelayMs(35);
i=0;
while(msg[i]!='\0')
{
lcd_display(msg[i]);
i++;
}
DelayMs(50);


//------------------------------------
// Second Line Message Display
//------------------------------------

lcd_cmd(0xc0); //Second Line Initialization
DelayMs(35);
i=0;
while(msg1[i]!='\0')
{
lcd_display(msg1[i]);
i++;
}
DelayMs(50);
}

//--------------------------
// DelayMs Function
//--------------------------

void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}





devesh@electroons.com
Best viewed at Firefox 1024x768 resolution