Nothing drives me nuttier than messy program listings
— I hate them. An easy way to get a neat listing in the end
is to start with a clean template. In the downloads file
(available at www.nutsvolts.com), you’ll find template.pbas
which is what I use to start most projects (I have a few
other templates, based on different project types). Starting
with template.pbas, I’ve created hello.pbas which we’ll
work through to introduce the elements of a PropBASIC
program. Of course, not all sections of the template are
used for a simple LED blinker, but we’ll introduce them
anyway, and then explore them later.
Again, if you’ve worked with SX/B, much of this will
be very familiar. If you’re coming from the BASIC Stamp,
there will be some new things to get used to. Trust me,
you can, and when you do so many neat things will open
up for your [perhaps postponed] projects.
All right, let’s go through it, section by section.
DEVICE P8X32A, XTAL1, PLL16X
We’ll start nearly every PropBASIC program with this
“standard” setup; it declares the Propeller 1 (P8X32A),
running with an external crystal, using a PLL multiplier of
16x. The external crystal frequency is 5 MHz which gives
us a system frequency of 80 MHz ( 5 x 16).
There are, of course, options to the standard setup. For
low power applications that are not timing-sensitive, we can
select one of the RC modes. RCFAST runs the chip at about
12 MHz while RCSLOW runs the chip at about 20 kHz. To
run in RCFAST mode, we can reduce the setup to this:
■ FIGURE 2. RCSLOW not adjusted.
When no mode is specified, the compiler assumes
RCFAST; to use RCSLOW, we must specify it in the DEVICE
directive. In RC modes, the PLL is always 1x and the XIN
value is ignored. Again, RC modes should only be used in
programs that are not timing-sensitive, as the RC oscillator
varies from chip to chip. RC modes would not, for
example, be good to use when serial communications is a
That last statement probably caused you sharp folks to
raise a Spock-like eyebrow. If RC modes are no good for
serial communications, then how can the IDE reprogram
the Propeller when no crystal is attached? Good question.
The IDE actually times the Propeller as part of the
download protocol and then adjusts the baud rate from
the IDE to accommodate the individual chip. So, if you
really have to use serial communications and need a low
clock speed to reduce current consumption, you can do it
but you’ll need to do a couple extra steps:
• Measure the clock speed in RC mode (with no FREQ
specified). You can do this by measuring the width of an
output pulse using an oscilloscope.
Let’s say we create a program (see speed_test.pbas) to
output a 10 ms pulse and on a ‘scope we measure — as I
did — a pulse width of 9.06 milliseconds. This means that
the processor is actually running a little faster than
expected. We can calculate a scale factor for the assumed
clock frequency by dividing the measured pulse width into
the 10 milliseconds we expected.
scale = 10.0 / measured
■ FIGURE 3. RCSLOW after adjustment.
March 2010 17