cancel
Showing results for 
Search instead for 
Did you mean: 

How to use TIM with STM32CubeMX2

B.Montanari
ST Employee

Summary

This tutorial provides a concise guide on how to enable and configure the general-purpose timer (TIM) peripheral using STM32CubeMX2. The demonstration uses the NUCLEO-C562RE board. The following TIM features are covered:

  • Basic timer configuration and usage
  • Timer interrupts for periodic events

Introduction

Timers in STM32 microcontrollers are versatile peripherals capable of generating precise time bases, PWM signals, input capture, output compare, and triggering events. They are essential for tasks such as periodic interrupts, pulse generation, and measuring time intervals.

STM32 timers support multiple modes and can be used with interrupts and DMA to optimize CPU usage and achieve accurate timing control.

With STM32CubeMX2 and the HAL2 driver, configuring and using TIM has changed. This tutorial provides a step-by-step guide on how to use the new tools.

Prerequisites

Install the following tools:

The hardware used in this tutorial is the NUCLEO-C562RE board.

1. STM32CubeMX2 project creation and basic setup

Follow these steps to create a simple application that creates a periodic 1KHz interrupt for time base project for the NUCLEO-C562RE board.

1.1 Project creation

Open STM32CubeMX2. On the "Home" page, click the [MCU square] to create a new project

BMontanari_0-1769798930057.png

In the search field under MCU name, enter STM32C562RE and select the MCU. Click [Continue].

BMontanari_1-1769798930059.png

Enter the project name and location. Click [Automatically Download, Install & Create Project] to finish project creation.

BMontanari_2-1769798930062.png

Select [Launch Project] to start.

BMontanari_3-1769798930063.png

1.2 Pinout and Configuration

Configure the green LED, available on PA5.

BMontanari_4-1769798930069.png

BMontanari_5-1769798930072.pngNavigate to the "Peripherals"section, then "Timers". Enable a general-purpose timer, for example, TIM2, and configure the timer mode for simple time base.

BMontanari_6-1769798930076.pngBefore setting the prescaler and counter period to define the timer frequency and period, configure the clock settings. Click the [Clock] icon and adjust the frequency as needed. For example, set the clock to its maximum of 144 MHz.

BMontanari_7-1769798930083.pngReturn to TIM and configure the prescaler to achieve a 1 ms time base. Use the standard formula:

Frequency = f_clk / [(PSC + 1) × (ARR + 1)]

Example calculation:

  • Step 1 (Prescaler): 143 + 1 = 144
  • Step 2 (Period): 999 + 1 = 1,000
  • Step 3 (Total Divisor): 144 × 1,000 = 144,000
  • Step 4 (Final Result): 144,000,000 / 144,000 = 1,000 Hz (1 kHz)

BMontanari_8-1769798930085.png Use the Quick menu to find the NVIC settings. Enable update interrupt to trigger an interrupt on timer overflow.

BMontanari_9-1769798930090.png

1.3 Code generation

To generate the code:

  1. Click on the [Project settings] icon and select the desired IDE.
  2. Click the Generate button.

BMontanari_10-1769798930093.png

2. Configuring the project in Visual Studio Code

Open Visual Studio Code and open the project folder.

If prompted, select the configuration. If not prompted, press Ctrl+Shift+P, type CMake: Select Configure Preset, and choose the debug configuration.

BMontanari_11-1769798930094.png

Build the project to ensure everything is set, then proceed to code implementation.

BMontanari_13-1769798930105.png

3. Code editing (basic timer with interrupt)

After opening the generated project in Visual Studio Code, edit main.c. This example

demonstrates how to configure a basic timer to generate periodic interrupts.

In main.c:

/**
  ******************************************************************************
  * file           : main.c
  * brief          : Main program body
  *                  Calls target system initialization then loop in main.
  ******************************************************************************
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
hal_tim_handle_t *pTIM;           // Timer handle pointer from generated code
volatile uint32_t timerTick = 0;  // Timer tick counter
/* Private functions prototype -----------------------------------------------*/
/* Timer interrupt callback */
void HAL_TIM_UpdateCallback(hal_tim_handle_t *htim)
{
    if (htim == mx_tim2_gethandle())
    {
        timerTick++;
    }
    if(timerTick >= 1000)
    {
      HAL_GPIO_TogglePin(HAL_GPIOA,HAL_GPIO_PIN_5);
      timerTick = 0;
    }
}

/**
  * brief:  The application entry point.
  * retval: none but we specify int to comply with C99 standard
  */
int main(void)
{
  /** System Init: this code placed in targets folder initializes your system.
    * It calls the initialization (and sets the initial configuration) of the peripherals.
    * You can use STM32CubeMX to generate and call this code or not in this project.
    * It also contains the HAL initialization and the initial clock configuration.
    */
  if (mx_system_init() != SYSTEM_OK)
  {
    return (-1);
  }
  else
  {
    /*
      * You can start your application code here
      */
    HAL_TIM_Start_IT(mx_tim2_gethandle());
    while (1) {}
  }
} /* end main */

3.1 Validation

After building the application, locate the [Run and Debug] icon, create your debug session by selecting the [STM32Cube: STLINK GDB Server] option.

BMontanari_14-1769798930105.png

Once in debug mode, add a breakpoint in the callback or observe the LED blinking at a 1 second rate.

BMontanari_15-1769798930112.png

BMontanari_17-1769799305381.png

Conclusion

This tutorial guides you through setting up and using STM32 timers on the NUCLEO-C562RE board with STM32CubeMX2 and STM32Cube for Visual Studio Code. It demonstrates basic timer usage with interrupts, enabling you to implement precise timing and control functions in your embedded applications.

Related links

 

 

Version history
Last update:
‎2026-03-16 2:45 PM
Updated by: