17
ECE 4510/5530 Microcontroller Applications Week 7 Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences

ECE 4510/5530 Microcontroller Applications Week 7

  • Upload
    others

  • View
    4

  • Download
    1

Embed Size (px)

Citation preview

ECE 4510/5530Microcontroller Applications

Week 7

Dr. Bradley J. BazuinAssociate Professor

Department of Electrical and Computer EngineeringCollege of Engineering and Applied Sciences

ECE 2510 2

MISC Stuff

• Keypad revisited

Optional Required Reading

Reminiscences of the VLSI Revolution: How a series of failures triggered a paradigm shift in digital designBy Lynn ConwayIEEE SOLID-STATE CIRCUITS MAGAZINE, vol.4, no.4, pp.8-31, Dec. 2012

• http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6393023

ECE 4510/5530

3

Keypad Revisited

ECE 4510/5530

4

ECE 2510 5

16-Button Keypad

• 4200 Series by C&K Components, Inc. Web Site: www.ckcorp.com– Matrix connections based on which key is pressed– Time multiplex pins 1-4 while reading pins 5-8– A keypad controller IC can be purchased to act as a peripheral

ECE 2510 6

16-Button Keypad

• Series 96 by Grayhill, Inc. Web Site: www.grayhill.com– Matrix connections based on which key is pressed– Time multiplex pins 1-4 while reading pins 5-8– A keypad controller IC can be purchased to act as a peripheral

ECE 2510 7

Text Keypad Circuitry

A

B

C

D

E

F

0

1

2

3

4

5

6

7

8

9

10K

VC C

PA7

PA6

PA5

PA4

PA3

PA2

PA1

PA0

HCS12 MCU

Figure 7 .41 S ixteen-key keypad connected to the H C S12

outputs

inputs

PA7 PA6 PA5 PA4 Selected keys

1110

1101

1011

0111

0,4,8,C,

1,5,9,D,

2,6,A,E,

and 3and 7and Band F

Table 7.16 Sixteen-key keypad row selections

ECE 2510 8

Text Keypad Scanning

• Keypad scanning is usually performed row-by-row or column-by-column

• A 16-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 HCS12 PortA is as shown in

the next slide

ECE 2510 9

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

1110

1101

1011

0111

0,4,8,C,

1,5,9,D,

2,6,A,E,

and 3and 7and Band F

Table 7.16 Sixteen-key keypad row selections

Class Keypads

ECE 4510/5530

10

Keypad Input vs. Key Pressed(When PA7-PA0 not all 1’s)

ECE 2510 11

Key PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 KeyDriven Outputs Inputs Value

0 1 1 1 0 1 1 1 0 01 1 1 1 0 1 1 0 1 72 1 1 1 0 1 0 1 1 43 1 1 1 0 0 1 1 1 14 1 1 0 1 1 1 1 0 F5 1 1 0 1 1 1 0 1 86 1 1 0 1 1 0 1 1 57 1 1 0 1 0 1 1 1 28 1 0 1 1 1 1 1 0 E9 1 0 1 1 1 1 0 1 9A 1 0 1 1 1 0 1 1 6B 1 0 1 1 0 1 1 1 3C 0 1 1 1 1 1 1 0 DD 0 1 1 1 1 1 0 1 CE 0 1 1 1 1 0 1 1 BF 0 1 1 1 0 1 1 1 A

Text Keypad Code Function# define keypad PTA# define keypad_dir DDRAchar getkey (void){

char rmask, cmask, row, col;char temp, keycode;keypad_dir = 0xF0; // configure lower four pins for inputkeypad = 0xF0; // write upper four pins while (1) {

rmask = 0xEF;for (row = 0; row < 4; row++){

cmask = 0x01;keypad = rmask; // select the current rowfor (col = 0; col < 4; col++){

if (!(keypad & cmask)){ // key switch detected presseddelayby10ms(1);if(!(keypad & cmask)){ // check the same key again

keycode = row * 4 + col;if (keycode < 10)

return (0x30 + keycode);else

return (0x37 + keycode);} }cmask = cmask << 1;

}rmask = (rmask << 1) | 0x0F; // sequence of 0xEF, 0xDF, 0xBF and 0x 7F

} } }

ECE 4510/5530

12

Rethinking the Function# define keypad PORTA# define keypad_dir DDRAchar getkey(void){

char key_array[4][4] = {0, 7, 4, 1, 15, 8, 5, 2, 14, 9, 6, 3, 13, 12, 11, 10};char rmask, cmask, row, col;

cmask = 0xEF; // init keypad scanfor col = 0; col < 4; col++) // {

rmask = 0x01;keypad = cmask; // Test the 0th rowfor (row = 0; row < 4; row++){

if (!(keypad & rmask)) // key switch detected pressed{

keypad = 0xFF; // standby keypad valuesreturn (key_array[col][row]);

}rmask = rmask << 1;

}cmask = (cmask << 1) | 0x0F; // sequence of 0xEF, 0xDF, 0xBF and 0x 7F

}keypad = 0xFF; // standby keypad valuesreturn (0xFF);

}

ECE 4510/5530

13

Keypad Main Code

• If getkey() is called– If a key is pressed, the value– If a key is not pressed, 0xFF– 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 neededECE 4510/5530

14

Keypad Main Codewhile(1){

// Do something hereasm("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 <> 0xFF) {key_count++; // count the key press loopskey_status = 0x01; // status is 0x01 invalid pressif(key_count>KEY_THRESH) { // accept if greater

key_status = 0x02; // status is 0x02 valid pressif(current_key == 0xFF) {

key_status = 0x03; // status is 0x03 saved value*key_ptr++ = last_key;

} } }else {

key_count = 0;key_status = 0x00;

}last_key = current_key;

} } }

ECE 4510/5530

15

Keypad Main Code Misc# define keypad PORTA# define keypad_dir DDRA# define KEY_THRESH1000

void main(void){

int ii;char check_keypad_flag;

char key_status, last_key, current_key;char key_input[10];char *key_ptr;int key_count;

COPCTL=0x00;asm("sei");

// Initilize keypad and key variableskeypad_init(); // set up port pinslast_key = 0xFF;key_count = 0;key_status = 0x00;key_ptr = &key_input[0]; for(ii=0; ii<10;ii++) // initilize key_input{

key_input[ii]=0;}

asm("cli");ECE 4510/5530

16

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?

ECE 4510/5530

17