ECE 476: Keypad Library
Introduction
This library is a fully functioning keypad interpreter designed for the Atmel Mega32 microcontroller. Both the telephone and hexadecimal style keypads are supported. Keypad scanning, key debouncing, key string management, and key display management are handled automatically by the library. A simple user interface allows for complete customization of much of the functionality.
Version History
- 25 Feb 2005: first version of library published by Richard West with assistance from Eric Mesa
- 30 Jan 2006: second version (change log) of library published by Richard West
User interface
Standard user interface:
- void keypad_map_keysymbol(unsigned char old_keysymbol, unsigned char new_keysymbol);
Replaces a character in the key symbol table with a new user specified character.
- void keypad_define_terminator(unsigned char keysymbol);
Replaces a character in the key symbol table with '\0' (NULL). A terminator must be defined for the telephone and hexadecimal keypads unless using the keypad as a keyboard (see below).
- void keypad_ignore_keysymbol(unsigned char keysymbol);
Specifies which keypad buttons (based upon their keysymbol) to ignore.
- void keypad_get_string(void);
Responsible for running the keypad state machine. This function reads key presses, constructs the "keystring", and signals its completion/termination using "keystring_ready". Once the keystring has been terminated, the keypad state machine enters a done state so no additional key presses can be read until the keystring has been processed and the keypad released.
- void keypad_release(void);
Resets the keypad state machine. Call this function as soon as keystring has been processed and another keystring required.
Extended user interface for operating the display:
- void keypad_set_display(unsigned char display);
Specifies how the keypad library handles displaying key presses. There are three display modes: KEYPAD_NODISPLAY, KEYPAD_MASKDISPLAY (display an asterisk ('*') for each typed character), and KEYPAD_ECHODISPLAY (display characters as typed).
- void keypad_gotoxy(unsigned char x, unsigned char y);
Specifies the starting location for typed characters. The keypad library automatically advances the cursor as each letter is typed.
Setting up the keypad library
The keypad library has been designed to be versatile, requiring very little user intervention to operate as intended. However, several preprocessor directives must be invoked before including the keypad header file (keypad.h). Failure to include these directives will cause compile-time errors, run-time errors, or both. In the second version of the keypad library, an effort has been made to generate compile-time errors for common mistakes and to eliminate possibly ambiguous compilation.
There are two types of keypad available for use in ECE 476 (see Figure 1), and the user must specify which keyboard layout to use when interpreting keycodes. The two types of keypad are either the telephone style keypad (right) or the hexadecimal style keypad (left). To use the telephone keypad either invoke #define KEYPAD_TELPAD or #define KEYPAD_KEYBOARD (see below) preprocessor directive. To use the hexadecimal keypad invoke the #define KEYPAD_HEXPAD preprocessor directive.
Figure 1: the two keypads with differing layout
The keypad library is designed such that the keypad can be used on any of the Mega32's I/O ports. To specify which port to use, invoke the #define KEYPAD_USEPORTx preprocessor directive where x is replaced by the port letter (in uppercase). To use the display functionality of the keypad library, include the preprocessor directive #define KEYPAD_USEDISPLAY and #define DISPLAY_USEPORTx prior to including the keypad header file (keypad.h). A couple of advantages exist to not use the display functionality--smaller RAM usage and faster execution time--but displaying typed characters is an advantage for user interfaces. Play around with the provided test program to experiment with the various library setups.
Keypad keyboard
In addition to the standard keypad layouts, included in the keypad library is a fully defined 62-key keyboard layout (see Figure 2). The keypad keyboard works by holding down "shift" buttons (A, B, C, and D) and pressing one other button. For instance, pressing button '2' will display a '2' on the LCD, but pressing '2' while holding button 'A' will display an 'a' on the LCD. All alphanumeric keys and a vast punctuation set are available through sixty-two letter-number combinations. Caps-lock is available ('AB' or 'BA') to expand the alphabet to include both uppercase and lowercase, a delete key ('AC' or 'CA') is available, and a clear key ('AD' or 'DA') is available.
Figure 2: the keypad keyboard with 62-key layout
The simplest way to become familiar with the keypad keyboard is to experiment with it using the provide test program. To use the keypad as a keyboard, invoke the #define KEYPAD_KEYBOARD preprocessor directive (use a telephone style keypad). No terminator needs to be defined for the keypad keyboard since the '#' button is already mapped to NULL and functions as the terminator.
"If cell phones worked like this, I may use mine more often." ~ Professor Bruce Land
Wiring notes
When wiring either style of keypad:
- Connect pin 1 to PORTx.0
- Connect pin 2 to PORTx.1
- Connect pin 3 to PORTx.2
- etc.
For either style of keypad, pin 1 is the leftmost pin when looking at the front of the keypad. The easiest way to verify that your wiring is correct is to run the keypad test program given at the end of this page.
When wiring the LCD:
- Connect pin 1 to GND
- Connect pin 2 to VCC
- Connect pin 3 to 10K trimpot or to GND
- Connect pin 4 to PORTx.0
- Connect pin 5 to PORTx.1
- Connect pin 6 to PORTx.2
- DO NOT connect pins 7-10
- Connect pin 11 to PORTx.4
- Connect pin 12 to PORTx.5
- Connect pin 13 to PORTx.6
- Connect pin 14 to PORTx.7
Note that pin 1 of the LCD is the pin closest to the edge of the board. The easiest way to verify that your wiring is correct is to run the LCD test program.
Library files and test program
Download version 02 (latest) here:
- keypad.h - header file to include in your programs.
- keypad.c - automatically included by header file (bad C style, but CodevisionAVR convention).
- keypad_test.c - test program to allow you to experiment with various keypad setups and compiler directive combinations.
Download version 01 here:
- keypad.h - header file to include in your programs.
- keypad.c - automatically included by header file (bad C style, but CodevisionAVR convention).
- keypad_test.c - test program to allow you to experiment with various keypad setups and compiler directive combinations.
- keyboard.gif - old keyboard map for version 01.
Copyright Cornell University Feb 2005