cancel
Showing results for 
Search instead for 
Did you mean: 

Non interrupt driven ms timer

thommagn
Associate II
Posted on July 03, 2012 at 09:43

Hi,

I'm currently trying to implement a non interrupt driven millisecond timer. I have alot of other interrupts and functions, which i do not want to interrupt to increment a timer value. Also, i've already implemented alot of functionality using the different clocks, which is why i do not want to change my APB prescaler.

Is there any other trick I can use to implement this type of timer?

Thanks!
4 REPLIES 4
frankmeyer9
Associate II
Posted on July 03, 2012 at 10:41

...i do not want to interrupt to increment a timer value

 

Incrementing a variable does not cost much time.

You did not mention which STM32 derivate you are using, but even the F0 does have interrupt priorities.

I suggest assigning different interrupt priorities, with the least to your millisecond timer.

Posted on July 03, 2012 at 12:05

Not sure I understand the APB issue here. You have no free TIM units, or have some weird external clock?

You don't have to generate interrupts, you could still look at the update flag to see if a timer rolled, and poll that occasionally. You could look at a number of free running timers (RTC fractional, TIM CNT, DWT_CYCCNT) and see if they had advanced beyond a specific target, and then advance the target (1 ms worth of ticks).

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thommagn
Associate II
Posted on July 03, 2012 at 14:01

Sorry for being unclear.

I'm using stm32f4 discovery. The timer frequency is set to 84MHz, and maximum prescaler is 0xFFFF which means that I can't scale it down to 1MHz.

I have time consuming functions, so I don't have a natural way of polling the flags.

The reason for this timer is that I need long period timings and must keep track of this time with millisecond precision.

My reasoning about APB1 is that this is the clock that drives TIM2, and if i reduce the frequency of this clock I reduce the 84MHz timer, if I understood that correctly?

// Thom

Posted on July 03, 2012 at 14:41

Well yes, polling/comparing a counter value will probably consume more cycles than entering/exiting an interrupt.

Consider using the SysTick.

Also you don't understand the timers quite right. The update interrupt is a combination of the both the prescaler and period, so you have plenty of range to get 84 MHz to a 1 ms tick, 84000 can be factored to 84 and 1000, set the prescaler to 84-1 and period to 1000-1 and the update interrupt will occur at 1ms periodicity.

Also you have the 32-bit DWT_CYCCNT in the trace unit which ticks at the core clock, figure it will wrap every 25.5 seconds, but has very fine granularity. You can use this for fractional units for other timers/counters.

The RTC also has a prescale register which should be readable, with units of 1/32768th of a second, say 30.5 us ticks. More precision than you need, but probably not clean decimal units.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..