This is only a preview of the August 2003 issue of Silicon Chip. You can view 31 of the 104 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. Items relevant to "PC Infrared Remote Receiver":
Items relevant to "Digital Instrument Display For Cars, Pt.1":
Items relevant to "Home-Brew Weatherproof 2.4GHz WiFi Antennas":
Items relevant to "Fitting A Wireless Microphone To The PortaPAL":
Items relevant to "Jazzy Heart Electronic Jewellery":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
MORE FUN WITH THE PICAXE – PART 7
Get that fat cat
code purring . . .
In an era when even modest
home PCs demand 128
MEGA bytes of RAM, the
microscopic 128 bytes of
Picaxe–08 memory seems
almost laughable. To put this
million times ratio into
perspective, it’s roughly akin
to the price difference
between a car and a peanut.
Of course, with resourceful
design, even 128 bytes may
not be peanuts!
A
lthough PICAXE microcontrollers don’t require solid
programming skills, it’s crystal
clear to many users (myself included)
that such devices, using “ software to
tame the hardware”, look to be the
future of many electronic circuits.
Most Picaxe designs (including mine)
evolve under “cut and try” incremental programming, and parts drawer
fossicking for, say 82nF capacitors and
680kΩ resistors in a traditional timing
circuit, can be replaced by convenient
and versatile code-tweaking instead.
Layouts can be smaller and cheaper
too, and upgradable later without laborious unsoldering.
So – you want to be part of this
“Flash” revolution then? You’d better
take note of some basics!
If you yearn for more than simple
“08” tone output or LED flashing,
coding care and economy is obviously
crucial. And it’ll help develop good
design habits that may carry over to
larger microcontroller applications.
www.siliconchip.com.au
Many real-world electronic and software engineering projects, just as in
other fields, are characterised by skills
subdivision, such that teams may work
on the display, while others may be
slaving away with power supplies or
user interfaces.
Ultimately these have to be seamlessly integrated into the final product,
or much fist shaking and gnashing of
teeth can occur. It’s similar to building
a house and coordinating tradesmen
– of course you don’t want concrete
slabs poured before services like
drains are first installed.
Perhaps the Golden Rule here is to
DOCUMENT YOUR CODING.
Such comments (indicated by ‘ before a remark) not only inform others
about your actions, but allow you to
be reminded about things when later
(re)viewing your work.
These comments do not add to any
program bulk, but are saved along with
the program to your PC.
If you’re a two-fingered typist re-
by Stan Swan
Clever code can purr along with the
limited 128 bytes of an “08”!
entering programs by hand (rather
than a copy-and-paste from a web
page listing ), comments don’t strictly
need to be included. Styles vary but
it’s common to add such ‘remarks on
the same line as the action.
Thus SLEEP 300 ‘enter low power
sleep mode for 300 secs = 5 mins
No doubt you’ve noted the initial
preamble comments on our previous
listings too.
These “abstracts” make for convenient spots for parts lists, authors,
web sites, dates and versions – the
latter point a crucial feature of course!
Others may judge your skills by your
comments, much as electronic projects
are often judged by neatness of the
hook-up wiring or PC board design.
Indenting a set of instructions, especially a loop, is also accepted practice
since it quickly allows visual grouping. Using ‘—— spacers may help too.
Points so far are pretty much common sense but GOSUB/ RETURN –
command pairs may need explanation.
August 2003 77
LEDSOS.BAS
(Also downloadable from: www.picaxe.orconhosting.net.nz/ledsos.bas)
‘Switchable LED or SOS flasher for Aug 2003 SiChip PICAXE-08 article V 1.0 26/6/03
‘Connect 3 ultra bright white LEDs directly to PICAXE pins 4,2 & 1 + common ground.
‘Switch to pin 3 may need pulldown resistor (10 k ?) since tends to float high
‘Just a single LED could be used, but 3 give greater light output even though each‘-is actually lit sequentially & human “persistence of vision” perception exploited.
‘Additional security results,since if 1 or 2 LEDs damaged or blown at least 1 works.
‘No dropper R’s as PICAXE 20mA source limit each pin is inside white LED 30mA specs.
‘Extra driver transistor could be used to give ~100mA pulses?(Ref PWM “SiChip” 4/03)
‘New command here =GOSUB,which allows common routine streamlining,thus memory saving.
‘Maybe alter pulse duration,but 700 =7millisec seems highest flicker free pulse rate?
‘Just 96 bytes used, so scope for other lighting effects- chasers/random/1-2-3 on etc
‘Maybe “lost in the bush” beacon flasher once every 5 secs = prolonged battery life!
‘3xAA batteries (4.5V) had ~16mA max drain when pulsed ~60 hrs life (& longer on SOS)
‘
Via Stan. SWAN (MU<at>W,New Zealand) => s.t.swan<at>massey.ac.nz <=
‘Lines beginning ‘ are informative program documentation & may be ignored if need be.
‘Program available for web download => www.picaxe.orconhosting.net.nz/ledsos.bas
‘Further “08” Morse ID refinements (35 chs !)=> www.picaxe.orconhosting.net.nz/morse.bas
‘———————————————————————————————————————
ledtrio:
‘routine to pulse all 3 LEDs to prolong battery life
if pin3=1 then ledsos
‘if pin 3 switch is low(0) just “steady” light ouput
gosub pulse
‘access common LED pulsed lighting routine
goto ledtrio
‘loop back if switch set for steady light out still
‘-——————————————————————————————————————ledsos:
‘emergency routine to send endless SOS .../—/...
for b1= 1 to 3
‘morse S = dit dit dit
for b0= 1 to 4
‘short hold on for each “dit” element
gosub pulse
‘access common LED pulsed lighting routine
next b0
‘loop to hold on duration variable
pause 200
‘200ms pause between each morse element
next b1
‘repeat so 3 flashes generated
pause 500
‘1/2 sec delay between each morse character
‘———————————————————————————————————————
for b1= 1 to 3
‘morse O = dah dah dah
for b0= 1 to 15
‘longer pulse hold on loop for each “dah”
gosub pulse
‘access common LED pulsed lighting routine
next b0
‘loop to hold on duration variable
pause 200
‘200ms pause between each morse element
next b1
‘repeat so 3 flashes generated
pause 300
‘1/2 sec (total) delay between each morse character
‘————-——————————————————————————————————-
for b1= 1 to 3
‘morse S = dit dit dit
for b0= 1 to 4
‘short hold on for each “dit” element
gosub pulse
‘access common LED pulsed lighting routine
next b0
‘loop to hold on duration variable
pause 200
‘200ms pause between each morse element
next b1
‘repeat so 3 flashes generated
pause 500
‘1/2 sec delay between each morse character
‘———————————————————————————————————————
pause 1500
‘2 second pause between SOS sending
goto ledtrio
‘recommence program from start
‘———————————————————————————————————————
pulse:
‘subroutine to rapidly sequentially pulse all 3 LEDs
pulsout 4,700
‘pulse pin 4 700 x 10 microsecs =7000us =7ms
pulsout 2,700
‘pulse pin 2 (Perhaps try varying mark/space effects-)
pulsout 1,700
‘pulse pin 1 (using high 4:pause 5:low 4:pause 50 etc)
return
‘go back to program point where subroutine began
78 Silicon Chip
These act as an elegant GOTO,
and are seen as the heart of efficient
structured programming, since they
allow common routines to be referred
to and actioned as need be, with a
return back (to the next program line)
on completion. It’s similar to maybe
“going on auto” when asked to put out
the garbage while watching TV.
LEDSOS.BAS
The subroutine in the sample LED
flashing program at left, giving out either a steady light or switched SOS, is
called up as needed to give a common
“pre-wound” pulsed LED instruction
set rather than wastefully say the same
thing three times elsewhere.
Pulsout commands here were selected to minimise circuit current drain,
while giving the brightest light with
the least flicker.
A Lux meter (eg, DSE Q-1400) may
prove invaluable for this, since the
human eye rather falls short when
judging illumination changes.
As an aside from microcontrollers,
ultra-bright white LEDs look to be
the best lighting development in 100
years (and I’m related to Swan of 1880s
carbon filament lamp fame too!) Their
light output (typically now a dazzling
5600mCd for even “cooking” versions),
almost unlimited life, ruggedness, high
efficiency and (now) cheapness make
traditional hot filament lamps near
obsolete for portable work.
Although white LEDs typically draw
30mA at 3.6V, the 20mA Picaxe source
limit allows them to be driven directly
from output pins (here 4,2,1) at slightly
reduced brightness.
A trio of driver transistors, as featured
in the earlier pulse width modulation
(PWM) motor controller, (SILICON CHIP
April 2003) could perhaps boost this
for brief 100mA pulses.
Note the new 10mm types used here
give out no more light than normal
Any simple 2-wire conductor will
pass Picaxe serial display data. Use
the D9 pin 2 for the signal, with
ground pin 5.
www.siliconchip.com.au
5mm types, but seem to “have more
presence” according to one observer.
Being larger, they’re much easier to
find too – the transparent 5mm types
are almost invisible when dropped on
a carpeted floor!
Morse in the 21st century?
Although now very much the domain of amateur radio CW diehards,
Morse code remains invaluable for
beacon/lighthouse ID and emergency
signalling – perhaps by flashing a
(LED) torch or even knocking SOS on
the wall. Hidden transmitter outdoor
“fox hunts” sending Morse remain
highly popular too.
More to the point for program economy insights, Morse dit/dah characters
lend themselves to elegant analysis.
Somewhat as a joint effort challenge
(and inspired by an old BASIC Stamp
program), Eric van de Weyer and I have
managed to squeeze up to 35 Morse
characters into an “08”.
Without such “crunching”, it’d be
taxing to even fit “Leo” [.-.. . ---] into
128 bytes !
Practising what I preach, the program listing overleaf (MORSE.BAS)
is copiously commented, with even
Morse characters themselves included
for those who forget (or those who
never knew them!).
Audio output is just from our piezo
attached at pin 0, while the 10kΩ pullup resistor fitted to pin 3 also remains.
Driver transistors could be used to key
a transmitter for more powerful work.
The SLEEP command used here,
although only about ±1% accurate,
causes a low power resting mode to be
entered, which provides useful battery
life extension. The syntax is obvious ex.
SLEEP 60 will awake after 60 seconds,
SLEEP 3600 after an hour
Yes, it’s the
same old
protoboard
layout . . . or
is it? That’s
right, it is
now rather
simplified.
(The 10kΩ &
22kΩ
resistors are
only required
during
programming.)
MORSE.BAS
Enough of the 19th century – we’re
in the Internet age. PICAXE serial data
output (mentioned in the last article)
also ends itself to message display.
Although small LCD panels abound
(mostly Hitachi-style 16 characters x
2 lines), these usually need driving
with a parallel data stream.
Logic ICs can be wired to provide
SIPO (Serial In Parallel Out) shift registers, but these naturally may daunt
users with simple display needs.
Several options have presented
themselves, all of which will just need
a simple 2-wire serial lead from the
www.siliconchip.com.au
Following the finding (detailed last month ) that the piezo speaker can be left
permanently attached to pin 0, the PICNIK box wiring has been adjusted to suit.
The now idle jumper is here used to switch input 3 high or low via a 10kΩ pullup resistor.
August 2003 79
Picaxe output pin in use and ground
return.
1. Purchase and assemble the Rev.
Ed AXE033 LCD 2-part kit. (available
in Australia and NZ from Microzed
or their resellers). This comes with
decoding electronics on board, plus
a socket for a Real Time Clock (RTC)
chip option that can be used to trigger
program events.
Additionally up to 7 prepared messages can be organised and saved on
this LCD board for easy recall– hence
sparing the driving Picaxe the associated memory storage overhead. Yah!
If your project can stand the cost
(A$44) this LCD certainly will greatly
enhance it. However you’ll need to
have good soldering skills to assemble
the rather fiddly kit (the instructions
are microscopic!). A higher voltage
6V supply will also be needed, since
the now-normal Picaxe 4.5V supply
will not bring up the LCD image. Grr !
2. As an LCD workaround, the
Programming Editor displays a mini
serial terminal when F8 is pushed,
using a signal lead (NOT the normal
programming one) from the chosen
Picaxe output pin and ground.
This display text is wider than the
16x2 LCD display, but may be valuable
for initial display work. 2002 versions
of the Picaxe Editor (Ver. 3.0.3) also
offer a more graphical display when
F9 is pushed
MORSE.BAS
(Also downloadable from: www.picaxe.orconhosting.net.nz/morse.bas)
‘PICAXE-08 memory workout demo via Eric van de Weyer & Stan.SWAN Ver 1.02 27th June 2003
‘For Silicon Chip August 2003 PICAXE article. Author - Stan.SWAN => s.t.swan<at>massey.ac.nz
‘Ref. Edwin.C => chick<at>chickene.freeserve.co.uk -June 2002 RSGB “RadComm” “28” version too
‘Program (derived from a Basic Stamp-1 idea ) sends short repeating Morse Code ID message
‘————————————————————————————————————————
‘Almost unbelievably up to ~35 Morse characters can be stored in the tiny PICAXE-08 RAM !
‘Output here just simple Piezo speaker at PICAXE Pin 0, but could be used to key a Tx etc
‘Only other component needed = 10k pull up R pin 3 to +ve rail to avoid “floating” 1/0
‘Note - although now near obsolete for messages,International Morse Code ( CW ) still has
‘wide use for beacons etc since decoding can be via eye or ear,& even unskilled observers
‘can thus “read” simple IDs & status at just a few (5?)words per minute.Of course sending
‘SOS via torch etc still suits emergencies! Scouting days now long past? Morse chs.are...
‘A .–
B –...
C –.–.
D –..
E .
F..–.
.
.
.
.
.
.
.
.
.
‘G –––
H
I
J –––
K– –
L.–..
.
.
.
.
‘M ––
N –
O – ––
P –
Q–– –
R.–.
.
.
.
.
.
.
.
.
.
‘S
T –
U
–
V
–
W –––
X–..–
.
.
.
.
.
.
.
.
.
‘Y– ––
Z ––
1 ––––
2 – ––
3
––
4....–
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
‘5
6 –
7 ––
8–––
9––––
0–––––
‘ Full stop . – . – . –
Comma – –. . –
Slash – . . . . –
‘
‘By tradition 1 dah/dash = 3 dits/dots with letter space = 3 dits & word spacing 7 dits
‘
‘How DOES this work !? Each ch.to be generated is programmed in as a number whose binary
‘equiv. then generates the code ! The 5 MSBs (Most Significant Bits =LHS) represent dots &
‘dashes, with dit=0 & dah=1. The last 3 LSB (Least Significant Bits = RHS) indicate how
‘many elements in a ch. Hence V=00010100 (100 =4 elements). Bit 5 is meaningless here.
‘Converting to decimal yields 20. Another ? K=10100011 = decimal 163 (011= 3 elements)
‘Here’s is a list of these characters (abrev. as ch. in comments) & their equiv. number
‘———————————————————————————————————-————‘A - 66
B - 132
C - 164
D - 131
E-1
F - 36
‘G - 195
H-4
I-2
J - 116
K - 163
L - 68
‘M - 194
N - 130
O - 227
P - 100
Q - 212
R - 67
‘S - 3
T - 129
U - 35
V - 20
W - 99
X - 148
‘Y - 180
Z - 196
1 - 125
2 - 61
3 - 29
4 - 13
‘5 - 5
6 - 133
7 - 197
8 - 229
9 - 245
0 - 253
‘= - 141
/ - 149
. - 86
, - 206
‘
‘Encode to suit - thus “AUSTRALIA 2003” = 66,35,3,129,67,66,68,2,66,0,61,253,253,29
‘————————————————————————————————————————‘Copy & paste main program below to “08” editor via=> www.picaxe.orcon.net.nz/morse.bas
‘Still scope for “telemetry” or tweaking SLEEP/NAP, as only 105 bytes (of 128) used as is!
Close-ups of the AXE033 LCD kit.
Note how the flexible solder mask
strip must first be peeled away before
inserting the header pins.
‘————————————————————————————————————————-
Two terminal program screen shots, BananaCom & HyperTerminal, during the
demonstration serial program run.
80 Silicon Chip
www.siliconchip.com.au
Symbol Tone = 100
Symbol Quiet = 0
Symbol Dit_length = 7
Symbol Dah_length = 21
Symbol Wrd_Length = 43
Symbol Character = b0
Symbol Index1 = b6
Symbol Index2 = b2
Symbol Elements = b4
‘sets the tone frequency ( range 20 -127 )
‘set quiet tone
‘set length of a dot (7 milliseconds)- yields 10wpm
‘set length of a dash
(21 mS = 3 dots long)
‘set space between words (43 mS = 2 dashes, 6 dots)
‘set register for ch.
‘loaded with number of chs. in message
‘counts the number of elements
‘set register for number of elements in ch.
Start:
sleep 5
if pin3 = 1 then Identify
goto start
‘NB - good program spot to turn on ID, via sensor etc maybe?
‘5 sec low power delay(varies if no pullup R)-modify to suit
‘wait for high input on pin 3 to start message- 1 by default
‘if no input,loop to start.
Identify:
‘routine to lookup ch.& put its value into the ch. register
for Index1 = 0 to 27
‘cycle through lookup for times = number of ch. in message
lookup Index1,(3,2,68,2,164,227,130,0,0,164,4,2,100,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1),Character
‘ This means (S I L I C O
N
C HI P
dit
dit
dit etc
gosub Morse
‘go to the ch. generation routine
next
‘loop back to get next ch. and load it
goto Start
‘return to start to wait for next input
Morse:
let Elements = Character & %00000111
if Elements = 0 then Word_sp
‘look at 3 LS digits and load into Elements register
‘% means binary
Bang_Key:
for Index2 = 1 to elements
‘loop through correct no. of times for number of elements
if Character >= 128 then Dah
‘test MS digit of ch. If it is 1 goto the Dah sub routine
goto Dit
‘if it is 0 goto the Dit sub routine
Reenter:
let Character = Character * 2
next
gosub Char_sp
return
‘do a left shift on all the bits in ch.
‘loop back to get the next element
‘go to sub routine to put in inter-ch. space
‘return to Identify routine to get next ch. to send
Dit:
sound 0,(Tone,Dit_Length) ‘sound tone for dit length
sound 0,(Quiet,Dit_Length)
‘silence for dit length
goto Reenter
‘return to look at next element of ch.
Dah:
sound 0,(Tone,Dah_Length)
sound 0,(Quiet,Dit_Length)
goto Reenter
‘sound tone for dah length
‘silence for dit length
‘return to look at next element of ch.
Char_sp:
sound 0,(Quiet,Dah_Length)
return
‘send silence for dah length after ch.completely sent
‘return to get next character
Word_sp:
sound 0,(Quiet,wrd_length)
return
‘send silence for break between words
‘return to get next ch.
www.siliconchip.com.au
August 2003 81
References and
parts suppliers . . .
(also refer to previous months articles)
1. Ultrabright (white) LEDs 5600mCd
range ~A$3 – various suppliers:
Jaycar www.jaycar.com.au
Dick Smith Elect. www.dse.com.au
Altronics www.altronics.com.au
Oatley www.oatleyelectronics.com
etc
2. Lux meter – Dick Smith DSE Q-1400
(~A$100) www.dse.com.au
3. Australian Picaxe agents, MicroZed,
handle the AXE033 LCD kit & RTC
($44) as well as Picaxe chips
www.picaxe.com.au
4. A superb example of a well documented Picaxe program can be seen at
www.hippy.freeserve.co.uk/axe18mon.txt
5. Banana Comm Terminal Program
(shareware ~160k) Download from www.
picaxe.orconhosting.net.nz/bcom30.zip
6. StampPlot Lite Ver 1.7 Shareware
(~1.6MB)- now handles 2400bps OK.
Download www.selmaware.com
7. Author’s revamped site with many links
& usual demo program down-loads
www.picaxe.orconhosting.net.nz
3. Given the abundance of older
discarded notebook PCs, it’s tempting
to raid the broom cupboard, dust one
off and push into service running a
Terminal program. Numerous compact
organisers, such as the HP-200LX and
Sharp Wizard OZ/ZQ 700 range, also
Screen shots during a program run of
the Programming Editor’s F8 and F9
serial data “terminals”.
82 Silicon Chip
TERMDEMO.BAS
‘For Aug.”SiChip” display article. Stan. SWAN => s.t.swan<at>massey.ac.nz Ver 1.0 19/6/03
‘Picaxe serial output demo for PC VDU display via almost any datacomms terminal program
‘Many exist,espec.classic Windows HyperTerminalPE (~700k) via => www.hilgraeve.com Free!
‘Install it then run - properties - make session -”Connect- Direct to Com1" & 2400,8,N,1
‘Consider cheap 90s laptops/organisers too -Compaq Aero/HP 200LX & Sharp Wizard 7xx etc
‘DOS (but Win friendly) Banana Comm (~170k) espec.clean,& MODE CO40 allows enlarged text
‘Also works on Rev.Ed AXE033 16x2 alphanum. LCD kit,but text wraps since smaller display
‘Easy 2 wire serial only,here via “08” I/O pin 2 then D9F pin 2 for PC COM1 + pin 5 gnd
‘Suggest using 2 PCs for this - one as normal programmer, & other just for serout display
‘Program(s) can be downloaded => www.picaxe.orconhosting.net.nz/termdemo.bas,& also /bcom30.zip
‘2 notebook setup pix (Toshiba editor/Aero display)=> www.picaxe.orconhosting.net.nz/termdemo.jpg
‘NB-when Editing + 2 wire serial lead to correct I/O pin- F8 gives “mini terminal”
‘Even “datalogging” possible (via “F9” under Editor V 3.0.3 ) or via StampPlot Lite 1.7 !
‘——————————————————————————————————————
demo:
‘ ASCII control codes <32 are IBM style
serout 2,n2400,(12,10)
‘ Form Feed (FF)=clear screen- then a LF
pause 50
‘ brief pause before message shows
serout 2,n2400,(“Hello from your PICAXE-08”)
‘ displays message in quotes
serout 2,n2400,(32)
‘ acts on ASCII directly - a space
pause 500
‘ 1/2 sec pause for visual effect
for b0= 65 to 90
‘ / acts on direct ASCII request
serout 2,n2400,(b0)
‘ so translates & displays
next b0
‘ \ A - Z characters in sequence
pause 2000
‘ 2 second delay to hold message
b2=100 ‘ assign demo variable value
b1=b2/2
‘ simple divide 100 by 2 maths calc.
serout 2,n2400,(32,#b2,”/2 = “,#b1)
‘ space,then calc.(# forces values)
pause 1000
‘ 1 sec. pause
goto demo
‘ repeats entire message
came with inbuilt terminal programs
but these may not stoop to the Picaxe
2400bps limit.
Programs found to give seamless displays were the classic but bland Windows “HyperTerminal”, a tiny DOS (but
Windows friendly) “BananaComm”,
and the astounding StampPlot Lite.
This latter program not only shows
normal messages, but handles comma
separated value data (.csv – as used
with Excel), with graphical display.
Although initially written for the
BASIC Stamp, StampPlot works with
any serial data stream- the US author
even kindly tweaked it for 2400bps
Picaxe use!
Compared with the AXE033 LCD,
a full PC terminal program like Banana-Com allows both a wider screen
and larger text ( via MODE CO40 ),
ASCII control codes ( such as CR/LF )
plus saving and printing etc. With a
small computer like the 1993 Compaq
Aero used here, this approach may be
a versatile “zero cost” display solution.
The TERMDEMO.BAS demonstration serial data Picaxe program needs
no external hardware beside the serial
output cable. It simply displays a repeating message to whatever terminal
program (or LCD) you’ve connected via
the 2 wire lead.
Note how # forces actual result
variables (rather than just messages)
to also be displayed too.
You’ll no doubt quickly tire of irksome cable swapping when exploring
display syntax effects, so it’s (again)
suggested that two (notebook?) PCs be
used – one for editing, and the other as
a display terminal. Good viewing! SC
www.siliconchip.com.au
|