430 likes | 449 Views
Learn about matrix keypad scanning with a microcontroller interface, key decoding, and debounce techniques in ECE 447 lectures.
E N D
ECE 447: Lecture 10 ECE 447: Lecture 12 Keypads
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 1 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 0 1 1 1
Matrix Keypad - Scanning ECE 447: Matrix Keypad - Scanning 1 0 1 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 0 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 1 0
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning PC0 PC1 PC2 PC3 PC4 PC5 PC6
ECE 447: Matrix Key Codes F0 11110000 outputs inputs
ECE 447: Key Scanning Function while (1) { do ( PORTC = "XXXX0000" while ( PORTC = "X111XXXX"); c=decode_key(); ..... do something dependent on the key being pressed... PORTC = "XXXX0000" while (PORTC != "X111XXXX") /* do nothing */ } Wait until any key pressed Decode the key being pressed Wait until all keys released
ECE 447: Key Scanning Function int decode_key() { PORTC = "XXXX1110" if ( PORTC != "X111XXXX”) return decode_output (PORTC, 1) else { PORTC = "XXXX1101" if ( PORTC != "X111XXXX") return decode_output(PORTC, 2) } else { PORTC = "XXXX1011" if ( PORTC != "X111XXXX") return decode_output (PORTC, 3) } else { PORTC = "XXXX0111" if ( PORTC != "X111XXXX") return decode_output (PORTC, 4) else return NONE_KEY_PRESSED; } check Row 1 check Row 2 check Row 3 check Row 4
Possible Key Decoding Procedure (3) ECE 447: Key Scanning Function Decoding a column XCCCXXXX • Three bits of interest • Three combinations valid (“110”, “101”, “011”) • Decoding column possible
F0 11110000 inputs ECE 447: Matrix Key Codes
F0 11110000 outputs ECE 447: Matrix Key Codes
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 0 0 outputs 0 0 1 inputs 1 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 0 0 outputs 0 0 1 inputs 1 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 0 0 outputs 0 0 1 inputs 1 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 0 0 outputs 0 0 1 inputs 0 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 outputs 1 0 1 inputs 0 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 outputs 1 0 1 inputs 0 1
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 outputs 0 1 1 inputs 1 0
ECE 447: Matrix Keypad - Scanning Matrix Keypad - Scanning 1 1 outputs 0 1 1 inputs 1 0
Simplified Key Decoding Procedure (4) ECE 447: Key Scanning Function – Alternative decode_key() { unsigned char keys = [NONE, ONE, TWO, …, TWELVE]; for (i=0, i<=12, i++) { PORTC = keys[i] if (PORTC == keys[i] return i; } return UNKNOWN; }
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE NONE F0 11110000 ECE 447: Matrix Key Codes
ECE 447: Key Scanning Function – No Debounce while (1) { do ( PORTC = "XXXX0000" while ( PORTC = "X111XXXX"); c=decode_key(); ..... do something dependent on the key being pressed... PORTC = "XXXX0000" while (PORTC != "X111XXXX") /* do nothing */ } Wait until any key pressed Decode the key being pressed Wait until all keys released
ECE 447: Key Bounce Key Bouncing key bounce, tBOUNCE key bounce, tBOUNCE typically, tBOUNCE < 10 ms
ECE 447: Key Bounce KeypadBouncing key bounce, tBOUNCE key bounce, tBOUNCE typically, tBOUNCE < 10 ms NONE NONE NONE NONE
ECE 447: Key Bounce Keypad Debouncing in Software key bounce, tBOUNCE key bounce, tBOUNCE typically, tBOUNCE < 10 ms NONE NONE NONE NONE wait debouncing period wait debouncing period wait until all keys released wait until any key pressed wait until any key pressed decode_key; action dependent on the key
Key Decoding Procedure (7) ECE 447: Key Scanning Function – with Debounce while (1) { while (key_decode() == NONE) /* do nothing */ ; wait debouncing period(); c=decode_key(); ..... do something dependent on the key being pressed... while(key_decode()!=NONE) /* do nothing */ ; wait debouncing period(); } Wait until any key pressed Decode the key being pressed Wait until all keys released
ECE 447: 2 of 7 Keypad 2 out of 7 Keypad C
ECE 447: 2 of 7 Keypad Scanning 2 out of 7 Keypad - Scanning 1 1 1 0 1 0 1 0 C
ECE 447: 2 of 7 Keypad Scanning Function decode_key() { unsigned char keys = [NONE, ONE, TWO, …, TWELVE]; for (i=0, i<=12, i++) { PORTC = keys[i] if (PORTC == keys[i] return i; } return UNKNOWN; }
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE NONE ECE 447: 2 of 7 Key Codes
ECE 447: 2 of 7 Keypad Scanning Function (Simplified) decode_key() { unsigned char keys = [NONE, ONE, TWO, …, TWELVE]; code = PORTC for (i=0, i<=12, i++) { if (code == keys[i] return i; } return UNKNOWN; }
ECE 447: 2 of 7 Keypad Scanning Function while (1) { while (decode_key() == NONE) /* do nothing */ ; wait debouncing period(); c=decode_key(); ..... do something dependent on the key being pressed... while(decode_key()!=NONE) /* do nothing */ ; wait debouncing period(); } Wait until any key pressed Decode the key being pressed Wait until all keys released
ECE 447: 2 of 7 Keypad Hardware Debounce Key Debouncing in Hardware
ECE 447: 2 of 7 Keypad Hardware Debounce Key Debouncing in Hardware Capacitors associated with all pull-up resistors