18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7<br />

A 1-microfarad capacitor at KBM and 0.1-microfarad capacitor at OSC give a keyboard<br />

debounce of 10 milliseconds and a scan rate of around 600 hertz. Larger values will increase<br />

the debounce time and decrease the scan rate. <strong>The</strong> 74C922’s data sheet recommends<br />

choosing KBM’s capacitor to be ten times the values of OSC’s capacitor, so be sure to change<br />

both if you change either.<br />

Testing the Keypad<br />

Listing 7-3 tests Figure 4’s circuit. <strong>The</strong> program waits for a key press and when one occurs,<br />

displays the value of the key on the host computer.<br />

<strong>The</strong> following paragraphs explain the program in greater detail.<br />

Lines 10-25 are a lookup table that translates the 74C922’s A-D outputs into an ASCII code<br />

corresponding to the key pressed. <strong>The</strong> table is stored at locations 1FF0h-1FFFh in external<br />

RAM. <strong>The</strong> lookup table is arranged with the values of the 74C922’s data outputs (0-Fh) in<br />

ascending order. Notice that the key legends (0-F) do not follow in order in the table, because<br />

the legends on the keypad correspond to the data outputs only at keys 4-6 and F.<br />

Lines 30-50 are the main program loop. This does nothing except wait for an interrupt. When<br />

you press a key, the 74C922’s DA output goes high. <strong>The</strong> falling edge at INT1 then causes the<br />

program to jump to line 100.<br />

Lines 100-110 read the value of the keypress at AD0-AD3 and, use the lookup table to translate<br />

the key press into an ASCII code. For example, if the key labeled “7" is pressed, Y3 goes<br />

low when X1 is scanned, and the 74C922’s AD outputs identify the keypress as 1000 in binary,<br />

or 8 decimal, which the 8052 reads at E000h. Line 100 ANDs AD0-AD7 with 0Fh to clear<br />

bits 4-7, leaving 8, the value of the keypress. In line 110, 8 + 1FF0h = 1FF8h, and the value<br />

stored at 1FF8h in external memory is 55, which is the ASCII code for the numeral 7.<br />

Line 120 causes the character matching the keypress to display on-screen. <strong>The</strong> program then<br />

returns to the main loop to wait for another key press.<br />

If you have a keypad with different encoding, change the lookup table to match its layout.<br />

Customizing the Interface<br />

Listing 7-3 does little more than test the interface, but you can use the general idea in a<br />

specific project. You can also assign special functions to individual keys. In an EPROM<br />

programmer, these might be Select device, Program, Verify, and so on. If the functions aren’t<br />

labeled on the keys, you can describe them in an on-screen menu: Press 1 to select device;<br />

press 2 to program; and so on. <strong>The</strong>n, when a keypress is detected, instead of just displaying<br />

the value of the key, your program would branch to a subroutine that corresponds to the<br />

122 <strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong>

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

Saved successfully!

Ooh no, something went wrong!