www.nutsvolts.com/index.php?/magazine/article/december2010_PICAXEPrimer
PICAXE PRIMER
automatic switching among
program threads will occur so
rapidly that it will create the
illusion of multitasking.
EXPERIMENTING
WITH THE 18M2
TOUCH
SENSORS
We’ll explore many of the
18M2’s new hardware and
software features in upcoming
installments of the Primer. This
month, we’re going to start with what
I consider to be the most exciting
new feature: the capacitive touch-sensitive inputs. In order to do so,
we’ll conduct a series of simple
experiments to familiarize ourselves
with the capabilities and
characteristics of the new touch inputs.
Before we get to our
experiments, however, let’s take a
quick look at the 18M2 pin-out (see
Figure 2). The first thing you may
notice is that the locations of the
power, ground, sin, and sout pins
don’t follow the pattern of the older
M-class processors; the pin
arrangement is much more in
keeping with that of the 18X
processor (which, along with the
18M, has been superceded by the
18X2). As a result, some care is
necessary when setting up a basic
18M2 breadboard circuit, especially
when connecting the + and – power
pins. Secondly, the pin function that
is closest to each pin name is that
pin’s default function. As you can
see, almost all the pins default to
general-purpose inputs – this is a
safety feature that’s designed to
prevent outputs from being
improperly initialized at startup. If
you have worked with the 20X2
processor, you’re already familiar
with this concept.
Another 18M2 feature has been
adopted from the X2 processors: the
“port dot pin” naming convention; for
example, the name B. 7 identifies that pin
as portB, pin7. This same pin-naming
convention will also be used on all
the subsequent M2-class processors.
As a result, the way that a program
■ FIGURE 2. 18M2 pin-out.
refers to a pin (e.g., high B. 7) will be
standardized across the entire line of
PICAXE processors which greatly
simplifies the process of writing
programs for different processors.
If you don’t already have an
18M2 on hand, now would be a
good time to order a couple. I
ordered my first 18M2 processors
directly from RevEd in England on the
day that they were announced, and
then I waited impatiently for 10 days
or so to receive them in the mail.
Fortunately, the 18M2 chips are now
available here in the US from Peter
Anderson ( www.phanderson.com/
picaxe/ index.html), and shipping only
takes about three or four days or so.
pins. (You can see my sophisticated
experimental setup in Figure 3.) The
software for my first experiment is
just as simple as the hardware:
; === TouchTest1.bas ===
; Tests a touch sensor.
; === Directives ===
#com 5
#picaxe 18M2
#no_data
#terminal 4800
; == Begin Main ===
do
touch B.1, b1
sertxd (#b1,cr,lf)
pause 250
loop
EXPERIMENT 1:
THE “I CAN’T WAIT!”
EXPERIMENT
When I finally got my hands on
the 18M2, the first thing I wanted to
experiment with was the new touch-sensitive inputs. I was in such a hurry
that I didn’t even want to take the
time to make a simple touch pad, so
I just cut a six inch piece of jumper
wire, stripped about 3/8” of the
insulation from
one end, and
inserted it into a
basic 18M2
breadboard circuit
so that the wire
connected with
pin B.1 — which is
one of the 18M2’s
10 touch input
As you can see, the touch
command can be very simple, but
you may also want to read the
documentation in Section 2 of the
PICAXE manual for some of the more
complicated details. The complete
syntax is: touch channel, variable.
Channel is the pin that is being used
as the touch input (pin B.1, in this
case), and variable is the variable that
receives the byte value of the touch
reading (variable b1, in this case). Of
course, I could also have defined
meaningful names for both of the
■ FIGURE 3.
Breadboard setup
for Experiment 1.
December 2010 17