cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L031 How to use TIM2 as a 32 bit 32MHz with a 200ms period counter

Hugo Balette
Associate II
Posted on November 14, 2017 at 17:54

Hi everyone,

I used to work on STM32F407VG to develop a micro-sec timing application.

I'm reading on multiple Digital GPIOs configured as input, and measuring elapsed time between rising edges arriving on different GPIOS input.

Using STM32F407, I was triggering systick counter when I got the first rising edge, reading systick value while new rising edges arrives, and stop it after 200ms elapsed.

To adapt micro-controller size/price to my application, I'm now using STM32L031, and I'm facing problems : I can't get a precise systick counter: If I'm setting up systick to toogle a pin every 1us, it isn't precise at all.

It works fine for toogle pin every 1ms, but it's loosing precision if I try to get 1us.

I'd like to use TIM2 as a 32bit 32MHz counter, with a period of 200ms, but I can't manage to configure it as a 32bit counter.

Would you have any idea to help me ?

Thanks for your time, and have a good day !

Balette

#32bit-timer #microsecond #stm32l0 #timer
4 REPLIES 4
Posted on November 14, 2017 at 18:07

TIM2 is 16-bit on the L0

You're not going to be able to fire SysTick at 1us (1MHz), this type of interrupt rate is insane.

Use a TIM in toggle mode to transition a pin every 1us. TIM period/prescaler are programmed with N-1 values.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on November 14, 2017 at 18:51

Take a timer. When overflows, get a high priority interrupt to increase a global 16 bit variable (this will emulate a 32 bit timer). Use the input captures of the timer(s) to get all edges you need from multiple GPIOs. Use the HW for the time critical job and when the time stress is relaxed, emulate the rest by SW properly. If you need 128 bit timer with 128 bit input capture, you can. if the timer is at 1us per tick, the overflow will occur every 65 msec...

Posted on November 14, 2017 at 18:19

Thanks for your quick response Clive One !

My bad, I thought TIM2 was able to be configured as a 32bit timer.

So to count microseconds with a period of 200ms, I have to increment a variable every time the timer is overflowing, and stop counting when it's done isn't it ?

Which example should use to start using timer as a counter ? there is a lot of examples, but I don't know which one to use :\

Thanks again, and have a good day !

Posted on November 15, 2017 at 05:15

Input capture mode example from the corresponding nucleo. Get the Timerupdate interrupt and generate the extra 16 bit MSB by a single global variable. When you get input capture, concatenate the 16 bit ram MSB timer value with the 16 bit compare value and save it in RAM arrays for the basic mode of operation. Be rigorous to manage the timer overflow event (if capture comes just before or after, respective to the ISR that increment the 16 bit MSB variable).