Try   HackMD

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

STM32 Timer Overview
STM32 timers are versatile and categorized as:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Each timer has:

  • Prescaler (PSC) – Divides input clock
  • Auto-reload register (ARR) – Sets timer period
  • Counter (CNT) – Current count value
  • Interrupts – Triggered on overflow (update event)

Example: 1-Second Timer Interrupt (Using TIM2)
Objective:
Generate a 1 Hz interrupt using TIM2 on STM32 (assume 72 MHz system clock)

Steps:
1. Clock Configuration
Make sure TIM2 is enabled:

c

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

2. Configure Prescaler & ARR

c

TIM2->PSC = 7200 - 1;     // 72 MHz / 7200 = 10 kHz
TIM2->ARR = 10000 - 1;    // 10 kHz / 10000 = 1 Hz (1-second period)

3. Enable Interrupt and Start Timer

c

TIM2->DIER |= TIM_DIER_UIE;   // Update interrupt enable
TIM2->CR1 |= TIM_CR1_CEN;     // Enable timer
NVIC_EnableIRQ(TIM2_IRQn);    // Enable TIM2 interrupt in NVIC

4. Interrupt Handler

c

void TIM2_IRQHandler(void)
{
    if (TIM2->SR & TIM_SR_UIF)  // Check update interrupt flag
    {
        TIM2->SR &= ~TIM_SR_UIF;  // Clear interrupt flag
        // Your code here: toggle LED, increment counter, etc.
    }
}

Key Notes

  • Timer clocks are on APB1/APB2, which may be prescaled – always check in CubeMX or reference manual.
  • You can use STM32CubeIDE or HAL libraries to configure this with abstraction.