Code samples for STM8 architecture

(New!) Water tank monitor project using inexpensive ultrasonic module

Browse my library source code here. (Informative)

Browse my library source code on Github. (Easier browsing)

I have been writing code for embedded systems for ages, mostly 6809, MIPS, and PIC. Also some Z80, 8748, and 6805. I have been happy with the PIC processors, in particular. They are cheap and easy to use in small projects.

Just recently (last year) I learned of a very cheap processor from ST. The price is much lower than comparable PIC chips, and the available Flash, RAM, and EEPROM are great compared to PIC. I first bought ten (10) development boards with processors for less than eight dollars. They are a bit over a dollar each now. Still a great deal. The programming device was only two and a half. The USB serial adapter shown works fine with the UART, and goes for less than a dollar.

Please note that I have provided links to ebay where you can directly buy the parts. I try to keep them current and find the lowest price. If you see a better price, please let me know and I will update the links.

8K Flash, 1K RAM, 640 EEPROM

Currently 10 for $10.40

16K Flash, 2K RAM, 1024 EEPROM
With 8mhz crystal

Currently $2.55

Currently $2.41

USB Adapter
Currently $0.88

The architecture reminds me of the 6502 and 6809, but stronger in so many ways. It has two 16-bit pointer registers with a lot of addressing modes. The stack pointer also has many useful addressing modes, which make it easy to access function parameters directly on the stack. It obviously was designed with compiled languages in mind. Most of the instructions take only one cycle, so this 16mhz CPU compares well with PIC.

The first ones I bought off eBay have 8K program memory, 1K RAM, and 640 bytes of EEPROM. Already soldered on a breakout board for just over a dollar each!

There is another board with more pins, more Flash memory, more RAM, more EEPROM, and an 8mhz crystal for more accurate timekeeping. The price is still a bargain. Both boards can take power from a micro-USB cable.

With a year of experience with this architecture, I am very happy with pretty much everything. Only once did I wish STM8 had a third pointer like PIC18 has. The hardware peripherals work perfectly and I think they are somewhat easier than PIC. I still think PIC is good, and will gladly do PIC work for hire, but my personal projects are now with STM8.

For working with the hardware, I am using the open source SDCC (small device C compiler) and the stm8flash utility. For debugging in a simulator, I am using the "free" compiler from Cosmic. It is Windows software, but it seems to work just fine with Wine.

Very simple "Hello world" for the STM8S103 board.

Here is a rudimentary "hello world" program that uses timer4 with interrupts and blinks the LED.

AES-128 Encode and Decode for STM8

Here is my AES-128 library. It uses a lot of inline assembly for speed. Note that SDCC and Cosmic compilers use slightly different calling conventions, so there are conditionals in there to get the pointers right. According to the Cosmic simulator, encoding a block uses 6545 cycles (409 microseconds) and decoding 7505 (469 microseconds). Because of all the tables, the code size is rather high (just under 4K) but hopefully the other 4K will be enough for everything else. I may rewrite it to trading CPU cycles for memory, which would free up another 1.5K

2x16 LCD Interface, with binary-decimal, rotary encoder, and UART libraries

2 x 16 LCD Display
As low as $1.44

Here is an example program that displays a clock and counters on the Hitachi lm016l LCD. This LCD uses the very common HD44780 driver, so you can probably use it for most small LCD units with a little tweaking. It also "just works" with the 2x16 displays I bought on ebay (yellow, blue, and red), the ebay 4x20 LCD, and the Datavision P124-4 2x20 display. Be sure to tie *WR low.

This file has three libraries. The first has two binary to ASCII decimal functions, one for 16-bit and one for 32-bit integers. This can be handy if you just want to display numbers without dragging in the whole printf() code.

The second library is to read a ALPS rotary encoder and to return the current position when desired. (This is a rotary switch that has two pins that change as the knob is rotated back and forth. It uses two input pins and a connection to ground.)

This is the ALPS encoder. Note: I have seen these go on sale several times for around fifty cents in quantity.

The third library sets up and controls the STM8 UART1. It uses interrupts and buffers to minimize the chance of losing data. The functions are to send byte, send string, receive byte, get receive buffer status, and get overflow counts.

UPDATE: I have a newer UART library that handles both the -103 (UART1) and -105 (UART2) chips and has separate sizes for the RX and TX buffers. This is in the library download file.

Library for MAX7219 LED controller

8 digits, 7-segment
As low as $1.45

8 dots x 32
As low as $4.51

Here is my library for the MAX7219 LED driver. This display driver can handle 64 LED segments or dots and it is easy to connect them together to form larger displays. There are many 8 digit 7-segment LED modules on eBay for less than two dollars each. And there are modules with 4 of the 8x8 displays (8 x 32 dots) for less than five dollars each. For testing, I connected three of the 7-segment modules together to form a chain. For the dot matrix displays, I connected two of the 4-chip modules together to make a 8x64 dot display. Either way, you only need to use three data pins, 5V, and ground for any number of units.

This library starts with m7219_init() taking the type of display and the number of MAX7219 devices. The type can be 7-segments, dot matrix with a provided font, or purely grahpic (you supply the dot data directly.) For 7-segment and dot matrix with font, you supply ASCII characters or strings and the library converts them into the correct segments or dots. You can set the overall brightness. For the dot matrix type, you can use a "marquee" mode to make your message move across the display. The example program shows how to do this. The 7-segment display can handle the ASCII digits (of course), space, hyphen, and the letters "ABCDEFHJLOPSU". The example program has a list of words that can be shown on the 7-segment display.

This library does use inline assembly for the free SDCC compiler. I can modify it for the COSMIC compiler on request.

Download my library code here.

Browse my library source code here.

Questions or comments? Feel free to contact me:

I am currently available for software development projects.

This web page made with vi.
I write good code, not fancy HTML.

Last page update: December 22, 2018