Technological University of The Philippines Ayala BLVD., Ermita, Manila College of Engineering
Technological University of The Philippines Ayala BLVD., Ermita, Manila College of Engineering
Technological University of The Philippines Ayala BLVD., Ermita, Manila College of Engineering
COLLEGE OF ENGINEERING
ELECTIVE 1
MICROCONTROLLER
SUBMITTED BY:
SILVA, BRICE M.
BSME – 4A
SUBMITTED TO:
ENGR. NELSON DELA PEÑA Jr.
4x4 Matrix Keypad Interfacing with 8051 Microcontroller
Circuit Diagram:
As shown in above circuit diagram, to interface Keypad, we need to connect 8 terminals of the
keypad to any port (8 pins) of the microcontroller. Like we have connected keypad terminals to
Port 1 of 8051. Whenever any button is pressed we need to get the location of the button, means
the corresponding ROW an COLUMN no. Once we get the location of the button, we can print
the character accordingly.
Steps on how to get the location of the pressed button:
1. First we have made all the Rows to Logic level 0 and all the columns to Logic level 1.
2. Whenever we press a button, 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 we get the column no. See main()
function.
3. Now we need to find the Row no., so we have created four functions corresponding to each
column. Like if any button of column one is pressed, we call function row_finder1(), to find the
row no.
4. In row_finder1() function, we reversed the logic levels, means now all the Rows are 1 and
columns are 0. Now Row of the pressed button should be 0 because it has become connected
(shorted) to the column whose button is
5. So whenever we find the Row at logic 0, means that is the row of pressed button. So now
we have column no (got in step 2) and row no., and we can print no. of that button using
lcd_data function.
Same procedure follows for every button press, and we are using while(1), to continuously
check, whether button is pressed or not.
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
if(R1==0)
lcd_data('1');
if(R2==0)
lcd_data('4');
if(R3==0)
lcd_data('7');
if(R4==0)
lcd_data('*');
}
if(R1==0)
lcd_data('2');
if(R2==0)
lcd_data('5');
if(R3==0)
lcd_data('8');
if(R4==0)
lcd_data('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();
}
Reference:
https://circuitdigest.com/microcontroller-projects/keypad-interfacing-with-8051-microcontroller