Solved
TIM2 CNT restart problem
Hi,
I am struggling with TIM2 to restart its CNT from zero without using hal.
I am using nucleo-f446
TIM2 clock frequency is 21MHz (no prescaler, 1/4clock division)
CNT set for upcounting.
I want to use TIM2 to write a simple us_delay function, so I need to restart its CNT
whenever us_delay is called. No matter what I do, I can't make it work, here is the code
I am using:
TMR2_Start = 1; //start counter
TMR2_URS = 1; //Only counter overflow/underflow sets UIF bit
TMR2_UDIS = 1; //disable update event generation
TIM2->ARR = 2100; //set 100us delay (tim2 clock frequency = 21MHz)
TMR2_UDIS = 0; //enable update event generation
TMR2_UG = 0x1; //restart counter
while (TMR2_UIF == 0){ //wait until TMR2_UIF is set
}I put a breakpoint right before while loop is executed, I can see that CNT is enabled,
URS set, delay value loaded to ARR. However CNT is not reset to zero, it has a random
value, sometimes as high as 0x1A9C, plus the UIF and the Capture/Compare flags are
all set for unknown reasons.
Also, TIM2->CNT = 0 does not help
