A-B encoder / Still looking for code

Chris Conlon synchris at ricochet.net
Wed Apr 19 21:24:16 GMT 2000


At 09:07 AM 4/15/00 +0000, Mike Blakey wrote:
>I'm still looking for some pseudo code for a A-B encoder.

>The encoder only has two squarewave outputs A and B, when rotating
>CW, A leads B and when rotated CCW, B leads A by 90deg.

These are very simple to use, I'll try to explain it w/o using any
ascii-art drawings, but do grab a pen and paper.

Draw a few cycles of the A and B waveforms, one above the other. A
is just a squarewave, and B is a 1/4 cycle delayed version of A.
(Adjust as needed to fit your exact encoder.)

The 2 key features are these:

A and B transition separately, they never both change at once. This
is a Grey code.

The pattern is essentially reversed when the encoded is rotated the
other way. Just read the diagram backwards, from right to left. Rising
edges become falling edges and vice versa.

All you need to do is check the status of B, on each rising edge of A.
If the gizmo is going one way, B will be low on each rising edge of A;
in the other direction B will be high.  This only gives you 1/4th of
the device's intended resolution, but a similar logic applies to each
edge (rising and falling) of A and B, just the polarities differ. If
you look at B during the *falling* edge of A, the polarity will be
opposite to that during the *rising* edge of A, for the same direction
of rotation.

If this is getting mixed up verbally, just look at the drawing. In a
minute you can build a simple table of how each edge/level combination
relates to a direction.

If you want to use the full resolution, you also need to check the
status of A, on both edges of B. Since you mentioned pseudocode I'm
guessing you're after a software solution. Should be very easy.
Each edge of A or B means something moved (CW or CCW), you just need
to check the level of the other signal to tell which way.

If you're polling in software (for some reason) another approach is
to simply use B:A as a 2 bit value. It will count 0-1-3-2 in one
direction, and 2-3-1-0 in the other. Keep track of the last state,
read the current state, and you can tell which way it turned... as
long as you don't miss states.

If you want it in hardware it's a bit more annoying, at least to use
the full resolution. If you're content with 1/4 or maybe 1/2 res,
it's pretty easy again.

At this point I might mention that a couple of vendors (Analog Devices
and Xicor I think) make digitally controlled trimpots with
"count up 1" and "count down 1" inputs. (If it's not either of those
guys, drop a line, I know I have that info here *somewhere* and can
dig it up if needed.)  Maybe useful for people faking a TPS output.

How you reset to zero... that's a different problem.


If this was helpful I'd like to get some info on the encoders you're
using, mfg/part #, cost/source, do you like them etc? (And the servos
you use or plan to.) I imagine I'll need these sorts of things someday
and if I can learn from another's experience so much the better.

   Chris C.

----------------------------------------------------------------------------
To unsubscribe from diy_efi, send "unsubscribe diy_efi" (without the quotes)
in the body of a message (not the subject) to majordomo at lists.diy-efi.org




More information about the Diy_efi mailing list