Motoo Tanaka
/
matrix_key_4x4
A simple 4x4 matrix key pad test program
matrix_key_4x4.cpp
- Committer:
- Rhyme
- Date:
- 2017-05-11
- Revision:
- 1:be0aa2900453
- Child:
- 2:bac691bab19c
File content as of revision 1:be0aa2900453:
/** * matrix key scan program * * @note assuming key pad of 16 keys * @note 4 x 4 matrix scan */ #include "mbed.h" #if defined (TARGET_KL25Z) #define ROW0 PTC9 #define ROW1 PTC8 #define ROW2 PTA5 #define ROW3 PTA4 #define COL0 PTA12 #define COL1 PTD4 #define COL2 PTA1 #define COL3 PTA2 #endif #define NUM_COL 4 #define NUM_ROW 4 #define NUM_KEY (NUM_COL * NUM_ROW) int key[ NUM_KEY ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ; unsigned char key_char[NUM_KEY] = { '1', '4', '7', '*', '2', '5', '8', '0', '3', '6', '9', '#', 'A', 'B', 'C', 'D' } ; DigitalIn *col[NUM_COL] ; DigitalOut *row[NUM_ROW] ; void init_hardware(void) { col[3] = new DigitalIn(PTC11, PullUp) ; col[2] = new DigitalIn(PTC10, PullUp) ; col[1] = new DigitalIn(PTC6, PullUp) ; col[0] = new DigitalIn(PTC5, PullUp) ; row[3] = new DigitalOut(PTC4, 1) ; row[2] = new DigitalOut(PTC3, 1) ; row[1] = new DigitalOut(PTC0, 1) ; row[0] = new DigitalOut(PTC7, 1) ; } int scan_key(int key[]) { int r, c ; int count = 0 ; for (r = 0 ; r < NUM_ROW ; r++ ) { row[r]->write(0) ; for (c = 0 ; c < NUM_COL ; c++ ) { if (col[c]->read() == 0) { key[r * NUM_COL + c] = 1 ; count++ ; } else { key[r * NUM_COL + c] = 0 ; } } row[r]->write(1) ; } return(count) ; } void print_key(int key[]) { int i ; for (i = 0 ; i < NUM_KEY ; i++ ) { if (key[i]) { printf("%c", key_char[i]) ; } } } int main() { int count ; init_hardware() ; printf("\n=== Matrix Key Test %s ===\n", __DATE__) ; while(1) { count = scan_key(key) ; if (count) { print_key(key) ; fflush(stdout) ; wait(0.2) ; } wait(0.1) ; } }