Example 6 Hex Keypad Lecture L3.2
miniDragon+ 2 pushbutton switches Serial cable A/D Pot I/O headers Run/Load switch Reset button 7-segment display Power plug Keypad header
4 x 4 Hex Keypad
4 x 4 Hex Keypad
int key_scan(void) int key_scan(void){ const char keycodes[] = { 0xD7,0xEE,0xDE,0xBE, 0xED,0xDD,0xBD,0xEB, 0xDB,0xBB,0x7E,0x7D, 0x7B,0x77,0xE7,0xB7 }; int i,j, key; char readback; int found; i = 0; key = 16; // return 16 if no key pressed found = 0; while((i < 16) && (found == 0)){ PORTA = keycodes[i]; // write keycode to PORTA for(j = 0; j<10; j++){ } // wait a bit readback = PORTA; if(readback == keycodes[i]){ // read back PORTA, if same key = i; // get key number found = 1; // and exit loop } else i++; // else check next key return key; // return key int key_scan(void)
void wait_for_keyup(void) int get_key(void) int get_key(void){ int key; do { key = key_scan(); } while(key == 16); return key; void wait_for_keyup(void){ while(key_scan() != 16){ void wait_for_keyup(void)
Example 6a // Example 6a: 4 x 4 keypad in C #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" int key_scan(void); int get_key(void); void wait_for_keyup(void); void main(void) { int c; PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display DDRA = 0x0F; // Port A: A7:A4 inputs, A3:A0 outputs while(1){ c = get_key(); seg7dec(c); wait_for_keyup(); }
Example 6
Example 6b // Example 6b: 4 x 4 keypad using C function calls #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" void main(void) { char c; PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); // enable 7-segment display keypad_enable(); while(1){ c = getkey(); seg7dec(c); wait_keyup(); }