(NEW) STM8 library for TM1638 LED & Keypad controller

Back to STM8 code samples home page.

Here is my library for using the TM1638 controller on the inexpensive STM8S103P3 and STM8S105K4 processors.


8 Digits, 8 LEDs, 8 keys
As low as $1.88


8 Digits, 16 keys
As low as $2.58

These boards are an easy way to get an 8 digit display and a keypad in a cheap and convenient package.

To use this library, include the header file "lib_tm1638.h" with your #include files and call tm1638_init(mode) with either TM1638_8 or TM1638_16 for the board you have.

Output to display

Use tm1638_putc(char) to send a single char to the display or tm1638_puts(string) to send a string. For the decimal point, set bit-7 if you are sending one char at a time. For strings, just put the decimal place where you want it to appear.
tm1638_curs(position) sets the cursor position (0-7).
tm1638_clear() clears the display.
tm1638_bright(value) sets the brightness (0-7).
tm1638_blink(rate) sets the blink rate. The rate is times 1/100 second, or 0 to disable.
tm1638_setled(led, on) turns an LED on or off. This is only available on the 8-key boards.
On the 16-key boards, call tm1638_push()to push display changes to the LEDs. The LEDs are not updated until this call.

The 7-segment display can handle the ASCII digits (of course), space, hyphen, and the letters "ABCDEFHJLOPSU".

Input from keypad

To get key presses, call tm1638_poll() every millisecond. This is easy when you use the millisecond callback in my "lib_clock" library. Then periodically call tm1638_getc() to harvest key presses. Bit-7 will be low for a key press and high for release. You can use this to create shift function keys. The key values will be the ASCII characters "01234567" for the 8 key board and ""0123456789ABCDEF" for the 16-key board. This is the default: you can change this and set your own key map with tm1638_kmap().

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.

Download library and sample test code.


Browse the source code here:
lib_tm1638.h
lib_tm1638.c
test_tm1638.c

Other notes... I always run my STM8S103 boards at 16mhz on the RC oscillator, but the STM8S105 boards I buy have an 8mhz crystal on them, so I use that. A couple of my projects call for accurate time, and the crystals are pretty good, where the RC oscillators drift a lot over the days. So that is why my code uses different divisors for setting up the timers.

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

I am currently available for software development projects.

Last page update: October 8, 2018
This web page created with vi.