Showing results for 
Search instead for 
Did you mean: 

Encoder Trigger output at every transition

Associate III
Posted on June 03, 2013 at 17:01

I have a timer configured to interface an encoder.

Basically what I want to do is to create a trigger event at every encoder pulse, which resets another free running timer's counter value to 0.

What I have now, is that I configured the encoder timer as master, the other timer as slave. TIM_TRGOSource_OC4Ref is set as output trigger. But now I obviously have to set the Capture/Compare 4 register of the encoder timer manually right after the capture happens.

The problem with this is, that at very fast encoder speed the CCR4 register has to be changed very often, resulting in a very high CPU usage.

What I've come up with is to set the Auto Reload Register of the encoder timer to 1, to be able to use the TIM_TRGOSource_Update for event generation. The obvious flaw of this method is, that I would loose the direction information of the encoder rotation.

If you are wondering why I would need to have a trigger at every encoder transition, then please see this paper:


I also modified a picture from the paper for clarity on my method to implement the speed measurement:

If anyone has ideas, regarding the problem of the encoder transition triggering, or how to implement the mixed mode measurement, I am more than happy to hear them.

Posted on June 03, 2013 at 17:33

Strikes me that you could use an input capture mode, use that to snap shot a higher frequency timebase, and then do a delta measurement of the CCRx register samples of successive CCx interrupts.

More crudely you could use an EXTI interrupt on the pin, and a counter via SysTick.

Generally you wouldn't need to zero things, if you know the starting value and ending value the delta is cheap to compute.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Associate III
Posted on June 03, 2013 at 18:54

Thanks for the fast response, I'll consider this.

And BTW I forgot to mention the used uC, it is the STM32F373RC

Associate III
Posted on June 04, 2013 at 18:23

Thanks clive1 !

I did what you said, I've set up an EXTI interrupt for both encoder channel inputs.

In the interrupt I just save the actual timer value. When the encoder speed measurement timer fires, I can subtract the time when the last encoder transition happened.

Just what I needed.

I always forget that you can set up an interrupt to a pin, even if it used as an alternate config pin. Pretty neat!