Keypad interfacing plays a important role in interrupt processing and modes of giving inputs to the microcontroller.The above circuit teaches you how to interface a 4 x 4 keypad with the microcontroller and how the micrcontroller 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.
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.
PROGRAM:
#include<stdio.h>
#include<reg51.h>
unsigned char i; //Assigning Integers
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;} //Check that switch 1 is pressed and assign values for it
else if(i==0xd0){P0=0xfd;} //Check that switch 2 is pressed and assign values for it
else if(i==0xb0){P0=0xfb;} //Check that switch 3 is pressed and assign values for it
else if(i==0x70){P0=0xf7;} //Check that switch 4 is pressed and assign values for it
}
P0=0xff;
P2=0xfd; //Make row 2 of the keypad low
P3=0xf0;
i=P3;
{
if(i==0xe0){P0=0xfc;} //Check that switch 5 is pressed and assign values for it
else if(i==0xd0){P0=0xf8;} //Check that switch 6 is pressed and assign values for it
else if(i==0xb0){P0=0xf1;} //Check that switch 7 is pressed and assign values for it
else if(i==0x70){P0=0xf0;} //Check that switch 8 is pressed and assign values for it
}
P0=0xff;
P2=0xfb; //Making row 3 of the keypad low.
P3=0xf0;
i=P3;
{
if(i==0xe0){P0=0xfc;} //Check that switch 9 is pressed and assign values for it
else if(i==0xd0){P0=0xf8;} //Check that switch 10 is pressed and assign values for it
else if(i==0xb0){P0=0xf1;} //Check that switch 11 is pressed and assign values for it
else if(i==0x70){P0=0xf0;} //Check that switch 12 is pressed and assign values for it
}
P0=0xff;
P2=0xf7;
P3=0xf0;
i=P3;
{
if(i==0xe0){P0=0xfc;} //Check that switch 13 is pressed and assign values for it
else if(i==0xd0){P0=0xf8;} //Check that switch 14 is pressed and assign values for it
else if(i==0xb0){P0=0xf1;} //Check that switch 15 is pressed and assign values for it
else if(i==0x70){P0=0xf0;} //Check that switch 16 is pressed and assign values for it
}
}
}