cancel
Showing results for 
Search instead for 
Did you mean: 

Update interrupt flag goes high as soon as counter is enabled

Onkar Chincholkar
Associate II
Posted on January 15, 2018 at 14:36

I am new to STM32 controllers and I wanted to create a sample code for counter in TIM1. I am facing a problem that the update interrupt flag, which is supposed to go high when counter equals ARR register, is going high as soon as counter is enabled. I am using discovery F0 board and giving a simplified version of my code

void main(void)

{

    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;

    

    TIM1->PSC = 0xff;

    TIM1->CNT &= 0x0;

    TIM1->ARR = 0xffff;

    

    Glow LED1

    

    TIM1->CR1 |= 0x0001;

    while(TIM_SR_UIF == 0);

    

    Turn off led

}

Thank you

Regards

Onkar

#beginner #stm32-f0
7 REPLIES 7
Posted on January 15, 2018 at 15:07

Yes, the Update condition is (CNT == 0), not (CNT == ARR)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 16, 2018 at 08:42

Thank you for the update condition. If I change while(TIM_SR_UIF == 0); to while(TIM1->CNT != some_number) I can run my program.

Also I observed that for 8Mhz crystal, if I set the prescalar as 8000 and wait till the counter reaches 1000(ARR register is set 0xffff), I do not get 1 sec delay. Thank you for patient reading.

Regards

Onkar

Posted on January 16, 2018 at 09:06

Prescaler has to be one less than the desired dividing ratio, i.e. 7999 in your case.

JW

Posted on January 16, 2018 at 09:56

Thank you Jan for that information. Still my delay is nowhere near 1 sec. It is about half second rather.

Regards

Onkar

Posted on January 16, 2018 at 10:55

Sorry I did not get you at the first time. Thank you for pointing that out. I have increased the prescalar and changed the lines to

TIM1->CNT = 0x0+1;

TIM1->ARR = 0xfff+1; in my code above but still I observe the same behavior in the debug mode.

Regards

Onkar

Posted on January 17, 2018 at 23:43

I don't see you mentioning which STM32 model are you using, but look into RCC chapter, if you use APB divider other than 1, it's likely that timers run at twice the frequency of APB they are on.

JW

Posted on January 23, 2018 at 06:41

Controller used - STM32F051R8T6(Discovery board)

IDE used - Attolic truestudio

I observed that the values in pllmul = 10, apb = 0 and ahb = 0. So since the chip uses internal oscillator ticking at 8MHz, according to the clock tree it is divided by 2 and because of multiplication with the pll becomes 40Mhz. Now even if I use a prescalar of 40000 in the timer1 and wait for counter to reach 10000 for blinking I observe almost 8 seconds of delay in the blinking, should be 10 according to my calculations.

Regards

Onkar