7-1 Keypad Scanning interface Used for interfacing push-button(momentary) switches Used for large number(>8) push-buttons Relies on matrix arrangement of push-buttons F E 0 89D AB C ROWS Inputs COLUMNS Outputs PORT
7-2 Port F E 0 89D AB C bit 4 bit 7 bit 5 bit 3 bit 6 bit 2 bit 1 bit 0 Infineon 167 Target board connections BASIC APPROACH TO SCANNING Set port direction bits 4-7 I/p, bits 0-3 o/p. Initial column_scan = = 0x08 next column_scan value = then and finally Use 'C' >> operator which shift bits of an integer to the right At each stage read in all rows and if rows are non zero then find which of the four bits is a '1'
7-3 static char keypad_scan(void) { const char key_code[] = "FEDC369B A"; // keypad layout unsigned char row, row_scan, column, column_scan; column_scan = 0x08; for(column = 0; column > 4; if(row_scan ! = 0) // Any key pressed? { row = 0; //Find which bit in row while((row_scan % 2) == 0) // test least significant bit { row++; row_scan >>= 1; } return(key_code[column * 4 + row]); // return key code } // end if column_scan >>= 1; //ready for next column } // end for return 0; // No key pressed }