45/553 Microcontroller Applications Week 7 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences
MISC Stuff Keypad revisited 25 2
Optional Required Reading Reminiscences of the VLSI Revolution: How a series of failures triggered a paradigm shift in digital design By Lynn Conway IEEE SOLID-STATE CIRCUITS MAGAZINE, vol.4, no.4, pp.8-3, Dec. 22 http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=639323 45/553 3
Keypad Revisited 45/553 4
6-Button Keypad 42 Series by C&K Components, Inc. Web Site: www.ckcorp.com Matrix connections based on which key is pressed Time multiplex pins -4 while reading pins 5-8 A keypad controller IC can be purchased to act as a peripheral 25 5
6-Button Keypad Series 96 by Grayhill, Inc. Web Site: www.grayhill.com Matrix connections based on which key is pressed Time multiplex pins -4 while reading pins 5-8 A keypad controller IC can be purchased to act as a peripheral 25 6
Text Keypad Circuitry PA7 PA6 PA5 PA4 Selected keys HCS2 MCU PA7, 4, 8, C,, 5, 9, D, 2, 6, A, E, and 3 and 7 and B and F Table 7.6 Sixteen-key keypad row selections outputs PA6 PA5 PA4 PA3 3 7 B F inputs PA2 PA 2 6 5 A 9 E D PA 4 8 C K V CC 25 7 Figure 7.4 Sixteen-key keypad connected to the HCS2
Text Keypad Scanning Keypad scanning is usually performed row-by-row or column-by-column A 6-key keypad can be easily interfaced using any available 8-bit I/O port For the keypad application the upper four pins of the port should be configured for output and the lower four pins of the port should be configured for input (with pull-ups) The rows and columns of a keypad are simply conductors The keypad interface setup to HCS2 PortA is as shown in the next slide 25 8
Text Keypad Operation Whenever a key switch is pressed, the corresponding row and column are shorted together In order to distinguish the row and column of the key pressed Scan a zero through the columns If an input row becomes zero, the key pressed must be connected to the zeroed column. Figure it out to determine the key! PA7 PA6 PA5 PA4 Selected keys, 4, 8, C,, 5, 9, D, 2, 6, A, E, and 3 and 7 and B and F Table 7.6 Sixteen-key keypad row selections 25 9
Class Keypads 45/553
Keypad Input vs. Key Pressed (When PA7-PA not all s) Key PA7 PA6 PA5 PA4 PA3 PA2 PA PA Key Driven Outputs Inputs Value 7 2 4 3 4 F 5 8 6 5 7 2 8 E 9 9 A 6 B 3 C D D C E B F A 25
Text Keypad Code Function # define keypad PTA # define keypad_dir DDRA char getkey (void) char rmask, cmask, row, col; char temp, keycode; keypad_dir = xf; keypad = xf; while () rmask = xef; for (row = ; row < 4; row++) cmask = x; keypad = rmask; } } } // configure lower four pins for input // write upper four pins // select the current row for (col = ; col < 4; col++) if (!(keypad & cmask)) // key switch detected pressed delaybyms(); if(!(keypad & cmask)) // check the same key again keycode = row * 4 + col; if (keycode < ) return (x3 + keycode); else return (x37 + keycode); } } cmask = cmask << ; } rmask = (rmask << ) xf; // sequence of xef, xdf, xbf and x 7F 45/553 2
Rethinking the Function # define keypad PORTA # define keypad_dir DDRA char getkey(void) char key_array[4][4] =, 7, 4,, 5, 8, 5, 2, 4, 9, 6, 3, 3, 2,, }; char rmask, cmask, row, col; } cmask = xef; // init keypad scan for col = ; col < 4; col++) // rmask = x; keypad = cmask; // Test the th row for (row = ; row < 4; row++) if (!(keypad & rmask)) // key switch detected pressed keypad = xff; // standby keypad values return (key_array[col][row]); } rmask = rmask << ; } cmask = (cmask << ) xf; // sequence of xef, xdf, xbf and x 7F } keypad = xff; // standby keypad values return (xff); 45/553 3
Keypad Main Code If getkey() is called If a key is pressed, the value If a key is not pressed, xff There is no debouncing Requirements Debounce delay checking Create some type of count with a threshold test Only accept key when it is released Place the key value in a key-press buffer to be operated on Save the value at a pointer location to an array Provide a status flag of the keypad operational state Key being pressed, not yet valid Key being pressed, delay is long enough Key value received and is valid Initialize interface and all variables as needed 45/553 4
Keypad Main Code while() // Do something here asm("nop"); // Key scanning designed for every while loop // Looking for the key to be pressed and released // if(check_keypad_flag) current_key = getkey(); // retrieve a key value if(last_key <> xff) key_count++; // count the key press loops key_status = x; // status is x invalid press if(key_count>key_thresh) // accept if greater key_status = x2; // status is x2 valid press if(current_key == xff) key_status = x3; // status is x3 saved value *key_ptr++ = last_key; } } } else key_count = ; key_status = x; } last_key = current_key; } } } 45/553 5
Keypad Main Code Misc # define keypad PORTA # define keypad_dir DDRA # define KEY_THRESH void main(void) int char char char char int ii; check_keypad_flag; key_status, last_key, current_key; key_input[]; *key_ptr; key_count; 45/553 COPCTL=x; asm("sei"); // Initilize keypad and key variables keypad_init(); // set up port pins last_key = xff; key_count = ; key_status = x; key_ptr = &key_input[]; for(ii=; ii<;ii++) // initilize key_input key_input[ii]=; } asm("cli"); 6
Rethinking Code Although you have completed previous labs. It may be useful to review what has previously done so that is more useful for current or future labs. Is your keypad useful for inputting a string of numbers or hex digits? DAC output defined by keypad. If the four function calculator is Project #2. Can similar concepts be applied to the 5x7 display? 45/553 7