Controlling STM32 Hardware Timers using HAL

This tutorial shows how to use the STM32 hardware timers via the STM32 HAL API. If you want to use them with the legacy StdPeriph library, follow this tutorial instead.

Before you begin, ensure that you can build and debug embedded projects for your STM32 board by following one of our basic STM32 tutorials.

  1. Before we can start configuring the timer, we’ll give a brief overview how the timers work. Hardware timers keep counting up or down depending on the module until the timer period is reached. Then the timer is reset:
  2. We will use the timer to keep our LED on 80% of the time by setting a period of 500, turning it on when the counter reaches 400 and turning it off when it reaches 500:
  3. Create a basic HAL-based LEDBlink project for your board if you have not done that already. Then we will begin with configuring the timer. This is done by calling __TIMx_CLK_ENABLE(),  filling the fields of the Init member in the TIM_HandleTypeDef structure and calling HAL_TIM_Base_Init() and HAL_TIM_Base_Start():
  4. From the main() function we can get the current timer value using the __HAL_TIM_GET_COUNTER() macro and update the LED accordingly. The main source file will look the following way:
  5. Run the program and notice how the LED starts blinking. You can use the Preprocess Selected Lines command to see that the __HAL_TIM_GET_COUNTER() simply reads the CNT register of the timer:timer
  6. If you change timerValue to be a static variable, you can use the Live Variables to plot how the value is changing over time following the TIM2->CNT value:live
  7. You can also configure the timer to automatically generate the ‘update’ events once the value reaches the period value. This can be done by replacing a call to HAL_TIM_Base_Start() with HAL_TIM_Base_Start_IT():
  8. From the main() function you can check the status of the ‘Update’ interrupt and toggle the LED state when the update happens:

    If you run the program now, you will see how the LED changes its state every 500 timer cycles.
  9. The good thing about the timer interrupts is that you actually don’t need to check their status manually. As long as you enable the interrupt by calling HAL_NVIC_EnableIRQ(), the timer will automatically trigger an interrupt handler function when the event occurs. You can look up the interrupt function name in the startup_stm32xxxx.c file:handler
  10. The easiest way to write the interrupt handler is to simply call HAL_TIM_IRQHandler() from it. The handler provided by HAL will automatically check which event caused the interrupt and invoke one of the specialized handlers like HAL_TIM_PeriodElapsedCallback(). Add the following code to your main file to try that out:

    Note that the main() function does not need to do anything with the timer anymore and can simply handle the rest of your application’s logic.
  11. You can visualize what happens when the interrupt is triggered by adding an indicator variable and toggling it from the callback function (a value of 250 is chosen to scale good with the timer counter range):
  12. Simply add both timer counter and the g_Indicator to live variables and observe how the indicator variable is changed each time the timer gets reset:ind
  13. If you set a breakpoint at the HAL_TIM_PeriodElapsedCallback() function, you can see how it’s called by the HAL_TIM_IRQHandler() as a result of the timer 2 interrupt handler being invoked:stack
  14. You can use the Code Map to quickly explore what other event handlers the HAL_TIM_IRQHandler() method invokes:cb