How to configure TIM4
and test it on STM8S Discovery



Home Page
STM8 home page
STM8L home page


Suggested reading:
  • UM0817: STM8S Discovery user manual is here.
  • STM8S105xx DATASHEET is here.
  • RM0016: REFERENCE MANUALS is here.
  • PM0051: How to program STM8S Flash program memory and data EEPROM is here.
  • Flow diagram for developing software in C on STM8 - click here

Preconditions:
  • The PC operating system is Windows XP SP3.
  • In the PC are installed the:
    Cosmic C Compiler 32K free
    STM IDE (ST Visual Develop - STVD)
    STM8S firmware library (see below)

  • The example below is based on STM8S Discovery
  • What does the example:
    The example explain how to configure TIM4 and test it on STM8S Discovery.

Click here to get this SW Example ready to use, inside there is the excel sheet
(STM8-Discovery-TIM4)
The project was tested with Cosmic C Compiler, the tree of the project is:





First, is important look the clock tree that is show below.
It is important select the clock source that you want to use in your application (default is HSI, RC osc. at 16Mhz).
In my application I'm using HSE clock source.




Second, is a good idea see the TIMERs specification, see below.



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:
TIM4_Prescaler
TIM4_Period
see below



In the BOLD box you see the result of the calculation.
You have two possibility.

In C column, you have the possibility to insert:
Freq.
TIM4_Prescaler
TIM4_Period
and the result is the time of interrupt.

In F column, you have the possibility to insert:
Freq.
TIM4_Prescaler
Interrupt
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 TIM4
configure GPIOD pin_0 for drive the led that is present on STM8S Discovery
enable interrupts
NOTE: the GPIO pin_7 is not used in this example
see below.



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.
See below.













LINK:






Home Page
STM8 home page
STM8L home page