??? 07/01/06 14:05 Modified: 07/01/06 14:09 Read: times |
#119511 - keypad scanner in C Responding to: ???'s previous message |
I did this code ages ago to scan a keypad.
If you can C then have a look the logic is you output a zero to each column and check the rows. At the end of 4th column you have the switch states in keycode. I debounce all the switches together as follows: debounce(keyScan()); /****************************************************************************** * NAME : keyScan * DESCRIPTION : This function scans switch matrix and stores it in keyCode * INPUT : None * OUTPUT : unsigned short integer with each bit indicating a switch ******************************************************************************/ unsigned short int keyScan(void) { unsigned short int keyCode; // where all 16 switches are stored unsigned char col, colCounter; col = 0x0E; // initialise to col 0 keyCode = 0; for(colCounter = 0; colCounter < 4; colCounter++, col++) { P0 = (P0 & 0x0f0) | (col & 0x0f); // preserve P0.4 - P0.7 states keyCode = keyCode << 4 & 0x0ffff; // 0ffff to hush lint up keyCode |= (0x0f & P1); col = col << 1 & 0x0f; } return(~keyCode); } |