Electronic – STM32F103CB Timer update interrupt not working

cstm32

I am trying to setup the timer update interrupt of a stm32f103cb. My setup and interrupt code looks as following:

void TIM2_IRQHandler(void)
{
    GPIOB->BSRR = 1UL << 1; // turn on led, works if function is called manually
    TIM2->SR = ~1;
}

void setup(void)
{
    RCC->APB1ENR |= 1; // enable clock for TIM2

    TIM2->PSC = 71;
    TIM2->ARR = 0xFFFF;

    NVIC->ISER[0] |= TIM2_IRQn; //TIM2_IRQn == 28
    NVIC->IP[7] |= 2 << 4;

    TIM2->SR = 0;
    TIM2->CNT = 0;
    TIM2->DIER = 1; // enable update interrupt

    TIM2->CR1 = 1; // enable timer
}

From my understanding TIM2_IRQHandler should be called, everytime the timer updates, but that does not happen. Some things i know:

  • The rest of the programm runs fine, the interrupt is just never triggered
  • The timer is running
  • The update interrupt flag in TIM2->SR is getting set correctly, everytime the timer updates

Am i forgetting something in the setup or is the error somewhere else ?

Best Answer

I figured it out.

NVIC->ISER[0] |= TIM2_IRQn;

should be

NVIC->ISER[0] = 1UL << TIM2_IRQn;

because to enable the interrupt you want to set bit 28, not write 28 as bit value to the register. Now it works as expected.