4×4 Keypad interface with 8051 Microcontroller

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
4x4-keypad-interface-with-8051-Microcontroller

Keypad interfacing plays a important role in interrupt processing and modes of giving inputs to the microcontroller.The above circuit teaches about 4×4 keypad interface with 8051 microcontroller and how the microcontroller takes input from the keypad.The interfacing of the keypad with the microcontroller was done by means of a method called Scanning.In this Scanning method the keys are connected in such a way one end of the keys was connected in rows and another end was given to column respectively thereby forming 4 x 4 keypad.Each row was connected to each pins in the port  and columns to another port pins respectively.

After connecting the rows and columns to their respective ports as shown in the above circuit diagram then its time to apply logic to make it work and read input from the Keypad.In order to make this work we are about to keep a (either row or column) value as constant and change either row or columns input to observe the change when the keypad is pressed.In simple words we are going to use one as input and another as output.In the above circuit we are going to 

  • Take rows as inputs and columns as outputs.
  • Apply logic 1 to all columns in the keypad.
  • Apply logic 0 to all rows one by one and read the column output.
  • Say for example if logic 0 was applied to row 1 in the keypad and in that instant if switch “2” of that row was pressed  then the pin P3.5  in which the “2nd” switch was connected becomes low.
  • This low signal in that P3.5 pin tells the controller that the switch in the “SW2” was pressed.
  • By this way every row was scanned infinite times and controller looks for pressed key in the keypad.

Since the speed of the Microcontroller was very high it scans the whole keypad in fraction of seconds, so there is no chance of missing a pressed key.The speed of scanning depends on the frequency of the crystal used in the Microcontroller.We should give the value that a controller should take when a key is pressed in the program.I have added the LED’s in the port 0 in order to illustrate the working of the switches connected to the microcontroller.That is i have written the program in such a way that whenever a key was pressed corresponding LED given in the program will lit up.The LED’s was connected in the common anode mode so whenever logic 0 applied to port 0 will make the LED’s to light up.

Now lets move into the Programming the controller for scanning the microcontroller.The program was written in Embedded C language and the software used was Archimedes IDE 8051.

CODE:

#include<stdio.h>
#include<reg51.h>
unsigned char i;                         //Assigning Integer
void main()                                //Main program
{
P0=0x00;                                  //Initialising Port 0
while(1)                                    //Infinite loop to run the program infinitely
{
P0=0xff;                                   //Giving high values to Port 0.
P2=0xfe;                                  //Making row "1" of the keypad low.
P3=0xf0;                                 //Giving logic 1 to all the columns      
i=P3;                                       //Giving the values of port 3 to integer i
{
if(i==0xe0){P0=0xfe;}            //Checking switches 1,2,3,4
else if(i==0xd0){P0=0xfd;}   
else if(i==0xb0){P0=0xfb;}   
else if(i==0x70){P0=0xf7;}   
}
P0=0xff;
P2=0xfd;                                  //Make row "2" of the keypad low 
P3=0xf0;
i=P3;
{
if(i==0xe0){P0=0xfc;}            //Checking switches 5,6,7,8
else if(i==0xd0){P0=0xf8;}   
else if(i==0xb0){P0=0xf1;}   
else if(i==0x70){P0=0xf0;}   
}
P0=0xff;
P2=0xfb;                                //Making row 3 of the keypad low.
P3=0xf0;                                 
i=P3;
{
if(i==0xe0){P0=0xfc;}             //Checking switches 9,10,11,12
else if(i==0xd0){P0=0xf8;}    
else if(i==0xb0){P0=0xf1;}    
else if(i==0x70){P0=0xf0;}    
}
P0=0xff;
P2=0xf7;
P3=0xf0;
i=P3;
{
if(i==0xe0){P0=0xfc;}            //Checking switches 13,14,15,16
else if(i==0xd0){P0=0xf8;}   
else if(i==0xb0){P0=0xf1;}   
else if(i==0x70){P0=0xf0;}   
}
}

Related Designs and Schematics

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

Be the First to Comment!

Notify of
avatar
wpDiscuz