Inside the repeat loop, we add the value of ms1Tix to
sysT0 to create a new target for the Pause until block.
Jon “JonnyMac” McPhalen
jon@jonmcphalen.com
As you may know, the Propeller has a system counter
that runs at the system clock speed; in BlocklyProp, this
defaults to 80 MHz. This is great for short delays, but not
for practical delays where a milliseconds timer would be
more convenient.
you to read the preceding paragraphs than it would to
build a simple program like this from start to finish. That’s
the point: BlocklyProp allows us to build a program
through an intuitive process and does us the big favor of
handling pesky syntax details like curly braces and
semicolons (something I always forget about when
programming in C).
In listening to Ken talk about BlocklyProp, this is about
the extent of instruction that most students need. After
that, they are allowed to roam free in the system to
accomplish personal or assigned tasks. You should do that
too (after finishing my column, of course).
If you’re a regular reader, you know that I’m not a big
fan of the Arduino, but I do use it from time to time to
help my friends. One of its saving graces (for me) is the
millis() function that gives us the number of milliseconds
since reset. Using millis(), we are able to create programs
that appear to multi-task.
Here’s how we can use it (this is pseudo-code):
// ————Main Program ———
int main()
{
while (1)
{
print((“Nuts & Volts”));
print(“\r”);
high(26);
pause(500);
low(26);
pause(500);
}
}
xtimer = millis
ytimer = millis
Explore the menus. If you’re new, some items may not
make sense right away, so ignore them until later. If you’re
an experienced programmer, this is the time to get a feel
for the various menus and the elements they contain.
repeat forever
if (millis ––xtimer) >= XTIME
xtimer += XTIME
do_x_process
For those that want to take a look under the hood,
click on the Code button in the upper right-hand part of
the screen. Here’s what we get:
if (millis ––ytimer) >= YTIME
ytimer += YTIME
do_y_process
/* SERIAL_TERMINAL USED */
// ————Libraries and Definitions ———
#include “simpletools.h”
This simple framework adds a lot of power to
applications, allowing us to do more work in a core (the
Arduino only has one) while not wasting time with
pause().
In the beginning, we don’t need to look at the
generated C code. Down the line, however, BlocklyProp
will likely become a bridge to programming with
traditional tools. For my part, I copied the code produced
by BlocklyProp into SimpleIDE, made modifications, and
ran it.
The first step is to synthesize a milliseconds timer in
the Propeller. We will create a function that manages the
timer; this function will contain an infinite loop that will be
launched into its own cog. From the Functions menu,
drag a function block onto the work space and name it
“background.” From the Variables menu, drag a variable
block onto the canvas and change its name to “ms1Tix.”
This value will be the number of system clock ticks in one
millisecond, which is to say the clock frequency divided
by 1000. Can we have a variable assignment that is an
expression in BlocklyProp? We can.
The blinker project that we just created could, in fact,
run on any microcontroller that has a Blockly
environment. The ability to use multiple cores is what sets
the Propeller apart from the rest, and even in a beginner-friendly system like Blockly, we can create sophisticated
multi-core programs.
Open the Operators\Numbers menu, drag a math
operator (topmost) block onto the canvas, and connect it
to the ms1Tix block. Now, set the operator to division. In
the System menu, locate the system block and drag this
into the first parameter space of the math operator block.
The system block defaults to “counter.” Change this to
“clock frequency.” Complete the expression by dragging a
number value block from the Values menu into the
second parameter space of the math operator block, and
then change the value to 1000.
The output of the next LED blinker program will
match the first, but will use a framework that allows us to
do other work while waiting for the time to change the
state of the LED. The lesson here is that pause() is the
devil and should be avoided when possible.
By now, you have the hang of using BlocklyProp, so
I’m going to point you to Figure 4 as a guide to
completing the background function.
Okay, then. How do we create a delay without using
pause()? A simple way is to track the time between two
points.
Here’s what’s going on in the background function: A
variable called ms1Tix is initialized to the number of
system clock ticks in one millisecond. Next, a variable
called sysMillis (our timer) is initialized to zero. The final
variable, sys T0, is initialized to the current value of the
system timer. This variable allows us to run the repeat
loop at exactly 1,000 Hz.
14 July 2017