Athar Rasul
Newbie level 4
hi,
I am interfacing the keypad on a single port using B0-B7. I am having problem with the key detection.. kindly help.
I am interfacing the keypad on a single port using B0-B7. I am having problem with the key detection.. kindly help.
Code:
int keypad(){
int i; int key=0;
output_B(0x01); // Start with column 1
while((input_B() & 0xF0) == 0){ // While no key pressed
output_B(input_B() << 1); // next column
key++; // column number
if(key==4){
output_B(0x01); // Back to column 1
key = 0;
}
}
delay_ms(20); // Switch debounce
for(i=0x10; i!=0; i<<=1){ // Find the key pressed
if((input_B() & i) != 0)break;
key=key+4;
}
output_B(0x0F);
while((input_B() & 0xF0) != 0); // Wait until key released
delay_ms(20); // Switch debounce
return (key); // Return key number
}