4X4 Matrix Keypad
EXPERIMENT 3
INTERFACING 8051 WITH MATRIX KEYPAD
AIM:
To interface a 4X4 Matrix Keypad with 8051 Microcontroller.
COMPONENTS REQUIRED:
S.NO COMPONENTS QUANTITY
1 8051 Microcontroller Trainer Kit 1
2 In System Programmer 1
3 Jumper Wires As Required
4 Laptop with Data Cable 1
5 LCD (16X2) 1
6 4X4 Matrix Keypad 1
SOFTWARE REQUIRED:
1. Keil Microvision 5
2. ProgISP
THEORY:
Keypads are widely used input devices being used in various electronics and embedded
projects. They are used to take inputs in the form of numbers and albhabets and feed the same
into the system for further processing. The Matrix keypad consists of a set of Push buttons,
which are interconnected and there are 4 push buttons in each of the four rows of the 4X4
matrix keypad used here. The terminals of the push buttons are connected according to the
diagram. In the first row, one terminal of all the 4 push buttons is connected together and
another terminal of 4 push buttons are representing each of the 4 columns, same goes for each
row. So 8 terminals are required to connect with a microcontroller.
First the LCD module is interfaced to display the data which will be feed through keypad.
In this experiment keypad terminals are connected to Port 1 of 8051. Whenever any button is
pressed the location of the button is obtained, means the corresponding ROW and COLUMN
number are found out. Once the location of the button is known, the character can be printed
on LCD accordingly.
Scanning Logic
1. First all the Rows are made Logic level 0 and all the columns are made Logic level 1.
2. Whenever a button is pressed, column and row corresponding to that button gets shorted
and makes the corresponding column to logic level 0. Because that column becomes
connected (shorted) to the row, which is at Logic level 0. So the column number is found.
3.Now to find the Row number, four functions are created corresponding to each column.
Like if any button of column one is pressed, function row_finder1() is called, to find the row
number.
4. In row_finder1() function, the logic levels are reversed, means now all the Rows are 1 and
columns are 0. Now Row of the pressed button should be 0 because it has been connected
(shorted) to the column whose button is pressed, and all the columns are at 0 logic. So all
rows are scanned for 0.
5. So whenever the Row at logic 0 is found, means that is the row of pressed button. Now the
column number (got in step 2) and row number are found, and the character of that button can
be printed using lcd_data function.
Same procedure follows for every button press, and while(1) loop is used, to continuously
check, whether button is pressed or not.
KEYPAD WITH 8051 INTERFACING DIAGRAM:
PROCEDURE:
1. Give the connections as per the interfacing diagram.
2. Open Keil Software. Enter the type of microcontroller (AT89C52) that is to be used in
the window that is displayed at first.
3. Create a new Project file under which the “.c” file containing the code for the
experiment must be created.
4. Compile the code in the .c file by using the ‘Build’ command.
5. To create the hex file, click the ‘Target’ command, and in the dialog box that displays
after, enter the crystal frequency to be 11.0592 MHz, go to the Output window, where
you enable the option to create a hex file, and once again the file must be compiled using
the ‘Build’ command.
6. Once the hex file is created, connect the Trainer Kit to the In-System Programmer
using Jumper wires, and connect the In-System Programmer to the Laptop using the data
cable.
7. Open ProgISP software. Make sure that all the necessary drivers are installed along
with the software. Enter the details of the microcontroller used and ensure that the
software has read the presence of the microcontroller.
8. In the right-side panel of the window, click the ‘Load Flash’ button and select the hex
file that had been created earlier.
9. After the file gets uploaded, click the ‘Auto’ button, to download the program onto
the microcontroller from the laptop.
10. Once the program is downloaded into the microcontroller, the output can be
observed and verified.
EMBEDDED C CODE:
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller sbit
rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3 sbit e
= P3^4; //E pin connected to pin 4 of port 3
sbit C4 = P1^0; // Connecting keypad to Port 1
sbit C3 = P1^1;
sbit C2 = P1^2;
sbit C1 = P1^3;
sbit R4 = P1^4;
sbit R3 = P1^5;
sbit R2 = P1^6;
sbit R1 = P1^7;
void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command; rs=
0;
rw=0;
e=1;
msdelay(1); e=0;
}
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data; rs=
1;
rw=0;
e=1;
msdelay(1); e=0;
}
void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking msdelay(10);
lcd_cmd(0x01); //clear screen msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1 msdelay(10);
}
void row_finder1() //Function for finding the row for column 1
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('1');
if(R2==0)
lcd_data('4');
if(R3==0)
lcd_data('7');
if(R4==0)
lcd_data('*');
}
void row_finder2() //Function for finding the row for column 2
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('2');
if(R2==0)
lcd_data('5');
if(R3==0)
lcd_data('8');
if(R4==0)
lcd_data('0');
}
void row_finder3() //Function for finding the row for column 3
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('3');
if(R2==0)
lcd_data('6');
if(R3==0)
lcd_data('9');
if(R4==0)
lcd_data('#');
}
void row_finder4() //Function for finding the row for column 4
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('A');
if(R2==0)
lcd_data('B');
if(R3==0)
lcd_data('C');
if(R4==0)
lcd_data('D');
}
void main()
{
lcd_init();
while(1)
{
msdelay(30);
C1=C2=C3=C4=1;
R1=R2=R3=R4=0;
if(C1==0)
row_finder1();
else if(C2==0)
row_finder2();
else if(C3==0)
row_finder3();
else if(C4==0)
row_finder4();
}
}
PRE-LAB QUESTIONS:
1. What is a matrix keypad?
2. What is the function of a keypad?
3. How a key press is detected in keypad?
4. Why matrix structure is preferred for keypad interface?
5. What is meant by key bouncing?
6. How many buttons are in the keypad used and how many ports are required to
interface it?
7. What will be the port outputs when no key is pressed?
8. Is key press detection and key press identification same?
9. How many port pins are required to interface a 3X3 Keypad?
10. List some applications of Keypad interfacing.
RESULT:
The interfacing of 4X4 Matrix Keypad with 8051 microcontroller kit was
successfully completed and the character was displayed in an LCD.