Timer interrupts using registers

12 Jun 2015

Hi,

I'm trying to set the timers and attach an interrupt using the registers on my Nucleo F334R8, to be able to go faster than with the Ticker class' methods and to set priorities between the several interrupts that I will need.

After a lot of research on the internet, here's what I have tried :

DigitalOut myout(PA_5);

int main(void) {

  SystemInit();

    __TIM3_CLK_ENABLE();

    TIM3->CR2 &= 0; // UG used as trigg output
    TIM3->PSC = 0; // no prescaler
    TIM3->ARR = 72000; // 1kHz
    TIM3->EGR = TIM_EGR_UG; // reinit the counter
    TIM3->DIER = TIM_DIER_UIE; // enable update interrupts

    // enable timer
    TIM3->CR1 |= TIM_CR1_CEN;

    
    NVIC_EnableIRQ(TIM3_IRQn); // Enable interrupt from TIM3 (NVIC level)
 
    while (1) ;
}

void TIM3_IRQHandler(void) // interrupt routine
{
  if(TIM3->SR & TIM_SR_UIF) // if UIF flag is set
  {
    TIM3->SR &= ~TIM_SR_UIF; // clear UIF flag
    myout=!myout; // toggle PA_5
  }
}

but it doesn't work... I've read the programmer reference of the STM32F334 for configuring the timer's registers but I didn't really understand the NVIC part (how to activate interrupt n°54 for exemple, or how to attach a routine to the interrupt...) I'm quite a beginner to all this : I've only used much more simple microcontrollers before.

Any help would be much appreciated!

12 Jun 2015

okay so I have figured it out : I had to attach my handler's adress using NVIC_SetVector(TIM3_IRQn, (uint32_t)&TIM3_IRQHandler);

12 Jun 2015

I assume TIM3_IRQHandler is the default one. You can use this without using the SetVector command by declaring it on top as extern "C".

However in general I would say the SetVector option is easier, in which case you can also use any name you want for your handler.

12 Jun 2015

I see, thank you for your informations! I've also noticed that when I set ARR registers to 72000 and the prescaler to 0, I get a frequency of 5.5kHz instead of the 1kHz that I'm supposed to have. I suppose that it means that the timer's clock frequency is not of 72mHz, but how can I know this frequency? Or how can I set it? I've looked into the RCC registers but I didn't really understand everything. Sorry for bothering you with my newbie questions!