2018-01-15 05:36 AM
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-f02018-01-15 06:07 AM
Yes, the Update condition is (CNT == 0), not (CNT == ARR)
2018-01-16 12:42 AM
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
2018-01-16 01:06 AM
Prescaler has to be one less than the desired dividing ratio, i.e. 7999 in your case.
JW
2018-01-16 01:56 AM
Thank you Jan for that information. Still my delay is nowhere near 1 sec. It is about half second rather.
Regards
Onkar
2018-01-16 02:55 AM
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
2018-01-17 03:43 PM
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
2018-01-22 10:41 PM
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