STM32F411 Timer Interrupt for Micromouse - Basic Timer Operation, Timer Clock Calculation, Interrupt Generation Process, Important Timer Registers,Types of Timer Interrupts, Timer Interrupt vs Delay Function, Best Practices
STM32F411 Timer Interrupt for Micromouse - Basic Timer Operation, Timer Clock Calculation, Interrupt Generation Process, Important Timer Registers,Types of Timer Interrupts, Timer Interrupt vs Delay Function, Best Practices
Timer interrupts are one of the most important features of the STMicroelectronics STM32F411 microcontroller. They allow the CPU to execute specific tasks automatically at precise time intervals without continuously checking the timer status.
Timer interrupts are widely used in:
- Robotics
- Micromouse systems
- Motor control
- Real-time operating systems
- Sensor sampling
- Communication protocols
- Embedded automation
A timer interrupt helps embedded systems perform periodic tasks accurately and efficiently.
What Is a Timer Interrupt?
A timer interrupt occurs when a hardware timer reaches a specific condition.
Common interrupt events include:
- Counter overflow
- Counter match
- Input capture event
- PWM update event
When the interrupt occurs:
- Timer sends an interrupt request
- CPU temporarily pauses current execution
- Interrupt Service Routine (ISR) executes
- CPU returns to the previous program
This mechanism enables precise real-time control.
Basic Timer Operation
A timer continuously counts clock pulses.
he counter increases like this:
When the counter reaches the Auto-Reload Register (ARR) value:
- Counter resets
- Interrupt event can occur
This creates periodic timing events.
Timer Clock Calculation
The timer frequency depends on:
- System clock
- Prescaler
- Auto-reload register
The timer update frequency equation is:
Where:
- = interrupt frequency
- = timer input clock
- PSC = prescaler
- ARR = auto reload register
Example Timer Interrupt Calculation
Suppose:
- Timer clock = 84 MHz
- PSC = 8399
- ARR = 9999
Then:
Result:
This means:
Interrupt occurs once every second
Interrupt Generation Process
The complete interrupt sequence works like this:
Step 1 — Timer Counts
The counter increments continuously.
When CNT = ARR:
Update event occurs
The timer sets an interrupt flag internally.
NVIC stands for:
Nested Vector Interrupt Controller
It manages all interrupts in STM32.
The processor jumps to the interrupt function.
Example ISR:
void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim2);
}HAL library usually calls:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
// User code
}
This function contains user-defined tasks.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim9) {
mice->miceUpdate();
}
}
Important Timer Registers
Divides the timer clock frequency.
Equation:
Larger PSC: Slower counting
Defines maximum counter value.
When counter reaches ARR: Update event occurs
Stores current timer count.
Controls interrupt enabling.
Important bit: UIE = Update Interrupt Enable
Contains interrupt flags.
Important flag: UIF = Update Interrupt Flag
Types of Timer Interrupts
STM32 timers support multiple interrupt types.
1. Update Interrupt
Occurs when:
- Counter overflows
- Counter underflows
Most common interrupt type.
Applications:
- Periodic tasks
- Time scheduling
2. Capture Compare Interrupt
Triggered when:
CNT matches CCR value
Applications:
- PWM timing
- Event synchronization
3. Input Capture Interrupt
Triggered by external signal edges.
Applications:
- Frequency measurement
- Pulse timing
4. Trigger Interrupt
Generated by synchronization events.
Applications:
Timer synchronization
Configuring Timer Interrupts
Following example for 500Hz timer
/* 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 */
}
Following example for 500Hz timer
/* 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 */
}
STM32 HAL Interrupt Workflow
Typical STM32 HAL timer interrupt structure:
HAL_TIM_Base_Start_IT()
↓
Timer overflow occurs
↓
TIMx_IRQHandler()
↓
HAL_TIM_IRQHandler()
↓
HAL_TIM_PeriodElapsedCallback()
This layered structure simplifies development.
Example: LED Blinking Using Timer Interrupt
A timer interrupt can toggle an LED every second.
Basic logic:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
}
Micromouse update logic:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim == &htim9) {
mice->miceUpdate();
}
}
Advantages:
- Precise timing
- Non-blocking operation
- CPU free for other tasks
Timer Interrupt vs Delay Function
Delay Function
HAL_Delay(1000);
Problems:
- Blocks CPU
- Poor multitasking
- Inefficient
Timer Interrupt
Advantages:
- Non-blocking
- Real-time operation
- Better system responsiveness
Timer interrupts are preferred in professional embedded systems.
Applications of Timer Interrupts
Robotics
Used for:
- Sensor updates
- Motion control
- PID loops
- Encoder calculations
Micromouse robots heavily depend on timer interrupts.
Real-Time Operating Systems
Timer interrupts provide:
- Task scheduling
- System tick generation
Communication Protocols
Used in:
- UART timing
- SPI synchronization
- Software protocols
Data Acquisition
Periodic sensor sampling:
- ADC triggering
- Signal processing
Interrupt Priority
STM32 supports interrupt priority management.
Higher priority interrupts:
Execute first
Lower priority interrupts:
Wait until higher priority tasks finish
Proper priority design is essential in complex systems.
Interrupt Latency
Interrupt latency is the delay between:
- Interrupt occurrence
- ISR execution
Factors affecting latency:
- CPU speed
- Interrupt priority
- ISR complexity
STM32F411 provides very low interrupt latency.
Best Practices
Keep ISR Short
Avoid heavy processing inside interrupts.
Good ISR:
- Fast execution
- Minimal operations
Avoid Delays Inside ISR
Never use:
HAL_Delay()
inside interrupt functions.
Use Volatile Variables
Shared variables should use:
volatile
to prevent compiler optimization issues.
Use Flags
Instead of heavy processing:
- Set a flag in ISR
- Process in main loop
Efficient approach:
volatile uint8_t timerFlag = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
timerFlag = 1;
}
Common Problems
Interrupt Not Triggering
Possible causes:
- NVIC disabled
- Interrupt source disabled
- Wrong timer configuration
Wrong Timing
Causes:
- Incorrect prescaler
- Wrong ARR calculation
- Clock configuration issues
Interrupt Flooding
Occurs when:
- Interrupt frequency too high
- ISR too slow
This can overload CPU performance.
Timer Interrupt in Micromouse Robots
Micromouse systems commonly use timer interrupts for:
- PID motor control
- Sensor polling
- Encoder updates
- Motion timing
Typical interrupt frequencies:
- 1 kHz control loop
- 10 kHz motor PWM
- Periodic sensor reading
Accurate interrupts are essential for stable navigation.
Conclusion
STM32F411 timer interrupts are a core feature for real-time embedded system development. They allow precise periodic execution of tasks without blocking the CPU. By using hardware timers together with interrupt mechanisms, developers can build efficient, responsive, and accurate systems for robotics, automation, motor control, and communication applications.
Understanding timer interrupts is essential for advanced STM32 programming and professional embedded system design.

Comments
Post a Comment