STM32 Timer Interrupt using CubeMx and HAL API

STM32 Timer Interrupt using CubeMx and HAL API


4 minute read

Welcome to an exploration of STM32 Timer Interrupts—a fundamental aspect of embedded programming. In the realm of microcontrollers, Timer Interrupts stand as a vital tool, enabling the execution of code at precise intervals. Whether it's sampling analog data or performing periodic tasks, Timer Interrupts offer a reliable mechanism for time-sensitive operations.

This article serves as a practical guide for enabling and implementing Timer Interrupts on STM32 microcontrollers. Whether you're a novice embarking on your journey into embedded programming or an experienced developer seeking to master STM32's capabilities, you'll find valuable insights here.

Before the configuration of the STM32 Timer Interrupt, I would like to dive into the underlying mechanisms of this interrupt. However, if you are seeking just the implementation of the interrupt, you can skip this part and directly go to the next part of the article. 

The image below is a screenshot of the reference manual which illustrates the working principle of the timer. In this example, the auto-reload value is equal to 36, and the Timer’s mode is upcounting. The counter is incremented and reaches the auto-reload value (36). In this instance, we have a counter overflow event, and the counter resets to zero. We can generate the Timer update interrupt every time this event happens (update interrupt flag). It is worth mentioning that the counter overflow occurs with a constant period. The period depends on the Timer’s frequency and the auto-reload value. If you are willing to know more about notions such as counters, prescaler, and auto-reload value you can refer to my other article.

STM32 Timer Interrupt internal mechanisms

CubeMx Timer Configuration for the STM32 Timer Interrupt

For this tutorial, I use the Nucle-L476RG board along with STM32 CubeIde. However, this tutorial applies to any STM32 MCU board.

To enable the Timer interrupt, we need to configure the Timer using CubeMx Software. Choose one of the Timers, and enable the Timer by setting the clock source (in my project, I chose TIM4). Then, We need to set the prescaler and timer period. These parameters directly affect the frequency of the Timer Interrupt. There is another notion called a repetition counter which also controls the frequency of the timer update interrupt. If the value of the repetition counter is N, the processor will invoke the update interrupt every N + 1 timer overflows.

Finally, we can write the following formula for the Timer Interrupt’s frequency:

STM32 Timer Interrupt Frequency Equation

In my project, the clock frequency equals 80 MHz, the prescaler is 79, the auto-reload (timer period) is 9999, and the repetition counter is zero. Based on these values, the frequency of the timer interrupt is 100 Hz.

STM32 TImer Interrupt CubeMx configuration 

Finally, we need to enable the timer interrupt (clicking the box as shown in the picture below). Then, we can save the file to generate the code. 

STM32 Timer interrupt enable

STM32 Timer Interrupt Implementation using HAL API

Once, the code generation is over, we will find a function specially designed to configure our timer (in my case, MX_TIM4_Init()). Just go there and insert the following line to enable the interrupt at the end of the function or within the main function. 

HAL_TIM_Base_Start_IT(&htim4);

HAL_TIM_Base_Start_IT is a function to start the interrupt. Next, we need to define the callback function of the interrupt which is invoked automatically with the constant period. 

uint8_t timer_interrupt_check;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	timer_interrupt_check++;
}

You can write your custom code inside of the callback function to verify that the interrupt is functional. For example, you can blink an LED using the Timer interrupt. In my case, I increment a variable and monitor its value in real time using the Live Expressions Tool. Its values should be incremented based on the frequency of the timer interrupt.

STM32 Timer Interrupt test

That is it for this lesson. If you have any questions, feel free to drop a comment below.

STM32 Programming Resources

STM32 Programming Course

STM32 articles

STM32 IMU

« Back to Blog