The first thing you must know how does the keypad works.we initially make our keypad rows and coulombs high 1(Actually making the pins connected to the rows and coulombs high 1). When you press any key of the keypad the corresponding row and coulomb of the keypad becomes grounded.now we simply check the corresponding row and coulomb and identify which key is pressed. Here i am giving a code which checks the corresponding pressed key and then prints the associated character with that key on your lcd(16×2,16×1 etc ). Note that the letters or numbers that are printed on your keypad are not actually printing on the screen. Keypad doesn’t do any thing on its(Only just it grounds the associated row and coulomb of the key that you pressed on it).Its your code which made the letter printed on your lcd(8×1,8×2).You can also program your micro controller in the way that the keypad first button prints all the alphabets and digits. Oh! its some thing new to you by it is true. For example on you cell phone each button prints the alphabets and numeric digit as well as all the special characters and symbols on your mobile screen.Now starting the code.I am using 4×4 keypad and the code is made using it and it works you just have to place it in your code and call it from your main function and your alphanumeric keypad is ready to use.
SOME VARIABLES DESCRIPTIONS
SOME VARIABLES DESCRIPTIONS
- r0-r1-r2-r3 are rows that are connected to my keypad. co-c1-c3-c4 are coulombs that are connected to my keypad. 4 rows and 4 coulombs thus making a 4×4 keypad having 16 buttons on it.
- P1 is micro controller port which i am using for rows and coulombs.
- cmd(); is function to send commands to the lcd.
- lcddata(); is function which is sending data to lcd.
- delay(); is function to include necessary delay();
NOTE: That a character data can only be sent to lcd we can not send any data that is other than character or grater that 8-bits .You can also send ASCII characters.
4×4 Alphanumeric keypad code
//IDENTIFYING KEYSTROKE
void keypad(){
unsigned char c=’t’;
while(c!=’E’){
//a,b,c,1
r0=0;r1=1;r2=1;r3=1;
if(c0==0 && r0==0){
lcddata(‘a’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘b’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘c’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘1’);P1=0XFE;delay(10000);
}
}
}
P1=0xFF;
}
r0=0;r1=1;r2=1;r3=1; //d,e,f,2
if(c1==0 && r0==0){
lcddata(‘d’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘e’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘f’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘2’);P1=0xFE;delay(10000); }
}
} P1=0xFF;
}
//g,h,i,3
r0=0;r1=1;r2=1;r3=1;
if(c2==0 && r0==0){
lcddata(‘g’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘h’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘i’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘3’);P1=0xFE;delay(10000); } }
} P1=0xFF;
}
//j,k,l,4
r0=0;r1=1;r2=1;r3=1;
if(c3==0 && r0==0){
lcddata(‘j’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘k’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘l’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘4’);P1=0xFE;delay(10000); } }
} P1=0xFF;
}
//m,n,o,5
r0=1;r1=0;r2=1;r3=1;
if(c0==0 && r1==0){
lcddata(‘m’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘n’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘o’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘5’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//p,q,r,6
r0=1;r1=0;r2=1;r3=1;
if(c1==0 && r1==0){
lcddata(‘m’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘n’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘o’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘5’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//s,t,u,7
r0=1;r1=0;r2=1;r3=1;
if(c2==0 && r1==0){
lcddata(‘s’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘t’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘u’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘7’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//v,w,x,8
r0=1;r1=0;r2=1;r3=1;
if(c3==0 && r1==0){
lcddata(‘v’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘w’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘x’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘8’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//y,z,9
r0=1;r1=1;r2=0;r3=1;
if(c0==0 && r2==0){
lcddata(‘y’);P1=0xFB;delay(10000);
if(c0==0 && r2==0){
cmd(0x10); lcddata(‘z’);P1=0xFB;delay(10000);
if(c0==0 && r2==0){
cmd(0x10); lcddata(‘9’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//0,-,>
r0=1;r1=1;r2=0;r3=1;
if(c1==0 && r2==0){
lcddata(‘0’);P1=0xFB;delay(10000);
if(c1==0 && r2==0){
cmd(0x10); lcddata(‘-‘);P1=0xFB;delay(10000);
if(c1==0 && r2==0){
cmd(0x10); lcddata(‘>’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//!,@,#
r0=1;r1=1;r2=0;r3=1;
if(c2==0 && r2==0){
lcddata(‘!’);P1=0xFB;delay(10000);
if(c2==0 && r2==0){
cmd(0x10); lcddata(‘@’);P1=0xFB;delay(10000);
if(c2==0 && r2==0){
cmd(0x10); lcddata(‘#’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//$,%,^
r0=1;r1=1;r2=0;r3=1;
if(c3==0 && r2==0){
lcddata(‘$’);P1=0xFB;delay(10000);
if(c3==0 && r2==0){
cmd(0x10); lcddata(‘%’);P1=0xFB;delay(10000);
if(c3==0 && r2==0){
cmd(0x10); lcddata(‘^’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//&,*
r0=1;r1=1;r2=1;r3=0;
if(c0==0 && r3==0){
lcddata(‘&’);P1=0xF7;delay(10000);
if(c0==0 && r3==0){
cmd(0x10); lcddata(‘*’);P1=0xF7;delay(10000);
} P1=0xFF;
}
//(,)
r0=1;r1=1;r2=1;r3=0;
if(c1==0 && r3==0){
lcddata(‘(‘);P1=0xF7;delay(10000);
if(c1==0 && r3==0){
cmd(0x10); lcddata(‘)’);P1=0xF7;delay(10000);
} P1=0xFF;
}
//-,+
r0=1;r1=1;r2=1;r3=0;
if(c2==0 && r3==0){
lcddata(‘-‘);P1=0xF7;delay(10000);
if(c2==0 && r3==0){
cmd(0x10); lcddata(‘+’);P1=0xF7;delay(10000);
}P1=0xFF;
}
///,*
r0=1;r1=1;r2=1;r3=0;
if(c3==0 && r3==0){
lcddata(‘/’);P1=0xF7;delay(10000);
if(c3==0 && r3==0){
cmd(0x10); lcddata(‘*’);P1=0xF7;delay(10000);
} P1=0xFF; }
c=’E’;
}
}
void keypad(){
unsigned char c=’t’;
while(c!=’E’){
//a,b,c,1
r0=0;r1=1;r2=1;r3=1;
if(c0==0 && r0==0){
lcddata(‘a’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘b’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘c’);P1=0xFE;delay(10000);
if(c0==0 && r0==0){
cmd(0x10); lcddata(‘1’);P1=0XFE;delay(10000);
}
}
}
P1=0xFF;
}
r0=0;r1=1;r2=1;r3=1; //d,e,f,2
if(c1==0 && r0==0){
lcddata(‘d’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘e’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘f’);P1=0xFE;delay(10000);
if(c1==0 && r0==0){
cmd(0x10); lcddata(‘2’);P1=0xFE;delay(10000); }
}
} P1=0xFF;
}
//g,h,i,3
r0=0;r1=1;r2=1;r3=1;
if(c2==0 && r0==0){
lcddata(‘g’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘h’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘i’);P1=0xFE;delay(10000);
if(c2==0 && r0==0){
cmd(0x10); lcddata(‘3’);P1=0xFE;delay(10000); } }
} P1=0xFF;
}
//j,k,l,4
r0=0;r1=1;r2=1;r3=1;
if(c3==0 && r0==0){
lcddata(‘j’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘k’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘l’);P1=0xFE;delay(10000);
if(c3==0 && r0==0){
cmd(0x10); lcddata(‘4’);P1=0xFE;delay(10000); } }
} P1=0xFF;
}
//m,n,o,5
r0=1;r1=0;r2=1;r3=1;
if(c0==0 && r1==0){
lcddata(‘m’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘n’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘o’);P1=0xFD;delay(10000);
if(c0==0 && r1==0){
cmd(0x10); lcddata(‘5’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//p,q,r,6
r0=1;r1=0;r2=1;r3=1;
if(c1==0 && r1==0){
lcddata(‘m’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘n’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘o’);P1=0xFD;delay(10000);
if(c1==0 && r1==0){
cmd(0x10); lcddata(‘5’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//s,t,u,7
r0=1;r1=0;r2=1;r3=1;
if(c2==0 && r1==0){
lcddata(‘s’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘t’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘u’);P1=0xFD;delay(10000);
if(c2==0 && r1==0){
cmd(0x10); lcddata(‘7’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//v,w,x,8
r0=1;r1=0;r2=1;r3=1;
if(c3==0 && r1==0){
lcddata(‘v’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘w’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘x’);P1=0xFD;delay(10000);
if(c3==0 && r1==0){
cmd(0x10); lcddata(‘8’);P1=0xFD;delay(10000); } }
} P1=0xFF;
}
//y,z,9
r0=1;r1=1;r2=0;r3=1;
if(c0==0 && r2==0){
lcddata(‘y’);P1=0xFB;delay(10000);
if(c0==0 && r2==0){
cmd(0x10); lcddata(‘z’);P1=0xFB;delay(10000);
if(c0==0 && r2==0){
cmd(0x10); lcddata(‘9’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//0,-,>
r0=1;r1=1;r2=0;r3=1;
if(c1==0 && r2==0){
lcddata(‘0’);P1=0xFB;delay(10000);
if(c1==0 && r2==0){
cmd(0x10); lcddata(‘-‘);P1=0xFB;delay(10000);
if(c1==0 && r2==0){
cmd(0x10); lcddata(‘>’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//!,@,#
r0=1;r1=1;r2=0;r3=1;
if(c2==0 && r2==0){
lcddata(‘!’);P1=0xFB;delay(10000);
if(c2==0 && r2==0){
cmd(0x10); lcddata(‘@’);P1=0xFB;delay(10000);
if(c2==0 && r2==0){
cmd(0x10); lcddata(‘#’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//$,%,^
r0=1;r1=1;r2=0;r3=1;
if(c3==0 && r2==0){
lcddata(‘$’);P1=0xFB;delay(10000);
if(c3==0 && r2==0){
cmd(0x10); lcddata(‘%’);P1=0xFB;delay(10000);
if(c3==0 && r2==0){
cmd(0x10); lcddata(‘^’);P1=0xFB;delay(10000); }
} P1=0xFF;
}
//&,*
r0=1;r1=1;r2=1;r3=0;
if(c0==0 && r3==0){
lcddata(‘&’);P1=0xF7;delay(10000);
if(c0==0 && r3==0){
cmd(0x10); lcddata(‘*’);P1=0xF7;delay(10000);
} P1=0xFF;
}
//(,)
r0=1;r1=1;r2=1;r3=0;
if(c1==0 && r3==0){
lcddata(‘(‘);P1=0xF7;delay(10000);
if(c1==0 && r3==0){
cmd(0x10); lcddata(‘)’);P1=0xF7;delay(10000);
} P1=0xFF;
}
//-,+
r0=1;r1=1;r2=1;r3=0;
if(c2==0 && r3==0){
lcddata(‘-‘);P1=0xF7;delay(10000);
if(c2==0 && r3==0){
cmd(0x10); lcddata(‘+’);P1=0xF7;delay(10000);
}P1=0xFF;
}
///,*
r0=1;r1=1;r2=1;r3=0;
if(c3==0 && r3==0){
lcddata(‘/’);P1=0xF7;delay(10000);
if(c3==0 && r3==0){
cmd(0x10); lcddata(‘*’);P1=0xF7;delay(10000);
} P1=0xFF; }
c=’E’;
}
}
Keypad() is the function which i called from the main function.Than the unsigned char c=’t’ is a variable of character data type .I used it here to run my while() loop .The while loop statement is while(c!=’E’) which is true and it jumps in the while loop statements.
Now suppose you have pressed any key the statements are identifying the pressed key. How they are identifying them when the key is pressed the corresponding row and coulomb becomes grounded.Now i am grounding the the first row and making all the other high r0=0;r1=1;r2=1;r3=1; and then i am checking if row 0 and coulomb 0 are grounded if(c0==0 && r0==0) if they are grounded I send lcddata(‘a’) character a to lcd and then making P1=0xFE .This P1=0xFE is some thing you must understand this means that i am sending a hexadecimal value to P1 and to my P1 the keypad is connected .
Now if i convert this hexadecimal value to binary it is 11111110 .Actually this eight bit value is going to port P1. since each port have eight pins so this 8-bit value appears across the P1 pins making c0-c1-c2-c3-r1-r2-r3 high 1 and the r0 low 0 look at the first digit.Then their is delay(10000). This delay is very important now if you press the first key again in this delay time the next condition if(c0==0 && r0==0) is full filled and lcddata(‘b’) executes and again we made c0-c1-c2-c3-r1-r2-r3 high 1 and r0 low 0 .Thus this P1=0xFE is bringing our row and coulombs back in initial condition. For next check if the key is again pressed in required time.same like you are writing message in your cell phone and if you dont press the same key in time specified for it to display next character. Then the first character appears next. Like you pressed ‘a’ it appears and then quickly pressed the same button then ‘b’ appears but if you pressed ‘a’ and then pressed the same key after 20 second then ‘a’ again appear except ‘b’ which you need . The same applies here if you dont press the key in delay(10000) time. the output will not be what you want.The first key is printing a,b,c,1 in dadicated times and after 1 again if you press it it will print ‘a’. After this I am making port 1 P1 P1=0xFF which means P1=11111111 Thus making all rows and coulombs high 1 again . To identify next button pressed.
AT The END OF THE FOR LOOP I AM MAKING c=’E’ ,THUS MAKING WHILE CONDITION FALSE SO THE LOOP BREAKS.NOTE I AM CALLING keypad() FUNCTION IN MAIN UNDER FOLLOWING CONDITION.
while(1)
{keypad();}
SO THAT YOU CAN PRINT AS MANY CHARACTERS AS YOU WANT.IF YOU USE THIS CODE MAKE SURE YOU ARE CALLING THE FUNCTION keypad().
Some statements meaning
Now suppose you have pressed any key the statements are identifying the pressed key. How they are identifying them when the key is pressed the corresponding row and coulomb becomes grounded.Now i am grounding the the first row and making all the other high r0=0;r1=1;r2=1;r3=1; and then i am checking if row 0 and coulomb 0 are grounded if(c0==0 && r0==0) if they are grounded I send lcddata(‘a’) character a to lcd and then making P1=0xFE .This P1=0xFE is some thing you must understand this means that i am sending a hexadecimal value to P1 and to my P1 the keypad is connected .
Now if i convert this hexadecimal value to binary it is 11111110 .Actually this eight bit value is going to port P1. since each port have eight pins so this 8-bit value appears across the P1 pins making c0-c1-c2-c3-r1-r2-r3 high 1 and the r0 low 0 look at the first digit.Then their is delay(10000). This delay is very important now if you press the first key again in this delay time the next condition if(c0==0 && r0==0) is full filled and lcddata(‘b’) executes and again we made c0-c1-c2-c3-r1-r2-r3 high 1 and r0 low 0 .Thus this P1=0xFE is bringing our row and coulombs back in initial condition. For next check if the key is again pressed in required time.same like you are writing message in your cell phone and if you dont press the same key in time specified for it to display next character. Then the first character appears next. Like you pressed ‘a’ it appears and then quickly pressed the same button then ‘b’ appears but if you pressed ‘a’ and then pressed the same key after 20 second then ‘a’ again appear except ‘b’ which you need . The same applies here if you dont press the key in delay(10000) time. the output will not be what you want.The first key is printing a,b,c,1 in dadicated times and after 1 again if you press it it will print ‘a’. After this I am making port 1 P1 P1=0xFF which means P1=11111111 Thus making all rows and coulombs high 1 again . To identify next button pressed.
AT The END OF THE FOR LOOP I AM MAKING c=’E’ ,THUS MAKING WHILE CONDITION FALSE SO THE LOOP BREAKS.NOTE I AM CALLING keypad() FUNCTION IN MAIN UNDER FOLLOWING CONDITION.
while(1)
{keypad();}
SO THAT YOU CAN PRINT AS MANY CHARACTERS AS YOU WANT.IF YOU USE THIS CODE MAKE SURE YOU ARE CALLING THE FUNCTION keypad().
Some statements meaning
- P1=0xFF ->|11111111| -> Making all rows and coulombs high 1.
- P1=0xFE ->|11111110| -> Making row 1,2,3 and coulomb 0,1,2,3 high and row 0 low.
- P1=0xFD ->|11111101| -> Making row 0,2,3 and coulomb 0,1,2,3 high and row 1 low.
- P1=0xFB ->|11111011| -> Making row 0,1,3 and coulomb 0,1,2,3 high and row 2 low.
- P1=0xF7 ->|11110111| -> Making row 0,1,2 and coulomb 0,1,2,3 high and row 3 low.
You can replace
- P1=0xFF with r0=1;r1=1;r2=1;r3=1;c0=1;c1=1;c2=1;c3=1;
- P1=0xFE with r0=0;r1=1;r2=1;r3=1;c0=1;c1=1;c2=1;c3=1;
- P1=0xFD with r0=1;r1=0;r2=1;r3=1;c0=1;c1=1;c2=1;c3=1;
- P1=0xFB with r0=1;r1=1;r2=0;r3=1;c0=1;c1=1;c2=1;c3=1;
- P1=0xF7 with r0=1;r1=1;r2=1;r3=0;c0=1;c1=1;c2=1;c3=1;
Keypad buttons specified characters,digits and arithmetic operates.
Button 1 = prints a-b-c-1
Button 2 = prints d-e-f-2
Button 3 = prints g-h-i-3
Button 4 = prints j-k-l-4
Button 5 = prints m-n-o-5
Button 6 = prints p-q-r-6
Button 7 = prints s-t-u-7
Button 8 = prints v-w-x-8
Button 9 = prints y-z-9
Button 10 = prints 0 , – , >
Button 11 = prints !-@-#
Button 12 = prints $-%-^
Button 13 = prints &-*
Button 14 = prints (-)
Button 15 = prints -,+
Button 16 = prints /-*
Filed Under: Knowledge Share, Microcontroller Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.