Seminar Topics & Project Ideas On Computer Science Electronics Electrical Mechanical Engineering Civil MBA Medicine Nursing Science Physics Mathematics Chemistry ppt pdf doc presentation downloads and Abstract

Full Version: Reading Rotary Encoders
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Reading Rotary Encoders

[attachment=21122]

Introduction.

This application note covers the use of incremental rotary
encoders with PIC microcontrollers. It presents an example program in
TechTools assembly language for reading a typical encoder and displaying
the results as an up/down count on a seven-segment LED
display.
Background. Incremental rotary encoders provide a pair of digital signals
that allow a microcontroller to determine the speed and direction of a
shaft’s rotation. They can be used to monitor motors and mechanisms,
or to provide a control-knob user interface. The best-known application
for rotary encoders is the mouse, which contains two encoders that track
the x- and y-axis movements of a ball in the device’s underside.


The dotted lines in figure 1 indicate a common method of reading
direction. For instance, if phase 1 is high and phase 2 is rising, the
direction is clockwise (CW). If phase 1 is low and phase 2 is rising, the
direction is counterclockwise (CCW).
For the sake of interpreting this output with a PIC or other microcontroller,
it’s probably more useful to look at the changing states of the phases as
a series of two-bit numbers, as shown in figure 2 above.
When the encoder shaft is turning CW, you get a different sequence of
numbers (01,00,10,11) than when it is turning CCW (01,11,10,00). You
may recognize this sequence as Gray code. It is distinguished by the fact
that only one bit changes in any transition. Gray code produces no
incorrect intermediate values when the count rolls over. In normal binary
counting, 11 rolls over to 00. If one bit changed slightly before the other,
the intermediate number value could be incorrectly read as 01 or 10
before settling into the correct state of 00.
Interpreting this code amounts to comparing the incoming sequence to
the known sequences for CW and CCW rotation. A lookup table would
do the trick. However, this approach, while easy to understand, is
inefficient. The shortcut method uses an interesting property of the twobit
Gray code sequence.
Pick any pair of two-bit numbers from the CW sequence shown in figure
2; for instance, the first two: 10, 11. Compute the exclusive-OR (XOR)
of the righthand bit of the first number with the lefthand bit of the second.
In this case, that would be 0 XOR 1 = 1. Try this for any CW pair of
numbers from the table, and you’ll always get 1.
Now reverse the order of the number pair: 11, 10. XOR the right bit of the
first with the left of the second (1 XOR 1 = 0). Any CCW pair of numbers
will produce a 0.