setint %00001000, %00001100, so
the same syntax works for more
complicated interrupts as well.
One final point: Whenever a
program jumps to its interrupt
routine, the compiler immediately
disables the interrupt. If it didn’t, the
interrupt would most likely interrupt
itself multiple times! For example, if
the Pi uses a 1 mS high pulse to
trigger the interrupt, pin C. 3 could
still be high when the PICAXE
program enters the interrupt routine
which, of course, would again trigger
an interrupt; immediately disabling
the interrupt avoids that problem.
However, it also means that we
need to include another setint
command (identical to the one at the
beginning of the program) just before
the program returns from the interrupt
routine, so that the interrupt is re-enabled.
Now that we’ve covered the
basics of interrupts, we’re ready to
take a look at the two programs we’ll
be using in this experiment. First,
open the PICAXE program
( sendReport.bas). As you can see, it’s
almost identical to the program we
used in Experiment 2, so it requires
little explanation.
The only significant difference is
that the serial transmission has been
moved from the main do/loop to the
interrupt routine. Also, note how we
re-enable the interrupt before
returning from the interrupt routine.
The companion program for the
Pi ( requestReport.py) is a little more
involved, so there are a few details
that may require clarification. Open
the program in the idle3 editor, and
take a look at the main portion of the
code. In the first program line, the
serial port is opened, but this time
we’re including the optional timeout
argument and setting its value to one
second. As a result, whenever a
ser.read() statement is executed, if a
data byte is not received within one
second, the program will move on.
At the end of the try block in the
main while loop, the for loop also
requires a brief explanation. When I
first wrote the program, I simply used
a sleep( 10) statement at this point
because I wanted the program to
check for new data every 10
seconds. However, it appears that
Python can’t respond to the ctrl-c
keyboard interrupt when the program
is sleeping. If I pressed ctrl-c, it took
as long as 10 seconds for the
program to terminate; the for loop
solved that problem.
Now, let’s turn our attention to
the getData() function which does
most of the work in the program. The
following comments elaborate on the
correspondingly numbered program
comments in the getData() function:
14 April 2014
SPECIAL OFFER: 20% OFF
YOUR FIRST CLASS!
To reserve your discount sign up at
www.ros-training.com/nuts_volts
LEARN • BUILD • SHARE
mwww.ros-training.co
CONTROL
LOCATION PERCEPTION
INTELLIGENCE
ActivityBot
ROBOT KIT $199
Learn to program in C on Mac or PC. Online tutorials are suited for the self-guided learner yet easily
expand to accomodate a classroom or club setting.
Greatfor STEM learning!
Preview the lessons at:
learn.parallax.com/ActivityBot
[]