This is only a preview of the September 2004 issue of Silicon Chip. You can view 16 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. Items relevant to "Bed Wetting Alert Sounder For Toddlers":
Items relevant to "You’ve Had Your Fun – Now Make A Doorbell":
Items relevant to "PICAXE The Red-Nosed Reindeer":
Items relevant to "Build A Programmable Robot":
Purchase a printed copy of this issue for $10.00. |
Build it now
in time for Christmas . . .
PICAXE the
red-nosed
reindeer
By Clive Seager
This simple novelty project enables you to
explore two of the new features of the latest
PICAXE micro and have some fun into the
bargain. Complete with antlers, flashing
eyes and nose, it can play several popular
Christmas tunes on command. It can even
play mobile phone ring tones!
M
ICROCHIP HAS recently released
a new PIC micro, the PIC12F683,
with twice as much memory as the
original PIC12F629 used within the
PICAXE-08. This has allowed Revolu-
tion Education to develop the PICAXE08M, an enhanced PICAXE-08 with
in-built music playing capabilities
– and much more.
The PICAXE-08M supports all of
Fig.1: the pinout
diagram for the
new PICAXE-08M
microcontroller.
40 Silicon Chip
the standard commands and features
of the PICAXE-08, with the following
enhancements:
• Program memory has been doubled
in capacity (approx. 80 lines of code).
• Musical tune playing capability,
user generated or four pre-programmed
tunes (play and tune commands).
• 10-bit ADC option on 3 pins (readadc10/readadc commands).
• Interrupt feature on inputs (setint
command).
• Infrared remote control input
and output (infrain2/infraout commands).
• Continuously driven PWM motor
drive output (pwmout command).
• Control of radio control type servos
(servo command).
• Count high frequency pulses within
a set time period (count command).
• Accurate digital temperature sensor interface (readtemp/readtemp12
commands).
• Read serial number from any Dallas
1-wire device (eg, iButton) (readowsn
command).
siliconchip.com.au
Fig.2: with the aid of a PICAXE-08M micro and simple software, the
circuit can flash Rudolph’s nose and eyes and play a seasonal tune. The
infrared receiver (described next month) allows remote selection of
one of four possible tunes.
•
Software support for increased
(8MHz) clock frequency (setfreq command).
Note that all existing PICAXE-08
programs will run on the 08M without
modification.
Playing tunes on a PICAXE
The main obstacle when playing
tunes on PIC microcontrollers is limited memory space. All the PICAXE
chips have a sound command to make
noises. However, the number of notes
and playback speed, or “tempo”, is
very limited, as the sound command
data rapidly consumes all available
program memory.
To overcome these issues, the
PICAXE-08M has a newly developed
tune command to play music. The tune
command incorporates a note data
compression algorithm to save memory space. It is also pre-programmed
with four melodies: Happy Birthday,
Jingle Bells, Silent Night and Rudolph
the Red Nosed Reindeer. In addition,
it can be programmed with the mobile
phone ring tone of your choice.
Mobile phone ring tones are widely
available on the Internet in “ring tone
text transfer” format (RTTTF), as used
on most Nokia phones. Although some
web sites charge for ring tone downloads, there are still a number of sites
that provide these free of charge. A
simple microcontroller cannot play
complicated “polyphonic” tunes, but
siliconchip.com.au
can make a good attempt at
playing “monophonic” (one
note at a time) tunes.
Circuit description
Fig.2 shows the circuit of our
simple Christmas decoration,
with the PCB shaped in the
form of Rudolph’s head. Output 0 is connected to a 10mm
red LED, forming Rudolph’s
nose. Output 4 drives two smaller green LEDs for the eyes, whereas
a piezo transducer on output 2 plays
the tune. Inputs 1 and 3 are connected
to a light-dependant resistor (LDR) and
push-button switch.
In this circuit, the PICAXE-08M micro can be programmed to play a tune
when the switch is pressed and/or the
light level (sensed by the LDR) changes.
Note that the push-button switch circuit is “active low” for compatibility
with the infrared upgrade, which will
be described next month.
The supplied piezo transducer does
a reasonable job of tone reproduction.
For a richer sound, it can be replaced
with a low-cost 40Ω speaker if desired.
Wire one of the speaker leads in series
with a 10µF electrolytic capacitor before hook-up to the PC board.
Kit assembly
A kit of parts that includes the preshaped PC board will be available
from your usual PICAXE reseller.
Fig.3: follow this diagram closely when
assembling Rudolph. Note in particular
the orientation of the three LEDs and
the PICAXE micro as well as the power
supply positive and negative leads.
The PC board overlay is shown in
Fig.3. Assembly of the board is very
straightforward, with attention to the
points listed below.
Although the infrared sensor (IR)
September 2004 41
Fig.4: this piano keyboard representation shows the key frequencies in octaves 5, 6 and 7. This is the range of
frequencies covered by the PICAXE-08M.
and capacitor (C1) appear on the
circuit and overlay diagrams, they
are required only for remote control
operation and can be left out for the
moment.
Take care with the orientation of the
three LEDs, which must have their flat
(cathode) sides positioned as shown.
Also, make sure that the notched (pin
1) end of the PICAXE micro faces the
serial link socket. Before soldering
Bit No. 7, 6 – Duration
00 = 1/4
01 = 1/8
10 = 1
11 = 1/2
the battery clip leads, thread them
through the adjacent hole to provide
strain relief.
Power your completed project only
from a 3 x alkaline AA cell (4.5V) battery
pack or regulated 5V DC supply. Take
particular care that you have the power
leads around the right way, otherwise
you’ll destroy the PICAXE!
The following paragraphs explain
how the music encoding process
Bit No. 5, 4 – Octave
Bit No. 3, 2, 1, 0 – Note
00 = Middle Octave (6)
01 = High Octave (7)
10 = Low Octave (5)
11 = not used
0000 = C
0001 = C#
0010 = D
0011 = D#
0100 = E
0101 = F
0110 = F#
0111 = G
1000 = G#
1001 = A
1010 = A#
1011 = B
11xx = P (pause)
Notes: 1/16, 1/32 and ‘dotted’ notes are not supported.
Only octaves 5-7 are supported.
Fig.5: the encoding of
each note byte used
by the tune command.
42 Silicon Chip
works. If you’re not interested in the
technicalities and just want to play
tunes, you can skip directly to the
“Tune Wizard” section below!
Tune command
The tune command has the following syntax: tune led, speed, (note, note,
note . . .) where:
(1). led is a variable/constant (0-3)
which specifies if other outputs flash
at the same time as the tune is being
played; ie,
0 = No outputs
1 = Output 0 flashes on and off
2 = Output 4 flashes on and off
3 = Output 0 and 4 flash alternately
(2). speed is a variable/constant
(1-15) which specifies the tempo of
the tune.
(3). note, note, etc is the encoded musical note data.
Note compression
Most ring tones use notes from octaves 5, 6 and 7 (octave 6 starts with
“middle C” for the piano players!).
A graphical representation of these
notes compared to a piano keyboard
is shown in Fig.4.
There are 12 notes to an octave (including incidental notes), and if you
add pause, this gives 13 different possiliconchip.com.au
exactly the same length as the number
of notes in the tune.
Tempo
Fig.6: import RTTTL tunes or create
your own with the Tune Wizard,
included in versions 4.0.1 and later of
the PICAXE Programming Editor
Speed
Beats/Minute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
812
406
270
203
162
135
116
101
90
81
73
67
62
58
54
Fig.7: this table shows the equivalent
tempo in beats/ minute for the 15
possible speed values.
sibilities. As the tune plays, each note
also varies in duration, and most ring
tones use 4 different lengths - notes of
duration 1/8 (quaver) 1/4 (crotchet) 1/2
(minim) or 1/1 (semibreve). Theoretically, notes of 1/16 and 1/32 are also
possible, but only a few ring tones use
these durations and so they are not
considered in our algorithm.
Considering this information, each
note byte can be encoded into just 1
byte of memory as shown in Fig.5. The
encoding is optimised to ensure that
the most common values (1/4 beat and
octave 6) both have a value of 00. Note
that as the PICAXE also performs further optimisation on the whole tune,
the length of the tune data will not be
siliconchip.com.au
The speed of music is normally
called “tempo”, defined as the number
of quarter beats per minute (BPM).
The PICAXE tune command allows
15 different speeds (1-15) calculated
as follows:
The sound duration of a quarter
beat within the PICAXE is given by
the following formula:
sound duration = speed x 65.64ms
Each quarter beat is also followed
by a silence duration, given by:
silence duration = speed x 8.20ms
Therefore, the total duration of a
quarter beat is:
total duration = (speed x 65.64) +
(speed x 8.20) ms = speed x 73.84ms
The approximate number of beats
per minute at different speed values is
shown in Fig.7. The chosen values give
a good range for most popular tunes.
Note that within electronically generated music a note normally plays for
7/8 of the total note time, with silence
for 1/8. With the PICAXE-08M the
ratio is slightly different (8/9) due to
the memory and mathematical limitations of the microcontroller. However,
unless you are a gifted musician you
probably won’t notice the difference!
Tune Wizard
The Tune Wizard within the
PICAXE Programming Editor software
(v4.1.0 or later) allows musical tunes to
be created for the PICAXE-08M. Tunes
can be entered manually using the
drop-down boxes if desired but most
users will prefer to import a mobile
phone monophonic ring tone.
Note that the PICAXE can only play
one note at a time (monophonic), and
so cannot use multiple note (polyphonic) ring tones.
A typical RTTTL ring tone, as downloaded from the Internet, is shown
in Fig.8. The first few text characters
describe the ring tone name, then the
default characteristics (default note
duration = 1/4, default octave = 5, bpm
Parts List
1 Rudolph PC board
1 miniature LDR
1 miniature piezo transducer
1 miniature pushbutton switch
1 3.5mm stereo socket
1 3 x AA battery holder
1 battery clip
1 8-pin IC socket
Semiconductors
1 PICAXE-08M
2 5mm green LEDs (LED1 &
LED2)
1 10mm red LED (LED3)
Resistors (0.25W 5%)
1 22kΩ (red red orange gold)
2 10kΩ (brown black orange
gold)
1 4.7kΩ (yellow violet red gold)
4 330Ω (orange orange brown
gold)
Note: the infrared receiver (IR) and
capacitor (C1) are not required
at this stage. Their use will be
covered in the infrared upgrade
next month.
Also required (not in the kit)
PICAXE Programming Editor
software (v4.1.0 or later)
PICAXE download cable (Part
No. AXE026)
3 x AA alkaline cells
Obtaining kits & software
The design copyright for this
project is owned by Revolution
Education Ltd.
Complete kits (Part No. AXE107S) for this project are available
from authorised PICAXE distributors – see www.microzed.com.
au or phone MicroZed on (02)
6772 2777.
The PICAXE Programming Editor software can be downloaded
free of charge from www.picaxe.
co.uk or ordered on CD (Part No.
BAS805).
Fig.8: Star Wars Theme Tune In RTTTL Format
Star Wars Theme:
d=4,o=5,b=125:1a#,1f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6,
8c6,1a#6,2f6,8d#6,8d6,8d#6,2c6,8f,8f,8f,2a#,2f6,8d#6,8d6,
8c6,1a#6,2f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6,8d#6,1c6
September 2004 43
Fig.9: Rudolph Program Listing
' ***** main loop *****
main:
if pin3 = 0 then playit
goto main
' ***** play tune *****
playit:
readadc 1,b1
REM debug b1
'read light level
'optional display on screen for testing
' play tune depending on light level
if b1 > 200 then play_xmas
if b1 > 150 then play_rudolph
if b1 > 80 then play_silent
play_jingle:
play 1,3
goto main
'internal tune Jingle Bells
play_silent:
play 2,3
goto main
'internal tune Silent Night
play_rudolph:
play 3,3
goto main
'internal tune Rudolph The Red Nosed Reindeer
play_xmas:
'external ring tone tune
PICAXE, but will differ slightly due to
the different ways that the computer
and PICAXE generate and playback
sounds.
The “outputs” section of the Wizard
interface allows you to choose which
outputs flash on and off as the tune is
played. For the Rudolph project, select
both outputs (nose and eyes!)
Rudolph program
The program listing shown in Fig.9
demonstrates how to play a tune when
the switch is pushed.
The tune played will vary between
four different melodies, depending on
the light level falling on the LDR when
the switch is pushed. Three of these
melodies (Jingle Bells, Silent Night &
Rudolph) are pre-programmed tunes
included within the PICAXE-08M
bootstrap program and are activated
by the play command.
The final melody (“We Wish You a
Merry Christmas”) is a user-defined
tune, generated by the Tune Wizard,
and played with the tune command.
Summary
' We Wish You a Merry Christmas
tune 3, 4, ($22,$27,$67,$69,$67,$66,$24,$24,$24,$29,$69,
$6B,$69,$67,$26,$22,$22,$2B,$6B,$40,$6B,$69,$27,$24,$22,
$24,$29,$26,$E7,$22,$27,$67,$69,$67,$66,$24,$24,$24,$29,
$69,$6B,$69,$67,$26,$22,$22,$2B,$6B,$40,$6B,$69,$27,$24,
$22,$24,$29,$26,$A7,$22,$27,$27,$27,$E6,$26,$27,$26,$24,
$E2,$29,$2B,$69,$69,$67,$67,$02,$22,$22,$24,$29,$26,$E7)
goto main
The play and tune commands allow you to play simple musical tunes
on the new PICAXE-08M. There are
approximately 1000 tunes for free
download on the software page of the
PICAXE website at:
www.picaxe.co.uk
Some other possible sources for free
ring tones are:
www.ringtonerfest.com
www.free-ringtones.eu.com
www.tones4free.com
Next month
= 125), then each note in turn. This
ring tone can be imported straight into
the Tune Wizard. The Tune Wizard
will then automatically generate the
BASIC code required for the PICAXE
program.
44 Silicon Chip
The tune can also be tested on
the computer by clicking the “Play”
menu (assuming the PC is fitted with
a soundcard and speakers). The tune
played will give you a rough idea
of how the tune will sound on the
Next month, we’ll show you how
to control Rudolph via an infrared
remote control. The transmitter also
uses a PICAXE-08M chip and will take
you less than five minutes to assemble.
In addition you have to install two
parts on the PC board and change the
SC
PICAXE program.
siliconchip.com.au
|