Millis() Function for Stm8s MCUs

Introduction: Millis() Function for Stm8s MCUs

Like many others, I started with Arduino platform, and cannot live without the useful millis() function. When ever study new MCUs, one of the first things I do is to look for millis() function. With AVR, that function is already available, using Timer0 is the simpliest way. For STM8S, I could not find one. So, I must build one. After a short time learning, I was able make a library so that it is easy to use.

Mainly, to have this function we must use a timer, in this case I use Timer4, which is basic 8-bit timer, and we will use its overflow interrupt. Below is the details of how to use it:

  • First, in the main source fine (normaly main.c), the library must be called:

#include "tim4millis.h"

  • At the main() part, we must initiate timer 4 by calling the TIM4_Init() function


  • Then, when ever need to know the current millis since startup, we just need to call the millis()

uint32_t currentTime = millis();

In the attachment (or in my Github repo) are the header file and source file of the tim4millis library, and the example file to show how to blink the led (pin PB5) as in this STM8S103F3P6 Minimum Development Board, using millis().

Hope this will save you some times when starting with STM8S. Cheers.

Be the First to Share


    • Pi Day Speed Challenge

      Pi Day Speed Challenge
    • Trash to Treasure Contest

      Trash to Treasure Contest
    • Microcontroller Contest

      Microcontroller Contest