cancel
Showing results for 
Search instead for 
Did you mean: 

How to correctly set up a micro second timer?

areify
Associate II

Hi there.

I know this question is already asked, but the topics I've read didn't worked for me, so I'm writing this in order to study mi concretely case.

I'm setting up my timer using TIM1 with the next parameters:

//Prescaler for 1 us
  uint32_t PrescalerValue = (uint32_t)(apb1clock / 1000000) - 1;
 
  //Other parameters
 timer_us_timhandle_.Init.Period      = 1 - 1;
  timer_us_timhandle_.Init.Prescaler     = PrescalerValue;
  timer_us_timhandle_.Init.ClockDivision   = 0;
  timer_us_timhandle_.Init.CounterMode    = TIM_COUNTERMODE_UP;

Where apb1clock is 9 MHz.

Am I doing wrong? Maybe a concept problem?

The result I'm having is that main line of code execution cannot go on because of the continous ISR calling. Am I overweighting my STM? (STM32F105VC).

P.S.: After the ISR call I set the flag to 0.

Thank you in advance.

19 REPLIES 19

> timer_us_timhandle_.Init.Period = 1 - 1;

You have to set Period to nonzero.

Besides, interrupt entry/exit is around 24 cycles, so it's definitively a bad idea to try to invoke an interrupt every 9 cycles.

JW

areify
Associate II

So is not possible with 9MHz to set my microsecond timer? I have to use a faster speed for TIM1?

It's not practical to have microsecond *interrupts* even at 100MHz clock.

But what would you want to do in those interrupts? If you just want count microseconds, set the period to the maximum value and leave it running freely, with no interrupts.

JW

areify
Associate II

I need to generate a 1us ISR in order to link with a library for communications which works at 9600 bauds (so microseconds are needed).

I can't count to 104 because the callback I need to link is ready for a 1us interruption cycle

A software UART?

You can't do interrupts or callbacks at this rate.

Best to use freerunning TIM or DWT_CYCCNT to count off cycles in linear code.

Consider running in RAM, FLASH is a bit of a sloth on the F1 series.​

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

> callback I need to link is ready for a 1us interruption cycle

That's then a completely irrationally written library, and you should talk to its author and ask for comment.

I'd like to hear his comments here.

JW

Already at the intersection of Bad and Worse

0690X000006CpmxQAC.jpg

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

Sorry for not answering at all this time. I was modifying the library so now I can play correctly with timers.

I'm working with apb1 at 9MHz as I used to do. I'm setting up my TIM3 as a 1MHz pulse signal (every count has 1us width).

Now my ISR get called every X seconds (where X is the period value that I can set up).

I need to correctly count to 52, 100 and 104us. But if I set my period as 4 (the Maximum Common Divisor of those values) I have the same problem that was having: interrupts are called very fast

(every 4us).

I need:

- To work with interrupts. THIS IS VERY IMPORTANT. Sequential code is not valid for this case.

- To have an accuracy count of time.

- To have 'paralelism'. I mean: maybe I could be counting to 52 at the same time I'm counting to 100, for example.

Is possible to achieve all this objectives?

Thank you.

You can accurately measure time from a free running timer.

If you succeed at interrupting at 1 MHz, it will be ALL the processor is doing.

Perhaps think this through, and consider a CPLD for tight signal recovery. ​

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