To do this efficiently,
we'll need to establish a rule: The encoder outputs (A
and B) will be connected to the 74x165 in pairs, with the
A input in the LSB position as shown in Figure 1. By
using the 3.3K resistor in the data line to the Propeller,
we can power the '165 with 5V or 3.3V — selection of
Vdd may be affected by the encoders used.
■ FIGURE 1. Encoder to 74x165 connections.
For review, the quadrature encoder has four output
phases (Figure 2). Note that from phase to phase, only
one bit changes (gray code). Detecting movement is
easy: The newest reading will differ from the last. When a
change is detected, there's a nifty trick to determine
direction. The new reading is XORed with the left-shifted
old reading; bit 1 (B bit) of the result will hold the
direction (zero for CW, one for CCW). You can see this
played out in Figure 3.
Okay, then. Let's build an encoder reader. Have a
look at the start() method:
pub start(dpin, cpin, lpin, x4, p_limits)
enc0.byte := dpin
enc0.byte := cpin
enc0.byte := lpin
enc0.byte := x4
enc1 := p_limits
x4scale := x4
cog := cognew(@entry, @enc0) + 1
■ FIGURE 2. Encoder phases.
mode was specified, we
reverse the result bits.
Note that we have two more parameters: x4 and
p_limits. The first is a four-bit value (%0000 to %1111)
that enables the use of detented encoders that output
four phases between "clicks."
I was toying with the
use of a few encoders
when I came across
There are encoders that spin freely; this type is great
for wheel tracking on robots. There are others that are
better for the human touch; these have detents that allow
precision with small changes.
Duane's suggestion of
using the 74x165 to
minimize I/O pins for
encoders on a robot.
"Great idea!" I thought.
■ FIGURE 3.
Using a single 74x165
would allow me to read up
to four quadrature
encoders using just three
I have a detented encoder that outputs one phase
per click (Figure 4). I have others that output four phases
per click — this is where the x4 parameter comes in.
When using this type, we put a one in the respective bit
position of the x4; this will cause the object to divide
values coming out of the encoder by four so that we get
a change of one per click. Conversely, any values we
send into the encoder driver are multiplied by four to
ensure proper setup.
The p_limits parameter is a pointer to a DAT table
(of eight longs) that holds the low and high limits of each
of the encoders. Using longs we can, technically, have
14 March 2014