There are two kinds of shaft encoders available. The more expensive version has some number of parallel outputs that, taken together, provide a binary value encoding the shaft's angular position. For example, the encode might have eight outputs that could be read as a single byte by the computer. The resulting value, in the range 0-255, would code the angular position to within about three-quarters of a degree. (Each of the 256 steps advances by 1.4 degrees, so the maximum error made if the closest step were selected would be 0.72 degrees.)
Cheaper encoders provide no absolute position indication, but allow the computer to detect the direction and extent of shaft motion. If the position is known at a particular instant of time, the program can update the current position on the basis of the encoder's output. For many applications this behavior is perfectly adequate.
Monitoring human input is a typical use of a shaft encoder.
Humans are used to using knobs to control a wide variety of system
parameters, many of which can be considered as volume or frequency.
For example, the volume control on a radio, the intensity control on a
ceiling lamp and the temperature control on a heating system are all used
to tell a system how big something should be; the tuning control on a radio
tells it what frequency to receive.
Interface
Our shaft encoders are optical devices that provide two
logical outputs called A and B.
Conceptually, the shaft is attached to two discs.
Each disc has alternating transparent and opaque segments, 256 in all.
There is a light source on one side of the disc and a light detector on the
other side.
When a transparent segment of the disc lies between the source and
detector the corresponding output is 1, and when an opaque sector lies
between the source and detector the corresponding output is 0.
Thus the output alternates between 1 and 0 as the shaft is turned.
The sectors of the two discs are not lined up. Instead, one is shifted by half a segment angle with respect to the other. (This shift is not exact; it may vary from a quarter of the segment angle to three quarters of the segment angle, but in any particular encoder it is the same for all segments.) Because of the relationship between the two discs, the output signals are shifted with respect to one another:
Assume that the shaft is positioned so that the signals are those at the
midpoint of this diagram, where both A and B are
0.
If the shaft is turned counterclockwise from this position, first
A and then B will become 1.
On the other hand, if the shaft were turned clockwise from the midpoint of
the diagram then B would become 1 before A did.
This means that by observing the order of the changes, a program can
determine which way the shaft is turning.
The sequence of values is known as a Gray code.
By counting the changes, the program can determine how far the shaft has
been turned.
The sequence of 2-bit values maybe accessed by reading a byte from location $700001. The bits of interest are in bits 0 and 1. Also, the byte, and the 2-bit values are in negative logic. A "00" value is represented by a 1111 1111, while a "11" value is read as 1111 1100.
A single shaft can be used to control any number of quantities by providing some mechanism for ``mode switching'': telling the computer that from this point forward, motions of the shaft have a different effect.
The apparent sensitivity of the shaft can also be easily altered by multiplying or dividing the count by an appropriate value. Another approach is to keep a count of each motion in a particular direction. When a threshold value is reached, the shaft has been turned far enough to activate some activity in the application.
| Instructor | Revision 1.7 (2003/11/15 20:50:17) |