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 dividing (also on the fly) the counter clock frequency by any factor between 1 and 65536.

  • Up to 6 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)

Refer to reference manual for full set of features

Callback registration

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

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

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

These functions allow to register following callbacks:

By default, after the Init 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 in HAL_TIM_STATE_IDLE state only.

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

The TIM driver might be used for a variety of purposes which include:

  • 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

The programming sequence follows these steps:

  • Initialization of the TIM handle (registration of a particular instance, etc)

  • Configuration of the different resources of the timer depending on the usage

  • Start of channels, if needed, followed by the start of the timer’s counter