cancel
Showing results for 
Search instead for 
Did you mean: 

Automatically count number of output compare events (without interrupts)?

pflunzi
Associate III

I set up Timer1 Ch4 and Timer2 Ch4 to toggle their Pins (PE14 and PE11) on Capture Compare. I use this as a step signal for stepper drivers with high microstepping (256x).

I recognized that when I activate the interrupts to count the number of pulses at high frequencies (>500kHz per Timer) a lot of CPU time is used to jump into the interrupt routine and clear the flags.

Is there a way to e.g. internally count via a counter the capture events, to count them without calling an interrupt routine?

I only found a solution to directly solder the step pin to an input pin of a low power timer and use them to count external clock events.

Any more elegant solution?

7 REPLIES 7
TDK
Guru

Yes, there is a better way.

  • Set up a timer.
  • Set a channel of this timer in IC (input capture) mode and hook up the channel pin to the STEP (or DIR) signal. You can set a channel to trigger on the rising edge, falling edge, or both.
  • Set a DMA channel to take events from this channel and put them into a circular buffer. This will write the TIMx->CNT value at the time of the event to the buffer.

Now you have a CPU-free way of capturing edges of a signal. Not only the number of edges, but also the time at which they occurred.

You will need to monitor the buffer periodically before it overflows and see how many events are there. You can do this by looking at the NDTR register of the relevant DMA channel.

If you feel a post has answered your question, please click "Accept as Solution".
pflunzi
Associate III

Thanks for your fast reply.

But I cannot find out how to "hook up the channel pin to the step signal". I can only take several pins as input capture, but not the same as for the output compare toggle pin.

Or do you also mean to solder those pins together?

Also just to clarify, I already have a PCB and the step pin is already connected. I just did not think about the amount of interrupts I would generate.

TDK
Guru

> "hook up the channel pin to the step signal". I can only take several pins as input capture, but not the same as for the output compare toggle pin. Or do you also mean to solder those pins together?

Yes, I just mean the timer channel pin should be connected/soldered to the STEP signal.

You want input compare mode here since you're monitoring the signal, not output compare. Output compare is for PWM generation.

If you feel a post has answered your question, please click "Accept as Solution".
pflunzi
Associate III

OK. You mean to monitor the pin I should use an input capture from my timer. I understand that.

I thought I could maybe somehow use some internal connection since I am also using a timer and its output compare mode to generate this signal. Thats why I thought maybe I don't have to produce this signal via a capture compare with one timer on one pin, and use an additional pin with a timer to input this signal and capture it.

Because if I solder an additional pin to the step signal, I think the input capture does not have an advantage over the lowpower timers, which can be set up to also capture edges.

You can output only one of the OCxREF as TRGO, not both, see TIMx_CR2.

That then could be input to some of the connected timers, see interconnection chapter in RM, and the TIMx_SMCR register for the chosen slave timer.

JW

pflunzi
Associate III

0693W000000WoNdQAK.pngOne trigger is fine for me. I still can reconfigurate the stepper driver to only act on a rising edge.

I attached the matrix from the RM.

I could take Tim8 as slave for Tim2 via ITR1 and take Tim5 as slave for Tim1 via ITR0.

I put both timers in "External clock mode 1" and then they should count up on every event. This sounds nice.

I will try this tomorrow (I will give feedback then, it is pretty late here right now).

Thanks alot

pflunzi
Associate III

Thanks again, works perfectly :)