cancel
Showing results for 
Search instead for 
Did you mean: 

Counting 10000000 with 16 bit TIMER

hazall
Associate III

Hello,

I'm counting a 10MHz oscillator(with external clock mode 1) for 1 second using the stm32f0 processor's TIM1. I get about 10 000 000 pulses.

Then the system has a starting point and I start the 1s counting process here. The timer counts a maximum of 16 bits. I make 152 full rounds (update overflow) and stop and reset the Timer with the remaining 38680(152*65535+38680 = 10000000) with capture compare interrupt. However, the system is constantly shifting from the starting point (up to 70 microseconds). As a result of the tests I made with the logic analyzer, there is a problem where I reset the Timer. How do you think I can fix it and do you have any suggestions? I've been trying for a long time but couldn't solve it. Thanks everyone in advance.

12 REPLIES 12

ICould you explain briefly ?

The goal is obtain 1 second with 10 MHz high accuracy crystal oscilator. Without any time shifting acording to starting point(When the timer starts).

Thanks

  • if ARR=0xFFFF (i.e. maximum, which is the default), one full timer cycle is not 0xFFFF clocks but 0x10000, i.e.

EOSCloopVal = ExactOSCCount / 0x10000 ;

EOSCOverVal =ExactOSCCount % 0x10000;

  • as others said, don't zero TIM1->CNT in TIM1_CC_IRQHandler() , instead, leave timer running and calculate the next value for CCRx incrementally. Also, as also said, it's better to have a timer period set as integer fraction of the "big period" (e.g. set period to 50000, ARR=50000-1), so you have no remainder and thus have fixed CCRx value. You can also count the timer periods directly in the CC interrupt, that makes things simpler, you don't need to enable/disable the CC interrupt.

JW