4X4 keypad to pic

Status
Not open for further replies.
i need to display keypad value on uart. please chk embedded c code. i wrote the code but its not working correctly plz chk the code given below:
#define keyport PORTD //keypad connected to PORTD
#define col1 RD0 //column 1
#define col2 RD1 //column 2
#define col3 RD2 //column 3
#define col4 RD3 //column 4
#define TRUE 1 //some defines
#define FALSE 0
const unsigned char character_from_keypad[4][4]={
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
void key_init()
{
TRISD=0x0F; //make Rows as o/p and cols are i/p
keyport=0xFF;
}

unsigned char get_key()
{
unsigned char row,pressed_col,keypad_value=0;

for(row=0;row<4;row++) //loop for 4 rows
{
keyport &= ~(0x80>>row); //to make rows low 1 by 1
if(!col1) //check if col1 is pressed
{
while(!col1)
{
continue;
}
DelayMs(50); //wait for release

pressed_col= col1; //set key number

keypad_value=character_from_keypad[row][pressed_col];// to read the value from keypad

return keypad_value; //return key number
}
else if(!col2) //check if col2 is pressed
{
while(!col2)
{
continue;
}
DelayMs(50); //wait for release

pressed_col=col2; //set key number

keypad_value=character_from_keypad[row][pressed_col];

return keypad_value; //return key number
}
else if(!col3) //check if col3 is pressed
{
while(!col3)
{
continue;
}
DelayMs(50); //wait for release

pressed_col=col3; //set key number

keypad_value=character_from_keypad[row][pressed_col];

return keypad_value; //return key number
}

else if(!col4) //check if key4 is pressed
{
while(!col4)
{
continue;
}
DelayMs(50); //wait for release

pressed_col=col4; //set key number

keypad_value=character_from_keypad[row][pressed_col];

return keypad_value; //return key number
}
else
{
//pressed_col+=4; //next row key number
keyport |= 0x80>>row; //make the row high again

}
}
return FALSE; //return false if no key pressed
}

void main()
{
unsigned char keypad_input;


init_comms();

key_init();


while(1)
{
keypad_input=get_key();
putch(keypad_input);

}

thanks
 
use the MM74C922N keypad encoder. its makes it so much easier, because by configuring the debounce and scan time periods you dont have to bother about them. all tht you hav to do is read the data from the IC...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…