The system timer is called the SysTick timer and is a simple timer provided in the ARM Cortex-M3 series as part of the CPU core. The general-purpose peripheral timers described in the next chapter are very sophisticated, but on the other hand, they also have a wide range of settings.
The SysTick timer counts the AHB clock HCLK of the STM MPU. the SysTick timer is a down counter that counts down as the value is set, and when it reaches 0, the timer repeatedly generates an interrupt on the next count to return the counter value to the set value.
The SysTick timer generates interrupts in cycles of a set duration, so it is easy to use the SysTick timer for a small application, for example, to have a process run periodically every second. The following is an example of this.
Interrupt with divided 9 MHz system clock as SysTick timer：
The settings are explained within the actual program.
In the case of a 72 MHz system clock, the default setting (SystemCoreClock) is 72M (72 million), so if SystemCoreClock is used as the setting value as it is, the clock will repeat 72M counts per second.
As a matter of fact, the counter on the SysTick timer is 24 bits, so the maximum countable limit is 224, or 16,777,216, and it must be set so that this limit is not exceeded. STM32 MCU allows you to choose whether to use the system clock at its current speed or divide it in one-eighth.
If the system clock is 72 MHz, the counter limit is exceeded, so use a counter divided in one-eighth.
① Set counter value for SysTick timer
SysTickConfig() is used to set the counter value for the SysTick timer. 1-second cycle interrupts can be generated by setting the counter value to SystemCoreClock if the system clock is used as is, or by dividing the clock by 8. SystemCoreClock/8 is used.
The program checks the return value of SysTick_Config() (0 if normal, 1 if an illegal tick is given, such as exceeding the timer's count limit) in the execution example to confirm normal execution before proceeding to the next process.
② Specify system clock source
SysTick_CLKSourceConfig()is used to specify the system clock source.
The argument can be either the AHB clock as it is or a clock divided by 8.
■ SysTick_CLKSource_HCLK : AHB clock as is
■ SysTick_CLKSource_HCLK_Div8 : AHB clock divided by 8
③ Description of Interrupt Processing
Interrupts are now enabled by executing SysTickConfig(), which is set to execute the dedicated interrupt handling function SysTick_Handler() when an interrupt is generated by the SysTick clock.
Therefore, if the process to be executed at the time of a system timer interrupt is described in this function, the desired process can be executed when the interrupt occurs.
The above program example generates an interrupt with a 1-second cycle. To change the interrupt cycle, simply change the argument (count setting value) of ①SysTick_Config().
For example, if SysTick_Config(SystemCoreClock/8 /1000) is used in the above program, an interrupt will be generated every 1ms cycle and the interrupt function SysTick_Hander() will be called. A 1 ms timer can be easily created by writing a process to increase or decrease the value by 1 each time it is increased or decreased.
The simple timer function using the SysTick clock introduced and explained in this chapter makes it easy to create delay functions and other functions often used in microcontroller programs with a minimum of code.