Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

counters or interrupts for rotary encoder?

Status
Not open for further replies.

mamech

Full Member level 3
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
Hello

I am new to the world of interfacing with rotary encoders with pic microcontroller. I searched and I read some codes, and I see that the efficient way is to use interrupts, but my question is, will I need to use interrupt on change on A / B pins ? or I will need to use counter/timer TMR0 & TMR1 for example?

most of the code that I have seen are using interrupts, will not be more efficient if i use counter to count pulses?
 

I had seen most of the people uses external interrupt to detect roraty encoder outputs.
But that method doesn't worked for me, and then i used timer interrupt and recorded the sequence of rotary encoder outputs and based on that i had done rest of the part.
 
The way I usually do it is to use one signal to the interrupt pin and the other to any port pin. Each interrupt means the encoder was rotated and by immediately looking at the polarity on the port pin you can see which direction. 'Polling' the encoder inputs without using interrupts presents the risk that one of the signal changes is missed. It depends on the latency of the polling and how fast the encoder pulses arrive. For a hand operated encoder you can probably use polling, if it is an encoder on say a motor shaft where the pulse rate can be very fast, I would only consider interrupts.

Note that some PICs have built-in quadrature detectors which are ideal for sensing rotary encoder signals.

Brian.
 

'Polling' the encoder inputs without using interrupts presents the risk that one of the signal changes is missed. It depends on the latency of the polling and how fast the encoder pulses arrive. For a hand operated encoder you can probably use polling, if it is an encoder on say a motor shaft where the pulse rate can be very fast, I would only consider interrupts.

yes, i know this, that is why I excluded polling because I know that it is inefficient. actually the problem is that I am facing is to deal 7200 pulse per second encoder with a motor that rotates with 1.667 rotation per second , which equals : 7200*1.667 = 12000 pulse per second , and this can not be dealt with polling I think.

when I say to use counters/timers, I meant to use TMR0 as a counter to catch channel A, and to use TMR1 to catch channel B. I imagine that this will reduce much the interrupts that happens to the main program , because in this case interrupts will happen only when counter overflow happens.

the available micros that I have does not have built-in detectors, I deal mainly with pic16f887 and pic16f84a.

so I am still wondering, will using TMR0 and TMR1 as counters instead of external interrupt on pins be more efficient?
 

Not really, although they can count fast (~50 million counts per second) they cannot tell you in which direction the encoder is turning. A standard quadrature encoder produces equal numbers of pulses per revolution on it's A and B outputs and they are at the same speed so you gain nothing by counting both of them.

If you are only using the encoder output to sense speed (as a tachometer) you can use just one of the outputs and measure its frequency. The easiest way to do that is to use TMR1 as a pulse counter and TMR0 or TMR2 to count a time interval using the PICs own clock. From that you can derive the number of pulses per second.

If direction has to be checked, feed one output to the interrupt pin and as soon as possible in the ISR, check the polarity of the other input. In one direction the input pin wil be high, in the other direction it will be low. You can still measure the speed by counting how fast the interrupts occur.

Brian.
 

Software based AB-counting is o.k. for low speed, however it requires execution time in the microcontroller. For higher speed incremental position encoding we used a counter solution with integrated direction discrimination like https://www.ichaus.de/wp2_simple_measurement .

Enjoy your design work!
 
  • Like
Reactions: mamech

    mamech

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top