I'm using TIM4 that is a basic timer and that has a prescaler selectable from 1 to128.
The steps are: 2, 4, 8, 16, 32, 64, 128.
From the RM0016: REFERENCE
MANUALS is here, I have develop an excel sheet for calculate the values of:
In the BOLD box you see the result of the calculation.
You have two possibility.
In C column, you have the possibility to insert:
and the result is the time of interrupt.
In F column, you have the possibility to insert:
and the result is the TIM4_Period.
My goal is to get 1mS interrupt, so I used the F column.
The Freq is 16Mhz the interrupt time is 1mS so I chose 128 for prescaler and 125 is the result of calculation.
125 is the TIM4_Period.
The implementation of the software was very easy because I used the STM8 Library.
The step are:
configure the HSE clock source
configure GPIOD pin_0 for drive the led that is present on STM8S Discovery
NOTE: the GPIO pin_7 is not used in this example
All the work is does inside the TIM4 Interrupt routine, see below.
Line n.658 changes the status of the LED.
Line n.660 reset the Interrupt.