This is only a preview of the December 2004 issue of Silicon Chip. You can view 20 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Schoolies 20W Amplifier Module":
Items relevant to "A Charger For Deep-Cycle 12V Batteries, Pt.2":
Items relevant to "Solar-Powered Wireless Weather Station":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
Sydne
can me y readers
et
at the P Stan Swan
IC
Decemb AXE fair,
er
see pag 10th –
e 43!
If you’ll excuse our Latin . . .
Picaxus
Interruptus
The new PICAXE-08M has been winning lots of
new friends around the world. Sorry to interrupt
but here’s yet another example of why that is!
U
nlike 2003’s popular PICAXE-08,
2004’s new 08M baby offers
valuable interrupts to program
flow. Such enhanced control has only
been previously been available on the
more costly Picaxes (typically the 18X),
so this new command is quite a bonus,
with significant potential for professional applications at a budget price.
Although understanding Picaxe
interrupts can be daunting, they turn
out to be quite straightforward to use.
However they’re not usually an issue
for PICAXE beginners and hence can
initially be neglected. If you’re still at
the HIGH 1: PAUSE 100: LOW 1 stage
then perhaps return to this article later,
since their benefits often arise when
trying to polish a programs action.
OK - but what ARE these so-called
“interrupts”?
Interrupts to computer programs are
similar to interruptions that plague our
daily lives. If mid-way thru’ a thriller
video your phone rings, then you’ll
probably pause everything and take
the call. (If it’s a Kiwi-made “Lord of
the Rings” video you’ll no doubt welcome the chance to subdue your envy!)
You’re effectively primed to break
out of the viewing routine when such
random phone calls arrive. Not all
activities accommodate such branching of course, with perhaps gunfire
the most dramatic – bullets hurtle to
the target irrespective of the hunters
Lashed up in the usual protoboard style, the PICAXE-08M ready to demonstrate
its interrupt capabilities. It’s powered by a 3.6V nicad battery (blue cylinder).
78 Silicon Chip
by Stan Swan
2nd thoughts after pulling the trigger.
Most PICAXE programs are however
essentially sequential in nature – first
do this, now this, then this – with any
breaks out of the sequenced flow first
awaiting completion of an ongoing
operation. Hence if a lengthy WAIT
is underway, perhaps WAIT 60 (1
minute), then no further actions will
occur until this period is timed out.
Grr- naturally this may be frustrating!
It’s comparable perhaps to customers
twiddling their thumbs while serving
staff finish their coffee break…
Wouldn’t it be more convenient if
your needs could be considered in a
more streamlined manner?
Tarah! – enter the interrupt command, or more correctly – a polled
(“checked”) interrupt.
Not all Picaxe commands can be
interrupted, since some require the
full attention of the micro controller
once they have started to execute. For
the Picaxe-08M these high priority non
interruptible commands are –
• INFRAIN2 - receiving IR data
• INFRAOUT - sending IR data
• NAP
- brisk sleep (<2.3secs)
• SLEEP
- long “hibernation” sleep
• SERIN
- awaiting serial data
• SOUND - production of simple
sounds
Annoyingly the power saving
SLEEP is on this list, meaning very
lengthy delays, with immense power
saving benefits, still have to run their
course. It’s maybe akin to hibernating
animals only being awoken by the arrival of spring.
WAIT (the shorter time delay) is
interruptible, so delays up to a minute
siliconchip.com.au
are thus viable. The PLAY and TUNE
commands can be interrupted partway
through, after each note has been
completed, even though a SOUND
command cannot. When a PLAY or
TUNE command is interrupted, any
notes that haven’t been played will
be discarded and execution after the
RETURN from the ‘Interrupt:’ routine
will continue at the statement following the PLAY or TUNE command
that was interrupted. Argh – shades of
musical chairs!
Probably the most valuable contribution involves a PAUSE or WAIT
command interruption that immediately terminates normal (short) delays.
Hence a WAIT 30, for a ½ minute
delay, will be chopped short as the
interrupt is sensed. It’s almost a WAIT
– “UNLESS” situation – wait for the
next bus unless a mate offers you an
earlier lift?
Configuring your program –
SETINT
The SETINT has two arguments, a
‘compare with value’ (input) and an
‘input mask’ (mask) in that order. The
syntax, although in cryptic binary (%),
is in fact akin to the efficient presetting
of normal Picaxe I/O channels with
‘LET PINS’ . Thus let pins = %00000000 switches all
outputs off while %00000010 turns
just pin 1 high
These binary digits read R-L, with
the left most being 7. With interrupts,
the ‘input mask’ defines which pins
are to be checked to see if an interrupt
should be generated.
%00000001 will check input pin 0
%00000010 will check input pin 1
%00000100 will check input pin 2
%00001000 will check input pin 3
These can be usefully combined
to check a number of input pins together ...
%00000011 will check input pins
1 and 0
%10000100 will check input pins
7 and 2 (useful for the larger Picaxe
–18X)
Having decided which pins you
want to use for the interrupt, the value
determines the second parameter of
the SETINT command. Once a SETINT
is active, the PICAXE monitors the
pins you have specified in ‘input
mask’ where a ‘1’ is present, ignoring
other pins. Every time the PICAXE
checks these input pins it creates an
8-bit value which reflects the value of
siliconchip.com.au
IO PINS
(CHANNELS)
(TO PC
CON2
DB9 SERIAL PORT)
+4.5V
(3xAA)
100kΩ
NTC
2
22k
3
10k
5
1
2
3
IC1
PICAXE-08M
4
7
0
6
1
5
2
8
8
3
4
4
1
RED LED
PIEZO
λ
10kΩ
GREEN LED
λ
LEDS
K
A
SC
2004
Picaxus interRuptus
CHANNEL 0: PROG OR OUT
CHANNEL 3: IN ONLY
The circuitry – programming, I/O, etc – remains the same for the 08M as the 08.
the pins it did read with others being
forced to zero.
Example: an input mask of
%00001100 will check pins 3 and
2 and create a value of %0000ab00
where bit ‘a’ will be 1 if pin 3 is high
and 0 if low, and bit ‘b’ will be 1 if pin
2 is high and 0 if low.
PROGRAM LISTING
The ‘compare with value’, the first
argument of the SETINT command, is
what this created value is compared
with, and if the two match, then the
interrupt will occur, if they don’t match
then the interrupt won’t occur.
If the ‘input mask’ is %00001100
(meaning pins 3 and 2), then the valid
Available for downloading from
www.picaxe.orcon.net.nz/int08m.bas
‘Demo “barebones” Picaxe-08M polled interrupt handling. Ver 0.9 28th Oct 2004
‘Overall effect is a Chernobyl alarm- a monitoring status beep & LED flash every
‘minute if cool, but alarm instantly sounds if NTC sensor thermistor gets too warm!
‘NB-works reliably as is, but possibly refine with READADC & PIN status check ?
‘
‘Triggered by raised temps(~60C) when NTC R falls to ~30k,since Ch.1 input then ‑
‘goes high. Returns to low (0) when it cools <60C. NTC & R voltage divider involves
‘+supply--(10k)--Pin 1--(100k NTC)--ground. Alter 10k &/or NTC value to suit your
‘own alert temp (Jaycar 100k <at> 25C used here).Red LED Ch.2 & Green LED Ch.4, with
‘piezo on Ch.0.Program hosted for download at =>www.picaxe.orcon.net.nz/int08m.bas
‘Protoboard layout =>www.picaxe.orcon.net.nz/pcxs08m.jpg (as used 08/M datalogger)
‘via Stan. SWAN => s.t.swan<at>massey.ac.nz for Dec. 04 “Silicon Chip” article.
‘------------------------------------------------------------------------------SETINT %00000010,%00000010 ‘Go to Interrupt routine when Pin 1 goes high
statusloop:
sound 0,(100,10)
high 4
pause 500
low 4
wait 60
goto statusloop
‘Normal cool monitoring routine
‘Audible beep for operator if room very bright
‘\
‘ Brief green LED flash in case noisy room
‘/
‘Temperature status interval (wait 60 ~1min)
‘Continue “all OK” safety status looping
interrupt:
sound 0,(100,10)
high 2
pause 100
low 2
SETINT %00000010,%00000010
RETURN
‘Beep/LED code when thermistor hot
‘Urgent beeps
‘\
‘ Rapid attention getting red LED flashing
‘/
‘re-enable interrupt
‘allow the interrupts
December 2004 79
‘compare with value’ can be one of the
following 4 binary options...
%00000000 - Pin 3 = 0 and pin 2 = 0
%00000100 - Pin 3 = 0 and pin 2 = 1
%00001000 - Pin 3 = 1 and pin 2 = 0
%00001100 - Pin 3 = 1 and pin 2 = 1
So if you want to generate an
interrupt whenever Pin 3 is high
and Pin 2 is low, the ‘input mask’ is
%00001100 and the ‘compare with
value’ is %00001000, giving a SETINT
command of –
SETINT %00001100,%00001000
Thus the interrupt will then occur if,
and only if, pin 3 is high (1) and pin 2
is low(0). Other combinations – maybe
pin 3 low with pin 2 high - will not
trigger the interrupt.
Processing the interrupt
Interrupts are not enabled until the
first SETINT is encountered. After that,
any input interrupt condition which
is met causes a subroutine jump to a
routine which must be labelled ‘Interrupt:’. The RETURN of that routine
cause execution to continue from the
point where the interrupt occurred.
With interrupts enabled the PICAXE
monitors the input pins specified by
a SETINT to determine if an interrupt
has occurred, and if so a subroutine
jump to the ‘Interrupt:’ routine is
made. The input pins are checked
(“polled”) just before each PICAXE
statement is executed, but not while
that statement is executing. This
means that the interrupt must exist
for long enough for the PICAXE to
sense, else it’ll be missed if it appears
only while the PICAXE is busy doing
something else.
When the ‘Interrupt:’ routine is
entered, the SETINT is disabled and
must be re-enabled for further interrupts to be responded to. Interrupts are
re-enabled by issuing another SETINT
command which is activated when
the RETURN of the interrupt routine
is executed
It is important that the condition
which initially caused the interrupt
has ‘disappeared’ or when interrupts
are re-enabled and the RETURN is
executed, another interrupt will be
immediately seen. . It’s rather like
your mates queuing up to give you
a lift – you only need a single ride at
that time but could do with another
ride tomorrow if the bus is again late.
If the interrupt was caused by a button push, then the interrupt routine
should check if that button is still held,
80 Silicon Chip
and only re-enable the interrupt and
continue when it has been released.
The following code demonstrates
how to handle an interrupt caused
by a button switch which sets Input
Pin 3 to 1 when pushed (perhaps via
a voltage divider) and to 0 (maybe
via a pull down resistor) when it is
released ...
SETINT %00001000,%00001000
‘Go to Interrupt routine on Pin 3 High
loop:
Main program code
GOTO loop
interrupt:
Code to do something when button
pushed
buttonhold:
IF pin3 = 1 THEN buttonhold
‘stay here if button still held on
SETINT %00001000,%00001000
‘re-enable interrupt
RETURN
‘allow the interrupts
There – that wasn’t too bad! Further
sophisticated extensions are possible
with clever use of diodes. With lateral
thinking a second Picaxe, devoted perhaps to a memory hungry task, could
even be switched on by an interrupt
driven 08M.
PICAXES can “source” (supply)
some 20mA a pin – easily enough to
supply the ~5mA needs of a second
bare 08. Transistors could amplify this
source current of course.
Chernobyl alert
Here’s short program that illustrates
08M interrupts at work in a realistic
application.
This uses almost exactly the same
hardware layout as our October 08M
NTC data logger, and pulses an “all
OK” green status LED every minute,
unless the temperature suddenly becomes too high (Chernobyl?). Urgent
LED flashes (red = hot) and distinctive
tones should be immediately given in
such circumstances. Naturally holding on (if still hot) and resetting (once
temperatures fall to safe levels again)
needs considering too.
If a classic PICAXE sequential
routine had been used, it would first
need to work through a WAIT (here 1
minute) before encountering the commands to detect the temperature rise
and sound alarms. Yikes – your reactor
could have melted down by then…
In our case, a quick blast from a hot
air gun readily triggers the alarm at
about 60°C.
Rather surprisingly, normal PICAXE
commands that monitor pin status
(readadc, pin etc) were found redundant in this example, as the NTC/10kW
voltage divider, under a 3 AA (4.5V)
supply, was alone enough to determine 0/1 logic states for the interrupt
settings at input pin 1. Your mileage
may vary !
References:
1: Picaxe Programming Editor help (F1 &
.pdf) provides interrupt help
2: Generous interrupt insights were provided by “Hippy” from the online Picaxe
support forum –
www.rev-ed.co.uk/picaxe/forum
3: www.picaxe.orcon.net.nz/pcxs08m.jpg
shows the 08M NTC/LED layout, with
just a single resistor changed (47kW to
10kW) for this example.
4: www.picaxe.orcon.net.nz/int08m.bas
hosts the Chernobyl Alert program.
A quick burst from a hot air
gun readily heats the sensor
thermistor enough to alert
the snoozing PICAXE. This
alarm temperature can be
easily altered (here ~60°C)
by changing the thermistor
and voltage dividing 10kW
resistor values.
Note – although neglected
on this simple 3.6V
breadboard, the 2 LEDs
strictly should use dropping
resistors, especially on
higher supply voltages.
siliconchip.com.au
|