STM32F411 Timers for Micromouse - Types, Clock, Registers, PWM Generation, Encoder Interface Mode, Interrupt-Based Timing

 

Understanding STM32F411 Timers for Micromouse - Types, Clock, Registers, PWM Generation, Encoder Interface Mode, Interrupt-Based Timing

The STMicroelectronics STM32F411 microcontroller is widely used in embedded systems, robotics, motor control, IoT devices, and real-time applications. One of its most powerful features is the built-in timer peripheral system. STM32F411 timers help developers generate precise timing signals, measure events, control motors, and manage communication protocols efficiently.

What Are Timers in STM32F411?

A timer is a hardware peripheral inside the microcontroller that counts clock pulses. These counters can trigger events at accurate intervals without heavily loading the CPU. Timers are essential in embedded systems because many applications require precise timing and synchronization.

The STM32F411 includes several advanced timer modules that support:

  • PWM signal generation
  • Input capture
  • Output compare
  • Encoder interface
  • One-pulse mode
  • Periodic interrupts
  • Motor control

Frequency measurement

These hardware timers allow real-time operations with high accuracy.


Types of Timers in STM32F411

The STM32F411 microcontroller contains multiple timer categories, each designed for different purposes.

1. Advanced-Control Timers

Advanced timers provide high-end motor control and waveform generation features.

Examples:

  • TIM1

Features:

  • Complementary PWM outputs
  • Dead-time insertion
  • Break input protection
  • Center-aligned PWM mode

These timers are commonly used in:

  • Brushless DC motor control
  • Servo systems
  • Power electronics
  • Robotics


2. General-Purpose Timers

General-purpose timers are flexible and suitable for many embedded applications.

Examples:

  • TIM2
  • TIM3
  • TIM4
  • TIM5

Features:

  • PWM generation
  • Input capture
  • Output compare
  • Encoder mode
  • Interrupt generation

Applications include:

  • LED dimming
  • Sensor timing
  • Speed measurement
  • Frequency counting


3. Basic Timers

Basic timers are mainly designed for simple timing operations.

Examples:

  • TIM9
  • TIM10
  • TIM11

Features:

  • Simple periodic interrupts
  • DAC triggering

Applications:

  • Time base generation
  • Periodic task scheduling


Timer Clock System

STM32F411 timers operate using internal clock sources derived from the microcontroller clock tree. The timer frequency depends on:

  • System clock frequency
  • APB bus prescaler
  • Timer prescaler settings

The timer counter increments according to the configured clock rate.

The timer frequency equation is:

ftimer=fclock(PSC+1)(ARR+1)f_{timer}=\frac{f_{clock}}{(PSC+1)(ARR+1)}

Where:

  • ftimerf_{timer} = output frequency
  • fclockf_{clock} = timer input clock
  • PSC = prescaler value
  • ARR = auto-reload register

This formula is important when generating PWM signals or periodic interrupts.


Important Timer Registers

STM32F411 timers use several hardware registers for configuration.

   Prescaler Register (PSC)

   The prescaler divides the incoming timer clock frequency.

   Example:

  • Input clock = 84 MHz
  • PSC = 83

   Result:

  • Timer clock becomes 1 MHz

   Auto-Reload Register (ARR)

   ARR determines the maximum counter value before the timer resets.

   Applications:

  • PWM period control
  • Interrupt timing

   Counter Register (CNT)

   CNT stores the current timer count value.

   The counter increments or decrements depending on the timer mode.

   Capture/Compare Register (CCR)

   CCR controls:

  • PWM duty cycle
  • Input signal capture timing

   For PWM:

  • Larger CCR value means higher duty cycle

   Example of practical application

// PSC , ARR

htim9.Instance = TIM9;

htim9.Init.Prescaler = 1000; // PSC

htim9.Init.CounterMode = TIM_COUNTERMODE_UP;

htim9.Init.Period = 200; // ARR

// CCR

*MotorCfgParam[LEFT].PWM_TIM_CCRa = LpwmA; // backward

*MotorCfgParam[LEFT].PWM_TIM_CCRb = LpwmB; // foreward

// CNT

MotorCfgParam[LEFT].EncoderTIM_Instance->CNT = 0;

MotorCfgParam[RIGHT].EncoderTIM_Instance->CNT = 0;



PWM Generation Using STM32F411 Timers

Pulse Width Modulation (PWM) is one of the most popular timer applications.

PWM signals are used in:

  • Motor speed control
  • LED brightness adjustment
  • Servo control
  • Power converters

A PWM duty cycle equation is:

Duty Cycle(%)=CCRARR+1×100Duty\ Cycle(\%)=\frac{CCR}{ARR+1}\times100

The STM32F411 timers can generate highly stable PWM outputs with minimal CPU usage.

   Example of practical application

void MX_TIM3_Init(void)

