the Pi can do the entire computation
in a single step.
4. Python’s round() function
takes two arguments: the value that
we want to round; and the number
of decimal places we want to use.
5. In this statement, we’re
converting the temperature reading
from C to F by doing the same two
computations we just carried out to
convert ADCval to Centigrade.
However, this time, we’re doing them
both in a single statement.
Python computes the value of
tempF using the standard C to F
conversion formula, and then rounds
the result to one decimal place — all
in one line of code.
6. These three print() functions
use some of the features we
demonstrated earlier with the
printPlay.py program. The second and
third print() functions demonstrate
the two methods we discussed for
formatting the data.
You may want to refer back to
that discussion to see if you can
predict how the printed output will
be formatted.
When you’re ready to carry out
Experiment 2, run the Python
program first. The blocking read()
function will cause it to wait until the
PICAXE program begins to send the
serial data.
Experiment 3: An
Interrupt-Triggered
Serial Transmission
In this experiment, we’re putting
the Pi in charge of when a serial
transmission occurs. The Pi will issue
an interrupt signal and the 08M2 will
respond by serially sending the raw
temperature data (ADCval) to the Pi.
We’re again transmitting two data
bytes (hiByte and loByte) to the Pi,
but the same approach can easily be
used for any amount of data.
In other words, it would be a
simple matter to serially connect a
larger processor (such as the PICAXE-
20M2) to the Pi and have the 20M2
monitor several sensors, then send all
the updated data whenever the Pi
issues an interrupt. Also, we’re going
to unblock the Pi’s serial reception so
that we don’t run the risk of the Pi
hanging because there has been a
glitch in a serial transmission.
All current PICAXE processors
support some form of interrupt
capability, but we’re going to limit
our discussion to the M2-class
processors and the 20X2. If you’re
interested in the more advanced
features of the 20X2 and 40X2
processors, you may want to read
the relevant documentation on the
setint and setintflags commands in
Section 2 of the PICAXE manual.
Also, we discussed interrupts way
back in the February 2009 Primer, so
I won’t repeat all the details here. If
you want more information than
we’re about to discuss, you may want
to reread the 2009 article, and the
setint documentation in the manual.
Let’s begin with a brief
explanation of how an interrupt
functions. First, we need to include a
setint command at the beginning of
our program to specify which pin(s)
we want to use for the interrupt, and
whether we want the interrupt to be
triggered by a high or low pulse.
(We’ll see exactly how to do that
shortly.)
Secondly, we also need to
include an interrupt subroutine which
must begin with the label interrupt:
and end with a return statement. The
interrupt code specifies the action we
want to take place in response to the
interrupt signal. (In this case, the
08M2 will send the values of hiByte
and lowByte to the Pi.)
When we run the PICAXE
program, after each program line is
executed (and continuously during
any pause or wait command), the
PICAXE compiler checks to see
whether an interrupt condition exists.
If it does, the interrupt subroutine is
immediately executed, and then
12 April 2014
; FIGURE 4. Port C pins for PICAXE interrupts.