FIGURE 5. Detailed design of a quadrature encoder helper module.
simple to achieve the desired operations
mentioned above with a minimal effort.
The ideal microcontroller for the job
should be able to perform interrupt
driven serial communications. It should
also have an external interrupt capability
to serve as the Send pin (this way we
would not need a Ready pin). The speed
of the controller (MIPS) should ideally be
high. An example of a very suitable
controller is the SX available from
Parallax, Inc. Besides being very cheap,
the SX has all the desired characteristics,
and there are C and Basic compilers
available for it (though for this job
assembly is more suitable).
For the purposes of this article,
however, we will use the Basic Stamp 2
(BS2) available from Parallax (www.
parallax.com) because it is very
popular, well supported, and easy to
use. Besides, it has many advantages
during the research and development
stage. It is easy to program and has
very nice debugging features that can
be invaluable when you are testing new
ideas. Once the details of the design
have been honed and tested, the BS2 can
be easily replaced with any other (more
powerful and cheaper) µC like the SX.
Programming
The Microcontroller
FIGURE 6. Wiring P0, P1, and P4 for manual testing of the HM.
amount of IR light. The reference voltage serves to establish
a cutoff point for when the receiver will be considered as
blocked and when it is active (active is low resistance, i.e.,
lower voltage). The optional LED is not necessary for the
operation of the circuit but will serve as a visual indicator
during debugging.
Building The Quadrature Encoder HM
Figure 5 shows the detailed design of the desired HM.
The comparators (e.g., LM339) are used to compare the
voltage from the IR sensors with the voltage reference to
generate a TTL level digital signal for on/off. As the system
is designed, the signal reaching the µC will be low (0V)
if the sensor is not blocked and high (+5V) if it is.
Instead of a microcontroller as shown, we could use an
FPGA (clocked counters, flip flops, shift registers, and so
on). However, powerful and cheap microcontrollers that can
be programmed to do serial communications make it very
62 SERVO 02.2009
The PBasic program in Listing 1
keeps track of the quadrature states and
performs the logic described earlier. The
count is sent on the serial line high byte (MSB) first using a
communication rate of 9600 baud with eight bits, one stop
bit, and no parity. The BS2 keeps a count of the transitions
in a two-byte register. This effectively gives a possible count
range of -32513 to +32512. You should take this into
account when using the controller and issue a Reset if the
count is about to be exceeded. The negative values are in
2's complement.
Since the BS2 performs 4,000 instructions per second
and since the two bytes ( 16+ 4= 20 bits 9600 baud with
one stop bit and one start bit) will take 2.1 ms to transmit,
The maximum states transition rate must not exceed 1 per
≈2.5 ms. If we assume 5° spokes for good precision, (2.5°
resolution and ±1.25° precision), then to be able to use the
BS2 based HM the wheel must not rotate any faster than
2.5° per 2.5 ms. This is 1,000° per second, or 2.8 revolutions per second. As long as the wheel speed does not
exceed this rate, the BS2 based HM will be very suitable.
Obviously, if a µC with interrupt driven serial
communication and high clock speed is used the allowable