cancel
Showing results for 
Search instead for 
Did you mean: 

Rotary encoder interrupt per rotation

SBrid
Associate II

Hi all.

I have a rotary encoder which generates 512 ticks per rotation and I would like to generate a single interrupt once the rotary encoder has done a full turn (512 ticks). What would be the best approach? I'm currently looking to see if the STM32 encoder mode supports this or I will have to use some type of input capture?

Thanks

3 REPLIES 3
Peter BENSCH
ST Employee

You could set the prescaler to 512-1, if you really want to get one interrupt per revolution.

Of course you wouldn't get any interrupt when the encoder turns e.g. ±350° alternately.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

> You could set the prescaler to 512-1,

I personally wouldn't set the prescaler to other than the default value of 0 in encoder mode. While that actually *would* result in CNT increment after 512 edges (according to particular mode setting) *regardless* of direction and its changes, it practically makes the *encoder* function useless.

What Peter probably meant was setting ARR to 512-1, that would after turning in one direction cause Update event and thus, if enabled, interrupt.

Perhaps a slightly better idea is to connect one of the signals to a second timer's input channel, and set that timer to external clock mode. You can do the interconnect internally, through the TRGI-TRGO mechanism.

JW

Or a second TIM in encoder mode but with a finite ARR setting so you get an Update interrupt for each rotation.

Figure the secondary could be a 16-bit where the count fits, and the primary is 32-bit for the long-count.

You'll need to find a way to zero the short-count, of an end-stop or other means.

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