Figure 1. PICAXE-14M pin-out
“pulsin” command to compute the
distance to the object. However, the
EZ1’s analog voltage output is even
simpler. According to the datasheet
(
www.maxbotix.com/uploads/
LV-MaxSonar-EZ1-Datasheet.pdf),
the EZ1 outputs an analog voltage
with a scaling factor of (Vcc/512) per
inch. Since the 14M’s “readadc10”
command divides Vcc into 1,024
steps, all we need to do is double the
ADC reading to get the distance to
the object in inches — it doesn’t get
any easier than that!
First You Have to
Eat Your Lima Beans
Of course, the first thing you
need to do is get your hands on one
of the MaxSonar EZ ultrasonic range
finders. While you are waiting for it,
we need to talk a little about the
14M’s I/O lines. (You can refer to the
14M pin-out in Figure 1 throughout
the following discussion.) Up until this
point, we have only been using the
14M’s default I/O configuration. In
other words, the I/O pins on the left
side of Figure 1 are all inputs by
default and the I/O pins on the right
side are all outputs by default. (In all
PICAXE documentation, the default
function of an I/O pin is always listed
closest to the pin.) As you can see,
two of the pins on the left (pins 3 and
4) are fixed as inputs and three of the
pins on the right (pins 11, 12, and 13)
are fixed as outputs. All of the 14M’s
other I/O pins (pins 5 through 10)
can be either input or output pins,
depending on your program’s
requirements.
Also in Figure 1, pins 5 through
10 can be accessed as C5 through
C0 (because they make up what is
referred to as port C on the 14M). In
order to reconfigure any combination
of these pins from their default state,
we can use the “dirsc” command. For
example, if we want to reconfigure C0
as an input (which we will actually do
when we get to our EZ1 programs),
we would include the command “dirsc
= %00000110” at the beginning of
the program. The digits are in the
standard “bit7, bit6, … bit0” order. In
other words, the left-most digit stands
for C7 and the numbers decrease as
you move to the right.
Of course, C7 and C6 don’t exist
on the 14M but they do on other
PICAXE chips — which is why they are
included in the command. On the
14M, the standard convention is to
always place a 0 in each of the
non-existent positions. The eight-digit
number assigned to dirsc is binary
(the symbol specifies that) and
for each digit a 0 means that the
Figure 2. Close-up of BrainAlpha I/O labels.
corresponding pin will be
an input and a 1 means it
will be an output.
If you examine the
eight-bit binary number
we just assigned to dirsc,
you can see we have
specified that C5, C4, and
C3 will be inputs (which
they are by default
anyway), so nothing has changed
there. In addition, we have designated
C2 and C1 as outputs (which is
also their default state), so the only
change we have made to the port C
pins is to reconfigure C0 as an input.
Figure 2 is a close-up photo of
the I/O labeling on a bare BrainAlpha
PCB (printed circuit board). If you have
been wondering about the purpose
of the small carets that point up and
down on the labels of the I/O pins,
the above discussion should clear up
the mystery. The I/O pins on the right
are the 14M’s default output pins.
(Outputs 4 and 5 are not there
because they are being used for the
two servomotors.) Output 3 has a
caret pointing in both directions (up =
out and down = in) because it can be
reconfigured as an input. Similarly, on
the left side, inputs 0, 1, and 2 each
have carets pointing in both directions
because they are the 14M inputs that
can be reconfigured as outputs.
A word of warning is necessary
at this point. On any microcontroller,
whenever you reconfigure an
output as an input it’s important to
remember that during the brief time
between booting the controller and
reconfiguring the output pin, that
pin will be in its default (output)
state. Most likely, the pin (which
will quickly become an input) will be
connected to some other output. This
is a potentially disastrous situation
— if the other output happens to
be high and your (soon to be
reconfigured) output happens to be
low (which is the typical power-up
situation), this creates a direct short
that is likely to damage or destroy
one or both pins. The solution is to
68 SERVO 07.2009