FROM PBASIC TO SPIN
To start with, I’m going to translate the BS2 stepper
motor demo code that is available on the Parallax website
(and in the ZIP for this article on the Nuts & Volts website;
www.nutsvolts.com). We’ll go section-by-section making
the translation from PBASIC to Spin.
Figure 1 shows the driver that we’ll use to run the stepper motor — an L293D. I like this chip for driving steppers
because it can drive unipolar (five or six wires) or bipolar
(four wires) steppers with the same code. The only difference
in the connections is that the unipolar motor has a common
connection (or two) that goes to GND. Note that the original BS2 demo does not deal with the L293D enable pin, so
we’re going to add that to the program — it’s a useful feature
as it allows the stepper to “coast” when enable is not active.
A couple things about the L293D: You’ll note that the
logic supply (+ 5) is called VSS by the manufacturer — do not
connect this pin to ground. The GND pins ( 4, 5, 12, and 13
on the DIP version) are for ground. The motor supply pin is
called VS; this is pin 12 on the DIP. Before you make the
connections for the L293D version you select, be sure to
review the datasheet so that you make the correct connections. Also note that I’ve pulled the L293D Enable pins low
through a 4.7K resistor; this disables the outputs when the
control pin is disconnected or floating. If the Enable pins are
allowed to float, the L293D outputs will be active.
Author’s Note: The TI SN754410 can be used in place
of the L293D.
Okay, let’s start translating code. At the top of the BS2
program, we’ll find the following definitions for the I/O
connections and the number of steps the motor requires for
a single revolution.
And here’s how we’re going to end up translating that
code to Spin:
= XTAL1 + PLL16X
= EN + 1
= M1 + 3
STEPS_PER_REV = 48
Okay, so there’s a little more work involved with the
Spin definitions, but with that we get a whole lot
more power and flexibility. We always start by defining
_CLKMODE and _XINFREQ; the listing shows our
“standard” selection of a 5 MHz clock input and the 16x PLL
tap; this causes the chip to run maximum speed ( 80 MHz).
Next come the I/O pins. As noted earlier, we’re adding
an Enable, along with the [contiguous] motor connections.
With the Propeller, we have enormous flexibility with I/O.
On the BASIC Stamp, we select nibble boundaries for the
four stepper motor outputs. With the Propeller, those
boundaries don’t exist and we can use any four contiguous
pins to control the motor (we’ll see that in just a second).
Next up are the global variable definitions:
Remember that the BASIC Stamp uses an eight-bit core
(PIC or SX), so its native variable type is the Byte. Bits, Nibs,
and Words are allowed, but they actually take work on the
inside of the Stamp to support them. The Propeller uses a
32-bit core, so its native variable type is the Long. Since the
Propeller has 16x the RAM space of the BASIC Stamp, we
don’t have to be so conservative with variable definitions, in
fact, by using Longs, the program is more efficient as that is
the native type.
long idx, stpIdx, stpDelay
Yes, we could have defined these variables as Bytes or
Words (there are no Bit or Nib variables in Spin) but, again,
it would not have improved the performance of the program.
The next section in our PBASIC version of the program
is a DATA table for the stepper motor coil patterns.
DATA %0011, %0110, %1100, %1001
And in Spin:
byte %0011, %0110, %1100, %1001
By convention, PBASIC DATA tables usually appear
near the beginning of the program and Spin DAT section(s)
appear at the end. This is just convention and the compilers
do not care either way. Do note that we define the element
size of items in a DAT table.
And now we get into the working code:
DIRB = %1111
stpDelay = 15
FOR idx = 1 TO StpsPerRev
FOR idx = 1 TO StpsPerRev
August 2006 17