{


/* USER CODE BEGIN TIM3_Init 0 */


/* USER CODE END TIM3_Init 0 */


TIM_MasterConfigTypeDef sMasterConfig = {0};

TIM_OC_InitTypeDef sConfigOC = {0};


/* USER CODE BEGIN TIM3_Init 1 */


/* USER CODE END TIM3_Init 1 */

htim3.Instance = TIM3;

htim3.Init.Prescaler = 6;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 255;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = 0;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN TIM3_Init 2 */

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);

/* USER CODE END TIM3_Init 2 */

HAL_TIM_MspPostInit(&htim3);


}


Input Capture Function

Input capture mode measures external signal timing.

It can detect:

  • Pulse width
  • Signal frequency
  • Time intervals

Common applications:

  • Ultrasonic sensors
  • Tachometers
  • Frequency analyzers

The timer records the counter value when an external edge is detected.


Encoder Interface Mode

STM32F411 timers support quadrature encoder decoding directly in hardware.

This feature is extremely useful in:

  • Robotics
  • Micromouse projects
  • Motor feedback systems
  • CNC machines

The timer automatically counts encoder pulses and determines rotational direction.

Benefits include:

  • Reduced CPU load
  • Accurate position tracking
  • High-speed response

   Example of practical application

void MX_TIM2_Init(void)

{


/* USER CODE BEGIN TIM2_Init 0 */


/* USER CODE END TIM2_Init 0 */


TIM_Encoder_InitTypeDef sConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};


/* USER CODE BEGIN TIM2_Init 1 */


/* USER CODE END TIM2_Init 1 */

htim2.Instance = TIM2;

htim2.Init.Prescaler = 0;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 4294967295;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

sConfig.EncoderMode = TIM_ENCODERMODE_TI12;

sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC1Prescaler = TIM_ICPSC_DIV1;

sConfig.IC1Filter = 0;

sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;

sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;

sConfig.IC2Prescaler = TIM_ICPSC_DIV1;

sConfig.IC2Filter = 0;

if (HAL_TIM_Encoder_Init(&htim2, &sConfig) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN TIM2_Init 2 */

HAL_TIM_Base_Start(&htim2);

HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);

// TIM2->DIER |= TIM_DIER_UIE;

/* USER CODE END TIM2_Init 2 */


}


Interrupt-Based Timing

Timers can generate interrupts at precise intervals.

Typical uses:

  • Real-time operating systems
  • Sensor sampling
  • Task scheduling
  • Communication timeouts

Instead of continuously checking the timer, the CPU responds only when an interrupt occurs, improving efficiency.

   Example of practical application

// Callback: timer has rolled over

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {


if (htim == &htim9) {

mice->miceUpdate();

}


}


/* TIM9 init function */

void MX_TIM9_Init(void)

{


/* USER CODE BEGIN TIM9_Init 0 */


/* USER CODE END TIM9_Init 0 */


TIM_ClockConfigTypeDef sClockSourceConfig = {0};


/* USER CODE BEGIN TIM9_Init 1 */


/* USER CODE END TIM9_Init 1 */

htim9.Instance = TIM9;

htim9.Init.Prescaler = 1000;

htim9.Init.CounterMode = TIM_COUNTERMODE_UP;

htim9.Init.Period = 200;

htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

if (HAL_TIM_Base_Init(&htim9) != HAL_OK)

{

Error_Handler();

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN TIM9_Init 2 */

HAL_TIM_Base_Start(&htim9);

TIM9->DIER |= TIM_DIER_UIE;


/* USER CODE END TIM9_Init 2 */


}


Applications of STM32F411 Timers

STM32F411 timers are widely used in embedded engineering projects.

   Robotics

   Timers control:

  • DC motors
  • Encoders
  • Servo motors
  • Sensor timing

   Micromouse robots heavily rely on timers for precise movement and wall sensing.

   Industrial Automation

   Timers are used in:

  • Motor drives
  • PWM inverters
  • Process timing
  • Encoder feedback

   Consumer Electronics

   Applications include:

  • LED drivers
  • Audio timing
  • Remote control decoding
  • Display refresh timing

   IoT Devices

   Timers help manage:

  • Low-power wake-up events
  • Sensor polling
  • Communication scheduling


Advantages of STM32F411 Timers

High Precision

Hardware timers provide accurate timing independent of software delays.

Low CPU Usage

Most timer operations run automatically in hardware.

Flexible Configuration

Multiple timer modes support various embedded applications.

Real-Time Performance

Timers enable deterministic and predictable system behavior.


Conclusion

STM32F411 timers are among the most powerful features of the microcontroller. They provide accurate timing, PWM generation, encoder interfacing, and interrupt handling for embedded systems. Whether designing a robot, motor controller, IoT device, or industrial automation system, understanding STM32F411 timers is essential for building efficient and reliable applications.

With advanced hardware features and flexible configuration options, STM32F411 timers remain a popular choice among embedded system developers and robotics enthusiasts worldwide.



Comments

Popular posts from this blog

How to Build a Micromouse Robot - Mechanical, Hardware, Software

Micromouse Competitions - Types, Overview, Comparison, Advantages, Worldwide

Complete Guide to Micromouse Maze - Dimensions, Structure and Components, Building