STM8 library for Keypad

Back to STM8 code samples home page.

Here is my new library for using generic keypads on STM8. It can handle any number of rows and columns and you can use any pins you wish. Note that if you use "true open drain" pins, you will need to add a pull-up resistor.



The example program uses a 4x4 keypad with pins D3, D2, D1, C7 as the columns and C6, C5, C4, and C3 as the rows. The keymap is whatever you choose, but it may be most convenient to use the ASCII decimal digits and other chars for the other keys.

keypad_init(row config, column config) Initialize library.
keypad_kmap(keymap string) Install or change keymap.
keypad_poll()Poll keypad (call every millisecond).
keypad_getc()Get next character from keypad.

The example program shows how to use these functions easily. Connect a USB serial adapter to the UART pins and watch the key presses and releases.

In the header file lib_keypad.h are four configuration items:

KP_BUFSIZE Size of keypad input buffer, can be any size.
KP_DEBOUNCE Debounce count in milliseconds
KP_KEY_REL Give notification when key is released (bit-7 set).
KP_POLL_MS Actual key polling rate, milliseconds

The polling takes about 36 microseconds with a 4x4 keypad. There is no need to poll every millisecond, so you can choose whatever you like. Every 4 milliseconds works fine and divides evenly with a 40 millisecond debounce.

I have prepared a tarball with the library, Makefile, headers, and an easy test program that you can play with. Assuming you have the SDCC compiler ready, just type "make". You can also browse the source code below.

lib_keypad.h
lib_keypad.c
test_keypad.c

lib_keypad.tgz: Complete source

Questions or comments? Feel free to contact me:
richard@hodges.org

I am currently available for software development projects.

Last page update: December 21, 2018 (corrected 36 "milliseconds" error)
This web page created with vi.
I write good code, not fancy HTML.