HAL TIM How to Use

group TIM_How_To_Use

TIMER Generic features

Depending on the timer’s type (Basic, Lite, General-purpose, Advanced), the timer features include:

  • 16-bit or 32-bit up, down, up/down auto-reload counter.

  • 16-bit programmable prescaler allowing division (also on the fly) of the counter clock frequency by any factor between 1 and 65536.

  • Up to 7 independent channels for:

    • Input Capture

    • Output Compare

    • PWM generation (Edge and Center-aligned Mode)

    • One-pulse mode output

  • Complementary outputs with programmable dead-time

  • Synchronization circuit to control the timer with external signals and to interconnect several timers together.

  • Repetition counter to update the timer registers only after a given number of cycles of the counter.

  • Encoder interface mode

  • Preload feature:

    The preload feature is available for:

    • The auto-reload timer register (TIMx_ARR)

    • The timer prescaler register (TIMx_PSC) (cannot be turned off)

    • The timer channel registers (TIMx_CCRy)

Callback registration

The compilation flag USE_HAL_TIM_REGISTER_CALLBACKS, when set to 1, allows the user to configure the driver callbacks dynamically.

Use the function HAL_TIM_Register<Callback_name>() to register a callback.

It takes the HAL peripheral handle and a pointer to the user callback function.

These functions allow registration of the following callbacks:

By default, after initialization and when the state is HAL_TIM_STATE_INIT, all interrupt callbacks are set to the corresponding weak functions HAL_TIM_<Callback_name>().

Callbacks can be registered only in the HAL_TIM_STATE_IDLE state.

When the compilation flag USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration feature is not available, and all callbacks are set to the corresponding weak functions.

How to use this driver

Use the TIM driver for purposes including:

  • Time base generation

  • Measuring the pulse lengths and duty cycle of input signals (input capture)

  • Generating output waveforms (output compare, PWM, complementary PWM with dead-time insertion)

  • Pulse generation

  • Determining rotor speed/position feedback provided by a quadrature encoder or a hall sensor

Use the following programming sequence:

  • Initialize the TIM handle (registration of a particular instance).

  • Configure the different resources of the timer depending on the usage.

  • Start channels, if needed, and then start the timer counter.