STM32 Timer

STM32 Timer


4 minute read

STM32 Timer Introduction

The timer is an essential peripheral in microcontrollers. It allows tracking time, generating digital signals, periodically executing some code, and many other things. At first glance, notions of clock and timer might be confusing. But they are entirely different things. The former is a physical part and is responsible for synchronizing all components of the microcontroller. The latter is a peripheral used with other peripherals to execute code periodically and generate digital signals. Let me present the fundamental parameters of timers in STM32 MCUs:

1. The Operating frequency of the timer is the number of ticks it does per second.

2. Prescaler is required to adjust the operating frequency of the timer:

Operating frequency = Clock frequency/ prescaler+1

3. Counter of the timer is a value that tracks the ticks of the Timer. It is incremented based on the frequency of the timer. For example, a 1 MHz frequency timer updates the counter million times per second. From a software point of view, the counter is a 16-bit variable (register) whose change rate is governed by the frequency of the timer.

4. Auto-reload. The counter cannot tick to an infinity. In STM32, microcontrollers usually have 16-bit counters, meaning they can count up to 65535 (216 − 1) and resets to zero. However, we can set a custom maximum value of the counter using the auto-reload register. If we set the auto-reload to 2000, the counter starts from 0 and reaches 2000. Then, instead of getting incremented further, it starts from zero.

It is also worthwhile to mention the counting modes of timers: upcounting, downcounting, and center-aligned. By default, timers work in the upcounting way when the counter starts from zero and gets incremented up to the auto-reload value. Then it restarts from zero. In the downcounting mode, the timer starts from the auto-reload value and decreases to zero. Then it resumes from the auto-reload value. The counter starts from zero and reaches the auto-reload value in the center-aligned mode. After, instead of starting from zero, the timer decrements the counter until it reaches zero.


Enabling Timer, STM32CubeMx

Let’s configure Timer 1 using STM32CubeMX. I will utilize the Nucleo-l412kb STM32 board and configure Timer 1. However, the tutorial can be adapted to other MCUs.  In this board, the clock frequency is 32 MHz (HCLK); see the figure below.

stm32 timer hclk

To enable Timer 1, we choose the internal clock as a clock source of the timer. Also, we need to set the Prescaler to 31999 to achieve a 1 kHz operating frequency because the HCLK frequency equals 32 MHz.

stm32 timer configuration

Then, we can generate the code by saving the IOC file. Eventually, we enable the timer using HAL_TIM_Base_Start(); function. As an argument, we provide a struct variable. We can write this line at the end of the MX_TIM1_Init function.

HAL_TIM_Base_Start(&htim1);

Finally, to check how it works, I defined two global variables and wrote the following code within the while loop:

timer_value1 = __HAL_TIM_GET_COUNTER(&htim1);

HAL_Delay(1000);

timer_value2 = __HAL_TIM_GET_COUNTER(&htim1);

HAL_Delay(1000);

, where the _HAL_TIM_GET_COUNTER function provides the Timer’s counter value. In the debug mode, we can monitor timer_value1 and timer_value2 in real time. Since I assign the value of the counter to the variables with a 1-second delay, the difference between these variables is equal to 1000 because the Timer ticks 1000 times per second because the operating frequency of the Timer is 1 kHz.

You can refer to this article to use the Timer to generate a PWM signal:

STM32 PWM signal generation

In addition, there is a video explaining the key parameters of STM32 Timers.

« Back to Blog