Monday 23 November 2015

How to interface keypad to pic microcontroller

           how to interface keypad to pic micro-controller

keypad is a set of buttons arranged in a block or "pad" which usually bear digits, symbols and usually a complete set of alphabetical letters. If it mostly contains numbers then it can also be called a numeric keypad. Keypads are found on many alphanumeric keyboards and on other devices such as calculatorspush-button telephonescombination locks, and digital door locks, which require mainly numeric input.

Matrix Keypad Interface Logic

Initially all switches are assumed to be released. So there is no connection between the rows and columns. When any one of the switches are pressed, the corresponding rows and columns are connected (short circuited). This will drive that column pin (initially high) low. Using this logic, the button press can be detected. The colors red and black is for logic high and low respectively.  Here are the steps involved in determining the key that was pressed.

Step 1:

The first step involved in interfacing the matrix keypad is to write all logic 0’s to the rows and all logic 1’s to the columns. In the image, black line symbolizes logic 0 and red line symbolizes logic 1.
For now let us assume that, the circled key is pressed and see how the key press can be detected by a software routine.
Wiring diagram

Step 2:

Now the software has to scan the pins connected to columns of the keypad. If it detects a logic 0 in any one of the columns, then a key press was made in that column. This is because the event of the switch press shorts the C2 line with R2. Hence C2 is driven low.
Note: color of the lines indicate the logic values they return.
step 3

Step 3:

Once the column corresponding to the key pressed is located, the next thing that the software has to do is to start writing logic 1’s to the rows sequentially (one after the other) and check if C2 become high. The logic is that if a button in that row was pressed, then the value written to that row will be reflected in determined column (C2) as they are short circuited. Note: color of the lines indicate the logic values they return.
step 4

Step 4:

The procedure is followed till C2 goes high with logic high is written to a row. In this case, a logic high to the second row will be reflected in the second column.
Note: color of the lines indicate the logic values they return.
Step 5
We already know the key press happened at column 2. Now we have detected that the key is in row 2. So, the position of the key in the matrix is (2,2)
Once this is detected, its up to us to name it or provide it with a task in the event of the key press.
har keypad()
{
while(1)
{
//unsigned char n;
d1=0;d2=d3=d4=d5=d6=1;
if(u1==0){lcd_delay(1000);while(u1==0);lcd_delay(1000);return'N';}
if((u2==0)){lcd_delay(1000);while(u2==0);lcd_delay(1000);return("+/-");}
if((u3==0)){lcd_delay(1000);while(u3==0);lcd_delay(1000);return("%  ");}
if((u4==0)){lcd_delay(1000);while(u4==0);lcd_delay(1000);return("sq  ");}
d2=0;d1=d3=d4=d5=d6=1;
if((u1==0)){lcd_delay(1000);while(u1==0);lcd_delay(1000);return('7');}
if((u2==0)){lcd_delay(1000);while(u2==0);lcd_delay(1000);return('4');}
if((u3==0)){lcd_delay(1000);while(u3==0);lcd_delay(1000);return('1');}
if((u4==0)){lcd_delay(1000);while(u4==0);lcd_delay(1000);return('0');}

d3=0;d1=d2=d4=d5=d6=1;
if((u1==0)){while(u1==0);lcd_delay(1000);return('8');}
if((u2==0)){while(u2==0);lcd_delay(1000);return('5');}
if((u3==0)){while(u3==0);lcd_delay(1000);return('2');}
if((u4==0)){while(u4==0);lcd_delay(1000);return('.');}


d4=0;d1=d3=d2=d5=d6=1;
if((u1==0)){while(u1==0);lcd_delay(1000);return('9');}
if((u2==0)){while(u2==0);lcd_delay(1000);return('6');}
if((u3==0)){while(u3==0);lcd_delay(1000);return('3');}
if((u4==0)){while(u4==0);lcd_delay(1000);return('=');}

d5=0;d1=d3=d2=d4=d6=1;
if((u1==0)){while(u1==0);lcd_delay(1000);return('*');}
if((u2==0)){while(u2==0);lcd_delay(1000);return('-');}
if((u3==0)){while(u3==0);lcd_delay(1000);return('+');}
if((u4==0)){while(u4==0);lcd_delay(1000);return('+');}
d6=0;d1=d3=d2=d4=d5=1;
if((u1==0)){while(u1==0);lcd_delay(1000);return('/');}
if((u2==0)){while(u2==0);lcd_delay(1000);return('m');}
if((u3==0)){while(u3==0);lcd_delay(1000);return('i');}
if((u4==0)){while(u4==0);lcd_delay(1000);return('d');}
//lcd_delay(1000);
 }}    

No comments:

Post a Comment