21.08.2013 Views

Embedded Systems Design with the Atmel AVR Microcontroller Part II

Embedded Systems Design with the Atmel AVR Microcontroller Part II

Embedded Systems Design with the Atmel AVR Microcontroller Part II

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7.3. INPUT DEVICES 189<br />

7.3.4 KEYPADS<br />

A keypad is simply an extension of <strong>the</strong> simple switch configuration. A typical keypad configuration<br />

and interface are shown in Figure 7.4. As you can see, <strong>the</strong> keypad is simply multiple switches in <strong>the</strong><br />

same package. A hexadecimal keypad is provided in <strong>the</strong> figure. A single row of keypad switches are<br />

asserted by <strong>the</strong> microcontroller and <strong>the</strong>n <strong>the</strong> host keypad port is immediately read. If a switch has<br />

been depressed, <strong>the</strong> keypad pin corresponding to <strong>the</strong> column <strong>the</strong> switch is in will also be asserted.<br />

The combination of a row and a column assertion can be decoded to determine which key has<br />

been pressed as illustrated in <strong>the</strong> table. Keypad rows are continually asserted one after <strong>the</strong> o<strong>the</strong>r in<br />

sequence. Since <strong>the</strong> keypad is a collection of switches, debounce techniques must also be employed.<br />

The keypad may be used to introduce user requests to a microcontroller. A standard keypad<br />

<strong>with</strong> alphanumeric characters may be used to provide alphanumeric values to <strong>the</strong> microcontroller<br />

such as providing your personal identification number (PIN) for a financial transaction. However,<br />

some keypads are equipped <strong>with</strong> removable switch covers such that any activity can be associated<br />

<strong>with</strong> a key press.<br />

In Figure 7.5, we have connected <strong>the</strong> ATmega164 to a hexadecimal keypad via PORTC.<br />

PORTC[3:0] is configured as output to selectively assert each row. PORTC[7:4] is configured as<br />

input. Each row is sequentially asserted low. Each column is <strong>the</strong>n read via PORTC[7:4] to see if<br />

any switch in that row has been depressed. If no switches have been depressed in <strong>the</strong> row, an “F”<br />

will be read from PORTC[7:4]. If a switch has been depressed, some o<strong>the</strong>r value than “F” will be<br />

read. The read value is <strong>the</strong>n passed into a switch statement to determine <strong>the</strong> ASC<strong>II</strong> equivalent of<br />

<strong>the</strong> depressed switch. The function is not exited until <strong>the</strong> switch is released. This prevents a switch<br />

“double hit.”<br />

//*************************************************************************<br />

unsigned char get_keypad_value(void)<br />

{<br />

unsigned char PORTC_value, PORTC_value_masked;<br />

unsigned char ascii_value;<br />

DDRC = 0x0F;<br />

//set PORTC[7:4] to input,<br />

//PORTC[3:0] to output<br />

//switch depressed in row 0?<br />

PORTC = 0xFE; //assert row 0 via PORTC[0]<br />

PORTC_value = PINC; //read PORTC<br />

PORTC_value_masked = (PORTC_value & 0xf0); //mask PORTC[3:0]<br />

//switch depressed in row 1?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!