vead
Full Member level 5
- Joined
- Nov 27, 2011
- Messages
- 285
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- india
- Activity points
- 3,815
Hello
I need to display characters on screen using matrix keypad. if i press key on keypad characters should be display on the screen. I wrote code to print characters on screen using keypad but when I press any key program is not showing anything on the screen.
I think there is problem in my keypad function. I am trying to figure out what's wrong in keypad function. Is it correct way to read keypad or I need batter function then this function
This code is only for keypad
I need to display characters on screen using matrix keypad. if i press key on keypad characters should be display on the screen. I wrote code to print characters on screen using keypad but when I press any key program is not showing anything on the screen.
I think there is problem in my keypad function. I am trying to figure out what's wrong in keypad function. Is it correct way to read keypad or I need batter function then this function
This code is only for keypad
Code:
/* Processor: AT89C51 */
/* Compiler: Keil for 8051*/
/* LCD 16*2 and keypad 4*4 */
#include <reg51.h>
#define Port P0 //keypad connected to port P0
#define C1 P1^0
#define C2 P0^1
#define C3 P0^2
#define C4 P0^3
char get_key()
{
unsigned char i, k, key=0;
k = 1;
for( i = 0; i < 4; i++) //loop for 4 rows
{
keyport &=~ (0x80 >> i); //to make rows low 1 by 1
if(!C1) //check if key1 is pressed
{
key = k+0; //set key number
while(!C1); //wait for release
return key; //return key number
}
if(!C2) //check if key2 is pressed
{
key = k+1; //set key number
while(!C2); //wait for release
return key; //return key number
}
if(!C3) //check if key3 is pressed
{
key = k+2; //set key number
while(!C3); //wait for release
return key; //return key number
}
if(!C4) //check if key4 is pressed
{
key = k+3; //set key number
while(!C4); //wait for release
return key; //return key number
}
k = k + 4; //next row key number
keyport |= 0x80 >> i; //make the row high again
}
return 0; //return 0 if no key pressed
}
void main()
{
unsigned char keypress;
Port =0xff; //all keypad pins high
while(1)
{
keypress=get_key();
if(keypress)
Lcd_Data(keypress);
}
}
Last edited: