finding the zero position of the motor shaft which would
be a problem if he tried to count only one revolution.
One last thing. While red+/black- is the usual power
wiring for this type of device, there are variants especially in
custom devices — sometimes they obfuscate the wiring on
purpose. Sam should put a 1/4 watt resistor of perhaps
100 ohms in series with the power (red) lead and slowly
ramp up the voltage across red and black to about 5V. This
will limit the current to 50 ma and the power to 1/4 watt
in the event the power wiring is incorrect and he ends up
driving a short or near short. The encoder should be able to
survive reverse voltage if current is limited to this 50 ma
level, for a few seconds at least. If the circuit draws more
than 25 ma with less than about two volts across red to
black, he needs to disconnect power and re-evaluate.
— Mark Lewus
A. Thanks for the kudos! Thanks also for sending in this useful information that will help Sam and other eaders who may be scratching their heads after
picking up what they thought was a bargain. With a little
ingenuity, bargain hunters can use your information, their
new acquisition, and the July Mr. Roboto column where
I describe how to make use of quadrature encoder
information to determine speed and direction of a motor.
It takes discoveries and effort in several areas before a
project can really come together.
Q. I have two questions. First, I am trying to get two PICF18458s to chat via SPI, and am having erratic results. I realize the error is mine; any common
errors you can talk about? Second. I need multiple 458s to
chat together. Is CAN BUS the best option? If so, can you
suggest a good reference for CAN BUS techniques?
— Rod
A. You don’t say specifically what problems you are having linking two PICs together with SPI (Serial Peripheral Interface), which — by the way — will work
fine. I’m going to assume that the issue you are having is
that you want any of the PICs to be able to initiate a
connection to the other. This would be a problem because
SPI requires there be one master on the bus and that all
other devices are slaves. In other words, only one of the
processors on the SPI bus can originate a conversation;
everyone else can only respond to a request from the
master. The SPI specification is basically just a hardware
specification. This means that it only dictates how the clock
signals are made, what I/O lines are needed, and what they
do. It is a pretty simple protocol. Table 1 shows the I/O line
names and descriptions for SPI.
You don’t say if you are using an SPI hardware module
in the PIC or if you are bit banging (forming the clock and
data signals in firmware with code). If it’s the latter, then
what you could be running into is timing issues due to how
long you let the data settle — or sit on the I/O line — before
you assert the active edge of the clock. Every synchronous
14 SERVO 08.2009
SPI
Signal Name
Description
SCLK
Master clock signal
MOSI
Master Output, Slave Input
MISO
Master Input, Slave Output
SS
Slave Select (active low)
Table 1. SPI signal definitions.
bus specification will define a settling time that allows data
to be ready to be read as soon as the active edge of the
clock is seen by the device. The active edge of the clock is
the edge (rising or falling) that is to be used as the trigger
for reading data on the data line MISO or MOSI. If you
don’t meet that settling time, then your data could be
corrupted when read in because the I/O line will see the
wrong logic level.
This settling time is especially critical if you have really
long lines (or wires) between your devices. What happens
with really long wires or really high frequencies is that the
resistance of the wire and the capacitance of the wire will
conspire to give you an RC (Resistor x Capacitor) charge or
discharge curve that will slow down the rise and fall time
of your data and clock lines. If you have really long wires
(more than 25 cm or so), then you might consider slowing
down your clock speed and/or allowing more settling time
on your data lines before asserting the clock. See Figure 1
for a graphical explanation of what happens when your
data and/or clock lines have RC influenced rise and fall
times. Another potential problem spot for you is the SPI
mode. SPI has four modes defined; these modes determine
whether the device reads and/or writes on the falling edge
or the rising edge of the clock. Table 2 shows what these
modes are and what they mean.
If one device is configured Mode 0 and another is
configured Mode 1, then you will have problems with
reliably transferring data.
You asked about CAN bus. The CAN bus was designed
to be robust and noise-resistant data bus with fairly long
transmission lines to control devices in a car. It does that
Figure 1. Long wire signal distortion.