This is only a preview of the September 2012 issue of Silicon Chip. You can view 22 of the 112 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 "Colour MaxiMite Microcomputer, Pt.1":
Items relevant to "Barking Dog Blaster: Shut That Annoying Mutt Up!":
Items relevant to "USB Test Instrument Interface For PCs":
Items relevant to "Digital Sound Effects Generator":
Purchase a printed copy of this issue for $10.00. |
Digital Sound
Effects Module
This inexpensive, compact module can
play back up to eight different sound effects,
lasting a total of 60 seconds or more. It’s
powered from a lithium cell or from a lowvoltage AC or DC supply and can be used with
model railway layouts or any other application
requiring sound effects.
T
HIS LITTLE MODULE is quite
simple but we’ve packed a host
of features into it. You can upload a
variety of sounds from a computer via
its USB port and it will then play back
the sounds when triggered. It’s small
enough to be hidden away inside a
vehicle, model or wherever and it can
be triggered by a microswitch, reed
switch, pushbutton, sound or light
detector, etc.
The most obvious use is to hide it
inside a model car or train, to produce
an engine sound and a horn or whistle
effect. Or you could build it into a door
to play back a sound each time it’s
opened. You could even hook it up to
a pet door so that it plays a sound to
let you know when your pet enters or
leaves the house.
Alternatively, you could fit it with
a pushbutton for sound effects while
playing a game or have it triggered
whenever equipment is used or the
fridge door is opened. In fact, the possibilities are endless.
In operation, the unit drives an
8-ohm speaker and if the speaker is
properly baffled and efficient enough,
the playback volume level can be quite
loud (more so with an AC/DC supply than a button cell). The playback
time can be up to 60 seconds or more,
depending on the sound quality used.
The module has two inputs to trigger different sets of sound effects and
70 Silicon Chip
each trigger can be assigned to any set
of the eight possible sound effect slots.
When triggered, it can either randomly
pick one sound from the selected set
or you can have it cycle through them
in sequence.
To keep the unit small and the cost
low, it uses virtually all SMDs (surface
mount devices). We’ve chosen the
easiest SMDs to solder so that just
about anyone can build it, given some
patience. The circuit is based around
two ICs, a PIC microcontroller and
an LM4819 low-power audio amplifier. Up to 108KB of the PIC’s internal
flash memory can be used for sound
storage but if that isn’t enough, it can
be expanded to over 1MB (more on
this later).
PWM sound generation
We initially considered using a PIC
microcontroller with an inbuilt DAC
(digital-to-analog converter) for sound
playback. Unfortunately, few PICs
contain an audio DAC and those that
do require a regulated supply of 2.73.3V. This isn’t really suitable for use
with a lithium cell as they can drop
below 2.7V under load or if a bit flat.
Rather than add the complexity of a
boost regulator to maintain the voltage,
we decided to use a standard PIC with
two high-speed PWM outputs. These
are used to drive low-pass filters, so
that we effectively build our own
By NICHOLAS VINEN
simple DAC. In practice, this works
quite well and gives performance comparable to a dedicated 10-bit or 12-bit
DAC, with quite an acceptable level of
distortion – typically less than 0.2%.
Block diagram Fig.1 shows the
general arrangement. IC1 produces
two PWM waveforms, each with a
duty cycle variable from 0-100% in 64
steps (26). The output from pin 7 (RP2/
PWM0) is determined by the six most
significant bits of the 12-bit sampled
waveform being played back, while
pin 2 (RP0/PWM1) has a duty cycle
based on the six least significant bits.
This second output is used to provide
smaller output voltage steps for better
resolution.
These two square waves each pass
through low-pass RC filters, to remove
most of the high-frequency harmonics
and produce voltages which are proportional to the input duty cycles. The
34kHz -3dB roll-off point ensures that
there is little attenuation of audible
frequencies.
After filtering, the signals are mixed
with a ratio of 64:1, to reconstruct the
12-bit digitally-sampled voltage level.
Refer to the panel later in this article
(Using PWM To Reproduce PCM
Audio) for a detailed explanation of
how the two 6-bit PWM outputs are
combined to give the equivalent of a
12-bit output.
We chose six bits per output for
siliconchip.com.au
187.5kHz 6-BIT PWM
(6 MOST SIGNIFICANT BITS)
MICRO
CONTROLLER
IC1
RP2/PWM0
7
LPF
(34kHz)
x1
MIXER
LPF
(34kHz)
x1/64
RP0/PWM1
2
(LONGER TIMEBASE)
4
3
Vdd
LPF
(34kHz)
5
20k
20k
8
8
SPEAKER
AUDIO
AMPLIFIER
(IC2)
187.5kHz 6-BIT PWM
(6 LEAST SIGNIFICANT BITS)
7
DUAL 6-BIT PWM DAC (~11 BIT EQUIVALENT)
Fig.1: block diagram of the Sound Effects Generator Module showing how the PIC micro reproduces the audio. IC1
generates two PWM square waves based on stored audio data and these signals fed through low-pass filters before
being mixed with a 64:1 ratio. The output of the mixer is filtered further and then passed to IC2, a low-power audio
amplifier which drives the 8Ω speaker in bridge mode.
two reasons: (1) a total of 12 bits
gives a good compromise between the
memory required to store an audio file
and the resulting playback quality;
and (2) this allows us to have a PWM
frequency well above the -3dB point
of the required low-pass filters, so that
the latter are reasonably effective.
The output from the mixer passes
through another low-pass RC filter to
further remove switching noise and
is then fed to the non-inverting input
of audio amplifier IC2. As shown,
this stage drives the speaker in bridge
mode. This not only maximises the
audio output power (important given
the low supply voltage of ~3V) but also
avoids the need for a large DC-blocking
output capacitor.
IC2 operates with a gain of +1 for
the non-inverting output and a gain
of -1 for the inverting output, giving
an overall gain of 2. It’s able to deliver
about 100mW to the speaker, which
produces quite a reasonable volume
if the speaker is efficient. In practice,
the available power is limited by the
lithium cell.
Fig.2(a) shows a scope grab of the
audio output when reproducing a
sinewave. It’s zoomed in far enough
to show the remnants of the highfrequency PWM signal but you can
also see the curved sinewave shape.
When we change the scope’s time base
to “zoom out”, we see from Fig.2(b)
siliconchip.com.au
Table 1: Playback Time vs Sample Rate & Bit Depth
Sampling Rate & Bit Depth
No Flash Chips
One Flash Chip
Two Flash Chips
8kHz, 8-bit
14s
80s
125s
11.025kHz, 8-bit
10s
58s
105s
8kHz, 12-bit
9.5s
53s
97s
11.025kHz, 12-bit
7s
38s
70s
22.05kHz, 12-bit
3.5s
19s
35s
32kHz, 12-bit
2.5s
13s
24s
44.1kHz, 12-bit
1.5s
9.5s
17.5s
48kHz, 12-bit
1.5s
8.5s
16s
that the waveform is quite smooth
(ignoring supersonic frequencies).
Interpolation
While the PWM outputs operate at
around 187.5kHz, the audio sampling
rate is a lot lower. If we simply changed
the PWM duty cycles at the sampling
rate of the audio file being replayed (eg,
11,025Hz), the output would have visible steps as shown in Figs.2(c) & 2(d).
This would result in extra harmonic
content in the audio output which
would sound quite bad, especially at
lower sampling rates due to the larger
effective step size. In fact, the audio
produced using this technique sounds
rather “crackly” – not good!
The simplest solution is linear interpolation. This involves changing the
PWM cycle a little for each pulse, for
the same total change over time but in
smaller increments. In fact, Figs.2(a)
& 2(b) show the identical waveform
to Figs.2(c) & 2(d) but the former have
the linear interpolation enabled. As
you can see, the resulting waveform
is much smoother and it sounds a lot
better too.
This interpolation requires a lot
more processing in the PIC. Each time
a new sample value is loaded, it must
calculate the required slope and given
the low PWM resolution (six bits), this
is often going to be a fractional value so
we need to do some fractional maths
to generate a smooth ramp.
The PIC18F27J53 is (just) powerful
enough to do this with some carefully
written code. With a 187.5kHz PWM
September 2012 71
Fig.2(a): a close up of the audio output from the module
(output of IC2), showing the residual PWM signal that isn’t
filtered out, plus the smoothly varying level of the sinewave
which is being played back.
Fig.2(b): the same sinewave signal as Fig.2(a) but with a
longer timebase. The low-pass filtering of the scope’s input
circuitry has rendered the switching residuals invisible,
leaving just the smoothly varying output.
Fig.2(c): the same sinewave (11.025kHz sampling rate)
being played back without the linear interpolation
code active. The resulting steps cause audible artefacts,
especially with lower sampling rates.
Fig.2(d): another view of the non-interpolated sinewave
with a longer time base, clearly showing the steps which
result from the limited time resolution available at low
sampling rates.
update rate and a maximum instruction clock rate of 12MHz, we have
just 12M/187.5k = 64 instructions
to perform these calculations. In the
end, we were able to make the code
fast enough, using an 8-bit fractional
sample-position counter and a handoptimised 8 x 12-bit multiply/scale
function to integrate the computed
delta (ramp) value over time.
Circuit description
Now take a look at Fig.3, the
complete circuit diagram. The three
low-pass filters and mixer shown in
Fig.1 are implemented using three
resistors (two 10kΩ & one 620kΩ) and
two 470pF capacitors. This is possible
72 Silicon Chip
because the two first-stage low-pass
filters and the mixer are combined.
You can essentially think of it as two
low-pass RC filters with a common
capacitor.
In addition, the different resistor
values effectively form an attenuator
between the two PWM outputs, to give
the correct (approximate) mixing ratio.
The relatively small capacitor
value (470pF) was chosen to minimise distortion due to loading on the
microcontroller outputs, which have
limited current capability. The second
low-pass filter is similar to the first and
is connected between the mixing node
(ie, the junction of the 10kΩ and 620kΩ
resistors) and the non-inverting input
(pin 3) of amplifier IC2.
In this configuration, IC2 only needs
two additional components to operate:
a 1µF supply bypass capacitor and a
10nF capacitor to filter its internal
half-supply voltage generator. This
latter capacitor also determines how
long it takes to go into and out of sleep
mode, which is used to minimise
power consumption when no sound
is being played. We want to play back
sounds immediately when triggered,
so the 10nF capacitor gives a turn-on
time of just 10ms.
Audio amplifier IC2 drives the
speaker in bridge mode via CON4.
The circuit is DC-coupled so IC1 is
programmed to deliver an average
siliconchip.com.au
CON3
10
+
D1 BAT54C
47 F
25V
A2
POWER IN
5 – 24V
1 F
Vdd
OUT
IN
K
–
Q1
DMP2215
REG1
LM2936MP-3.3
A1
100 F
16V
GND
PWDET
20
2
15
3
16
PWDET
CON2
10k
CS1
CS2
SCK
1k*
SDO
ICSP/TRIGGER
1
GND
TRIG2
TRIG1
2
3
Vdd
4
5
SDI
1k*
CON1
14
6
1 F
D–/RC4
D+/RC5
1 F
620k
6
1
10k
3
4
VUSB
2
Vss1
8
14
1
8
4
1
SDO
SDI
SCK
CS1
VO1
BYPASS
8
SPEAKER
5
CON4
GND
7
Vss2
19
Vdd
5
2
6
1
8
Vcc
SDI
SDO
SCK
CS
WP
IC3
AT25DF
041A
GND
4
3
5
SDO
2
SDI
HOLD
6
SCK
7
1
CS2
DIGITAL SOUND EFFECTS MODULE
SDI
WP
IC4
AT25DF
041A
SDO
SCK
CS
GND
4
HOLD
3
7
100nF
(OPTIONAL)
A2
K
A
LM2936MP
DMP2215L
BZX84B5V1
K
A1
8
Vcc
100nF
BAT54C
2012
IC2
LM4819
–IN
(OPTIONAL)
SC
VO2
+IN
10nF
Vdd
IC2, IC3, IC4
Vdd
SHUT
DOWN
470pF
470pF
VddCore
10 F
IC1
ZD1
BZX84B5V1
A
1 F
Vdd
17
3
RC6
RA1
24
2
RB3
RP0/PWM1
23
7
RB2
RP2/PWM0
25
SCK1/RB4
IC1
1
MCLR PIC18F27J53
10k
26
SDI1/RB5
18
SDO1/RC7
28
PGD/RB7
27
PGC/RB6
* SHORT OUT FOR
PROGRAMMING
28
620k
Vdd
A2
4
K
10k
K
1
CR2032
BATTERY
G
D2 BAT54C
A1
USB
TYPE B
D
S
D
G
S
TAB (GND)
IN
GND OUT
Fig.3: complete circuit of the Sound Effects Generator. IC1 generates the PWM waveforms which are filtered and then
passed to audio amplifier IC2. IC3 and IC4 are optional flash memory chips for more storage space and these are
controlled using a 5-wire serial bus. REG1 provides a regulated 3.3V rail when the unit is plugged into a USB port
or is running from an external supply; the rest of the time, it runs off a CR2032 lithium cell. Sounds are triggered by
pulling pins 4 or 5 of CON1 low and CON1 can also be used to program IC1 with an in-circuit serial programmer.
modulated output of 50% to prevent a
large DC voltage from appearing across
the speaker.
More memory
The firmware occupies 20KB of IC1’s
128KB internal flash memory, leaving
108KB available for sound storage.
This will be sufficient for some applications but if you want multiple
sound effects or longer sounds, you
will need more space than this.
siliconchip.com.au
In practice, the total flash memory
can be expanded to either 620KB or
1132KB by adding one or two low-cost
serial flash chips – IC3 and IC4. These
each store 4Mbit (512KB) of data. IC1
automatically detects whether either
or both chips are installed at power-up.
Table 1 shows the total playback
time available with various combinations of IC3 and IC4 installed. IC1
communicates with the flash chips
using a 3-wire SPI (serial peripheral
interface) bus plus two chip-select
lines – CS1 and CS2.
The specified flash chips (AT25DF041A-SSHF) were chosen for their
wide operating voltage range (2.33.6V) and low power consumption.
IC1’s minimum operating voltage is
2.15V but in practice, we expect that
all the ICs will run down to about
2V. The supply voltage for IC3 and
IC4 is critical during erase and write
operations, during which time they
September 2012 73
CON2
Q1
620k
10k
D2
ZD109109121
1 F
D1
SFX
POWER
1
(MINI
SPEAKER)
10nF
IC2
TRIG1
TRIG2
GND
CON1
ICSP
620k
470pF
POWER
IC3
470pF
1
1 F
100 F
100nF
1
IC4
ICSP
10k
10k
5
10
10 F
1
10k
BAT1
47 F
1k 100nF
1
(BUTTON
CELL
HOLDER)
REG1
1
1k
1 F
IC1
PIC18F27J53
1 F
SPKR
TOP OF PC BOARD
SPKR
UNDERSIDE OF PC BOARD
Fig.4: the SMD parts all mount on the top side of the PCB while the through-hole parts, including the cell holder, are
mounted on the bottom. CON1 is a friction-fit for programming but can be soldered in to connect the trigger inputs if
you don’t want to solder wires direct to the PCB. Note that there is room for a small speaker to be taped to the bottom
of the PCB but an off-board baffled speaker will give better results.
run from a regulated 3.3V rail derived
from an external PC’s USB port, via
D1 and REG1.
Sleep mode
When the module is not plugged
into a USB port and not playing any
sounds, IC1 goes into sleep mode to
save power and the whole circuit typically draws less than 10µA from the
CR2032 cell. If IC3 or IC4 are installed,
they are placed in “Deep Power-down”
mode which, according to the data
sheet, gives them a typical current
consumption of 15µA each.
You would expect then that installing IC3 and/or IC4 would reduce the
standby cell life substantially. However, we measured the actual sleep
current for IC3 and IC4 at about 2µA
each. This likely reflects manufacturing process improvements since the
AT25DF041A data sheet was written
and we expect most constructors will
find that installing these chips has
little effect on cell life.
During playback, IC3 and IC4’s operating current is negligible compared
to that of IC1 and IC2, due to the low
data rate (72kbits/s maximum).
USB interface
The PIC’s USB interface is used to
transfer sound data for later playback.
It’s also used to configure the various
trigger options. The only external
component required for the PIC to
communicate via USB is the mini-B
type connector (CON2). The necessary
USB impedance-matching and pull74 Silicon Chip
up resistors for the D+ and D- communication lines (pins 16 & 15) are
inside IC1.
In operation, the PIC monitors the
USB VCC line, to determine when the
unit is plugged in. This is necessary
so that the internal USB module can
be turned off at other times to save
power. The method used will be explained shortly.
Power supply
When a CR2032 3V lithium cell
is installed, it powers all the ICs via
Mosfet Q1, which provides reverse
polarity protection. Q1 is a P-channel
type with its gate tied to ground via a
620kΩ resistor, so that it is switched
on by default. However, if the cell is
somehow inserted backwards, its gate
will instead go positive compared to
its source. In that case, Q1 switches off
and its internal body diode is reverse
biased, so no current can flow.
Conversely, when it’s on, Q1 has a
very low on-resistance (<0.2Ω). As a
result, there is very little voltage drop
across it, given the low current drain
from the battery (<50mA).
As stated, the circuit can also be
powered via the USB port or from an
external DC or AC supply. In these cases, the 3.3V supply for IC1-IC4 comes
from REG1, an LM2936 low-dropout
linear regulator. This is especially important for USB communications, as
IC1 requires a supply rail that’s close
to 3.3V for proper USB operation.
When an external supply is present
and the LM2936 is powering the ICs,
its output voltage will typically be
above the cell’s voltage (nominally
3V). As a result, we need to prevent
it from charging the cell, which could
damage it.
This function is also performed by
Q1. The external supply voltage pulls
Q1’s gate high via dual Schottky diode
D2 and a 10kΩ resistor. One half of
this diode conducts if an external USB
supply is connected, while the other
half conducts if an external supply
is fed in via CON3. As a result, Q1 is
switched off and no current can flow
into the cell (since Q1’s internal body
diode is also reverse biased).
Note that dual-diode D2 is necessary
so that you can’t accidentally feed
power from CON3 into the computer’s
USB port (if connected).
Zener diode ZD1 protects both Q1
and pin 17 of IC1 from damage should
the external supply be above 5.5V. Pin
17 of IC1 is used to detect when external power is applied, to enable the USB
transceiver (this pin is 5.5V-tolerant
and so can be used for this task). The
software sets this pin as an interrupt
source, so it can wake the micro when
the USB interface is connected.
DC/AC supply
The external supply can be either
5-24V DC or 5-24V p-p (peak-to-peak)
AC and is fed in via CON3. This suits
many applications, including a model
railway system with DCC, which uses
a 15-22V AC square wave. For AC, one
half of dual-Schottky diode D1 rectifies the supply voltage while for DC,
siliconchip.com.au
this diode provides reverse polarity
protection.
A 47µF 25V electrolytic capacitor
filters the resulting supply rail while
a 10Ω series resistor limits the in-rush
current when power is first applied.
This prevents D1 from burning out
when the unit is first powered up. As
with USB power, REG1 then provides
the 3.3V supply for the ICs.
REG1 can pass up to 50mA, which
gives an instantaneous dissipation of
around 1W with a 24V input. That
would be too high if it were sustained
but in practice, power is drawn in
bursts by the audio amplifier. This
lowers the average dissipation to an
acceptable level.
Trigger inputs
CON1 serves both as an ICSP (incircuit serial programming) header
for IC1 and as the trigger input connector. For programming, the two 1kΩ
series resistors must be shorted out.
These resistors normally protect the
IC inputs from accidentally applied
voltages above 3.3V during operation
(eg, you can use a 0-5V trigger signal
if necessary).
Normally, to trigger a sound, either
TRIG1 or TRIG2 is pulled to ground
although the unit can be re-configured
to invert the trigger logic.
Software
IC1’s software must perform a number of tasks. As explained earlier, it
goes into and out of sleep mode as
necessary, powering up the USB interface and the serial flash chips only
when needed. Pin-change interrupts
on pins 17, 27 & 28 are used as wakeup signals.
When the USB interface is enabled,
the module appears as a virtual serial
port. The XMODEM protocol is used to
upload audio files (8-bit or 16-bit mono
WAVs). Configuration commands are
sent as text over the serial port and the
module responds to indicate that they
have taken effect. You can also query
some information from the module,
such as how much memory is free.
When you upload a WAV sound file,
it checks that the format is valid and
that there is enough free memory, then
stores it. If a 16-bit file is uploaded, it
is converted to 12-bit format on-thefly, to save memory and speed up the
playback code.
There are a number of configuration
options such as whether the sounds are
siliconchip.com.au
Features & Specifications
Module size: 59 x 28 x 13mm
Trigger inputs: 2
Number of sound effects: 1-8, triggered round-robin or random
Audio sampling rate: 8-48kHz
Audio resolution: 8-bit or 12-bit
Sound memory: 108KB, 620KB or 1.12MB
Total playback time: 1.5-125 seconds depending on sampling rate & data memory
(see Table 1)
Output power: Approx. 100mW into 8Ω
Supply options: CR2032 lithium cell, USB 5V, DC 5-24V, AC 5-24V peak-to-peak
Cell operating voltage: 2.15-3.3V (2.3-3.3V with memory >108KB)
Standby current: typically 9-14µA, depending on installed memory
Standby cell life: >1 year
Playback cell life: 4-24 hours, depending on sound volume, etc
Configuration interface: USB (mini type B socket)
USB protocol: virtual serial port (CDC), file transfer via XMODEM
Computer operating system: Windows XP, Vista, Windows 7*
* In theory, the module will work with Linux and Mac OSX using the CDC driver but
we haven’t tested it. The driver will need to recognise our Vendor ID and Product
ID (04D8, FD52).
looped, whether the sound continues
playing to the end of the file once the
trigger input is released, which input
has priority, how to deal with multiple sounds and so on. These are set
using text commands over the USB
serial interface and stored in IC1’s flash
memory to be used when the unit is
triggered (more on this later).
Construction
The Digital Sound Effects Module
is built on a double-sided PCB coded
09109121 and measuring 28 x 59mm.
Fig.4 shows the parts layout. The first
job is to fit the surface-mount devices
to the top side of the PCB.
Start by laying the board flat on your
workbench and fitting the USB connector (CON2). This has two plastic
locating posts on the underside which
go into matching holes on the PCB.
Ensure that the socket end is at the
edge of the PCB and that the connector
is sitting flat, then solder one of the
mounting feet.
That done, check that the five pins
are properly aligned on their pads,
then solder the other three feet plus
the five pins. You will have to angle
the soldering iron when soldering the
pins, as they are under the main body
of the connector.
Don’t worry about solder bridges
at this stage; just make sure they are
soldered correctly. It’s then simply a
matter of using solder wick to clean up
the bridges (note: adding a bit of flux
paste makes this much easier). Finally,
check that the bridges are gone using
a magnifier; if not, add more flux and
fix them.
The four SOT-23 (small-outline transistor package) devices can be installed
next (ie, D1, D2, Q1 & ZD1). Be sure to
remove them from their packaging one
at a time so you can’t get them mixed
up (they look virtually identical).
In each case, it’s just a matter of placing a small amount of solder on one
of the pads, then reheating the solder
while you slide the device into place.
If it isn’t aligned properly, simply reheat the solder and nudge it until it is
correctly aligned. The other pins can
then the soldered.
Follow with the ICs, taking care to
get the orientation correct. In each
case, pin 1 is indicated with a dot on
the PCB. IC1 and IC2 should have a
divot near pin 1 while the other two
ICs (if fitted) have a bevelled edge on
the same side as pin 1.
As before, it’s just a matter of applying some solder to one of the end pads,
then reheating this solder as the device
September 2012 75
Serial Commands For The Sound Effects Module
Commands are sent to the Digital Sound Effects Module by
typing them into the serial terminal. There are three basic types
of command: those which give you information, those which are
used to upload sound files and those which are used to change
the module’s configuration.
Most commands have an immediate effect and respond with
information after you press the enter key. If there is an error (eg,
you mistyped the name of the command), it will respond with
information about what has gone wrong.
Having prepared the sound files, the next step is to use the
Send command to upload them. If you have a speaker wired up
at this stage, you can then check that everything is working using
the Play command.
Here is the list of available commands with some information
about how to use them.
Send
Ready for file via XMODEM
Abort
Transfer aborted
Command: “Info”
Description: once you have uploaded a sound file, you can set some
options which determine how it is played back. By default, when
triggered, the sound will play once and won’t stop until the end of
the file (unless interrupted, see below). If you want it to loop as
long as the trigger input is held on, use the “loop” option (or “once”
if you don’t; this is the default). If you want the sound to stop as
soon as the trigger input is released, rather than wait for playback
to complete, use the “partial” option (the default is “complete”).
Example:
Options 1 loop, partial
Sound #1: 22050Hz, 12-bit, 12.8s, 415KB, loop, partial
Description: displays the firmware version, amount of memory
installed and free, what sound files are loaded and the configuration settings.
Example:
Info
SILICON CHIP Sound Effects Module v1.0
Total memory: 1131.9KB
Free memory: 721KB
Sound #1: 22050Hz, 12-bit, 12.8s, 411KB, loop, stop immediately
Trigger #1: NO, sound #1, priority, random
Trigger #2: NO, no sounds, round robin
Unsaved configuration changes
Command: “Clear all”
Description: deletes all sounds loaded into the Module, freeing up
all memory for new sounds
Example:
Clear all
Memory cleared, 1131.9KB free
Command: “Clear last”
Description: deletes the last sound loaded into the Module, freeing
up the memory it occupied.
Example:
Clear last
Sound #2 cleared, 721KB free
Command: “Send”
Description: initiates the upload of a sound file to the module. After
a successful Send command, the sound is uploaded via XMODEM.
The sound file is given the next available index, starting with #1.
Example:
Send
Ready for file via XMODEM
Saved to index #1
Command: “Abort”
Description: cancels a pending Send command. Can be used if
the transfer failed for some reason but the unit is still waiting for
it to finish. You can also re-start a transfer by doing a Send command again.
Example:
76 Silicon Chip
Command: “Play <index>”
Description: immediately plays back the sound loaded in the specified location. The USB interface does not respond during playback.
A response will be sent once playback is complete and the serial
port interface is then ready for more commands.
Example:
Play 1
Playing file #1 (12.8s)...
Playback complete
Command: “Options <sound index> <options>, <option> ...”
Command: “Sounds <trigger index> <sound index>, <sound
index> ...”
Description: allocates one or more sounds to a trigger index (1
or 2). Sounds can be allocated to either or both trigger inputs.
This determines which sounds are played back when the specified
trigger input is activated (one at a time, see below for information
on how they are chosen).
Example:
Sounds 1 1
Trigger #1: NO, sound #1, priority, random
Command: “Trigger <trigger index> <option>, <option> ...”
Description: sets the options for trigger 1 or 2. The available options are “NO” or “NC” to set the input mode to suit normally open
or normally closed switches (or active low and active high signals,
respectively), “priority” (which allows it to interrupt sounds which
are triggered by the other input) or “nopriority”, “roundrobin” (with
multiple sounds allocated, they are played in sequence) or “random”
(with multiple sounds, one is randomly selected each time).
Example:
Trigger 1 priority, random
Trigger #1: NO, sound #1, priority, random
Command: “Save”
Description: configuration commands (except for Send) are not
permanently saved until this command is executed. If you don’t
save configuration changes, they will be lost when the unit loses
power.
Example:
Save
Configuration saved
siliconchip.com.au
Digital Sound Effects Module: Parts List
1 PCB, code 09109121, 28 x
59mm
1 PCB-mount button cell holder
(Jaycar PH9238)
1 CR2032 lithium cell
1 5-pin header, 2.54mm pitch
(CON1)
1 SMD USB connector, mini-B
type (CON2) (eg, Altronics
P1308)
1 8Ω mini-speaker (eg, 27mm or
40mm diameter)
1 100mm length 2-wire ribbon
cable
1 USB cable, type-A plug to miniB plug
4 M3 x 9mm tapped Nylon
spacers
4 M3 x 6mm machine screws
Semiconductors
1 PIC18F27J53-I/SO microcontroller programmed with
0910912A.hex (IC1)
1 LM4819 audio amplifier [SOIC8] (IC2) (Digi-Key LM4819MXCT-ND) OR
1 LM4889MA 1W audio amplifier [SOIC-8] (IC2) (Element14
1286916)
1 LM2936MP-3.3 50mA 3.3V LDO
regulator [SOT-223] (REG1)
(Element14 1469062)
1 DMP2215L P-channel Mosfet
[SOT-23] (Q1) (Element14
1713864)
1 5.1V zener diode [SOT-23]
(ZD1) (Element14 1431236)
2 BAT54C dual Schottky diodes
[SOT-23] (D1, D2) (Element14
1467518)
Capacitors (SMD 3216/1206
unless specified)
1 100µF PCB-mount low-profile
electrolytic (eg, Element14
9452567)
1 47µF 25V PCB-mount low-profile
electrolytic (eg, Element14
1165523)
1 10µF
4 1µF
1 10nF
2 470pF
BitScope
Digital + Analog
w
Ne del
o
M
Pocket A
nalyzer
Everything in one tiny 2.5" package !
100 MHz Digital Oscilloscope
Dual Channel Digital Storage Oscilloscope
with up to 12 bit analog sample resolution
and high speed real-time waveform display.
40 MSPS x 8 Channel Logic Analyzer
Captures eight logic/timing signals together
with sophisticated cross-triggers for precise
multi-channel mixed signal measurements.
Serial Logic and Protocol Analyzer
Resistors (SMD 3216/1206,
0.25W 1%)
2 620kΩ
2 1kΩ
4 10kΩ
1 10Ω
Optional parts for
longer playback time
2 AT25DF041A-SSHF-B 4Mbit
flash memory ICs [SOIC-8]
(IC3, IC4) (Element14
1636622)
2 100nF ceramic chip capacitors
[3216/1206]
Capture and analyze SPI, CAN, I2C, UART &
logic timing concurrently with analog. Solve
complex system control problems with ease.
Real-Time Spectrum Analyzer
Display analog waveforms and their spectra
simultaneously in real-time. Baseband or RF
signals with variable bandwidth control.
Waveform and Logic Generators
Generate an arbitrary waveform and capture
analog & digital signals concurently or create
programmable logic and/or protocol patterns.
Multi-Channel Chart Recorder
Record to disk anything BitScope can capture.
Allows off-line replay and waveform analysis.
Export captured waveforms and logic signals.
Protocol Analyzer
Note: the PCB & the programmed
PIC micro are available from
SILICON CHIP PartShop.
Digital Oscilloscope
is slid into place. That done, solder
the diagonally opposite pin, then solder the remaining pins, ignoring the
inevitable solder bridges.
Removing the solder bridges
Once the device is in place, apply
a thin layer of flux paste along both
rows of pins, then clean up the solder
bridges with solder wick. That’s done
by first placing the solder wick alongside (but not on top of) the pads. The
soldering iron is then placed on top of
the solder wick and the wick gently
slid towards the solder on the pads.
As the wick heats, it will start to melt
the flux and the excess solder, creating
visible smoke. At that point, you can
slide it right up against the pins and
most of the excess solder should then
be sucked into the braid.
siliconchip.com.au
Repeat this procedure until all the
solder bridges are gone. It’s not strictly
necessary to clean off the flux residue
provided you are using no-clean (noncorrosive) flux. However, if you do
want to clean it off, this can be done
using pure alcohol (eg, isopropanol).
For a more detailed description on
soldering in SMD ICs, refer to pages
80-82 of the June 2012 issue.
The passive SMDs are next on the
list. These include nine 3.2 x 1.6mm
(3216) resistors and 8-10 similarly
sized ceramic capacitors. The resistors
have their value code printed on top
but the capacitors will be unlabelled.
As before, the best tactic is to remove
them one at a time from their packaging, so you don’t get them mixed up.
Regulator REG1 is the last SMD components to be installed. It’s mounted
Spectrum Analyzer
Compatible with major operating systems including
Windows, Linux & Mac OS X, Pocket Analyzer
is your ideal test and measurement
companion.
bitscope.com/sc
September 2012 77
30
Using PWM To Reproduce PCM Audio
20
10
0
2
0
10
20
30
0
2
2
1
2
2
3
24
Fig.5(a): 32-sample sinewave with 30 voltage steps (5-bit resolution)
8
7
6
5
Uncompressed digital audio is stored in Pulse-Code Modulation
or PCM format. This consists of a series of numbers which represent
a proportional voltage at a point in time. The voltage is sampled at
a fixed rate (the sampling rate) and stored. The resulting numbers
form a representation of the audio waveform.
Refer to Fig.5(a); this shows a sinewave converted to 5-bit PCM
with 32 samples. With five bits, we have about 30 voltage steps
and as you can see, some of the sample points (blue) don’t quite
line up with the original waveform (red). In reality, we use more
than five bits but this is just an example.
At the bottom of Fig.5(a) is a plot of the five data bits. Consider
the first sample (left-most blue dot), which has a value of 15. This
is encoded as 20 + 21 + 22 + 23 (1 + 2 + 4 + 8) and hence the bits
corresponding to these numbers are high (one) whereas the top
bit, 24, is initially low (zero). Some bits then flip as the sampled
value changes and a new sample is binary encoded.
Now look at Fig.5(b). We’ve taken the three most significant bits
(MSBs) and re-numbered them to start from zero, without changing the data. The resulting sample values are plotted above. The
resulting waveform has the same general shape as the original but it
lacks the fine details since the least significant bits (LSBs) are gone.
This is a type of audio decimation; dropping some of the LSBs is
an easy way to reduce the amount of data required to store a PCM
stream but it also reduces the audio quality. It’s how we convert
16-bit audio to 12-bit for storage in the Digital Sound Effects Module
(as mentioned early in the main article).
In Fig.5(c) we plot the two LSBs missing from Fig.5(b). This
4
30
3
2
1
0
2
20
0
10
20
30
0
10
21
2
SUM
2
Fig.5(b): three most significant bits with corresponding waveform
0
3
0
2
0
10
20
30
0
0
10
20
30
Fig.5(d): summing waveforms with appropriate scaling
reproduces original wave shape; deviations are due to
limitations of the 5-bit resolution
21
Fig.5(c): two least significant bits with corresponding waveform
in a similar manner to the ICs and
SOT-23 devices.
Through-hole parts
Now flip the PCB over and mount
the cell holder. The two electrolytic
capacitors can then be fitted, with their
longer leads going through the holes
marked “+” on the PCB.
78 Silicon Chip
Finally, complete the assembly by
fitting four M3 x 9mm tapped Nylon
spacers to the corner mounting holes.
These are secured using M3 x 6mm
machine screws.
Programming
If you don’t have a pre-programmed
PIC (available from the SILICON CHIP
PartShop), program it now. To do
this, temporarily short out the two
1kΩ resistors (eg, using a lead off-cut)
and then plug (or solder in) a 5-way
pin header in for CON1 (friction will
hold it in place).
That done, connect an ICSP (in-circuit serial programmer) with its pin 1
orientated as shown. If you are using a
siliconchip.com.au
shows the “residual” or the difference
between the original and decimated
waveforms. If we add this waveform to the
decimated version with the correct ratio,
we reconstruct the original 5-bit audio
data, as shown above. This summing can
occur either digitally or in analog.
The 12-bit audio used in the Digital
Sound Effects Module sounds almost as
good as 16-bit audio but only requires 75%
as much memory. Using the technique
shown here, we split each 12-bit sample
into the six MSBs and the six LSBs.
These sample values are each then
converted to an analog voltage using a pair
of 6-bit PWM (pulse width modulation)
outputs on the microcontroller. A sample
value of 0 give us a 0% duty cycle and a
value of 63 (26 - 1) gives us a near-100%
duty cycle. The PWM square waves pass
through low-pass RC filters which remove
most of the switching noise and give us
smoothly varying voltages which are
proportional to the duty cycles and hence
sample values.
All that’s left is to then sum the two
resulting analog waveforms in the correct ratio (64:1). This is achieved using
a resistive voltage divider/mixer and we
then have an analog signal which closely
approximates the original audio waveform,
to within about 0.025% (1/4095).
PICkit programmer, you can power IC1
at 3.3V from its inbuilt power supply.
Alternatively, you can connect a
USB cable from a PC for power (although the device won’t be detected
yet). The software, 0910912A.hex, is
available from the September 2012
downloads section on the SILICON CHIP
website (www.siliconchip.com.au).
Unplug the CON1 pin header when
you have finished programming IC1.
and tells you how to determine which
COM port number has been assigned.
That done, download and install the
free serial terminal program TeraTerm
Pro (available from www.ayera.com/
teraterm/). Launch it and open the port
assigned to the USB driver.
Now type “Info” and press enter and
you should get a response confirming that the module is working and
showing its firmware version and the
amount of free memory.
Installing the driver
Assuming that the PIC micro has
been programmed, the next step is
to plug the unit into a USB port and
check that it is detected. A message
should pop up adjacent to the System
Tray saying “Installing device driver
software” (Windows 7) or “Found New
Hardware” (XP) – see Fig.6. You will
then be prompted to locate a driver.
You need to use the SILICON CHIP
USB serial port driver, which is also
available for free download from the
SILICON CHIP website.
This is the same driver as used for
the Maximite and several other SILICON CHIP projects. The accompanying
panel details the driver installation
siliconchip.com.au
Oversampling
Using 6-bit PWMs gives a maximum
switching frequency of 188kHz (the 12MHz
CPU clock divided by 26 or 64). We want a
switching frequency well above 20kHz so
that we can filter out most of the switching
harmonics without affecting the audible
frequencies (20Hz-20kHz).
A typical audio sampling rate is between
8kHz and 48kHz, giving between 23.4 and
3.9 PWM pulses per audio sample. To
give a smooth output waveform without
lowering the switching frequency (for the
reasons explained above), we generate
intermediate steps for the samples so that
we can update the PWM duty cycle after
each pulse.
You can think of this as a joining-thedots exercise (see Fig.5(b)). The original
samples are shown as blue dots but we
could also put dots anywhere along the
red line segments joining them to get an
estimated intermediate sample value that
we can then use to update the PWM duty
cycles, making the output less “steppy”
and more smooth. This is shown in the
scope grabs in the main article.
Preparing the sound files
You now need to prepare the sound
file(s) so that they can be uploaded to
the module. They must be saved as
mono 8-bit or 16-bit PCM WAV files
with a sampling rate of between 8kHz
and 48kHz.
To check the format of your files or
to convert them if necessary, you can
use a free sound-file editing program
called “Audacity” (http://audacity.
sourceforge.net/download/).
Load up Audacity and open a sound
file. At the left side of the window, you
can see whether it is stereo or mono. If
stereo, use the “Stereo Track to Mono”
option from the “Tracks” menu to mix
them together.
At the bottom of the screen, select
the desired sampling rate (labelled
“Project Rate (Hz)”). Keep in mind
that higher sampling rates give better audio quality but also use more
memory. There’s no point selecting a
higher rate than that of the original file
(which will be the default).
Volume adjustment
The volume adjustment can now be
done. The module plays the file back
with a fixed scale (the supply voltage
will affect the volume somewhat).
If your sound files are full-scale
(ie, normalised), then the peaks may
be slightly clipped due to the limited
output power of the unit, especially if
they have heavy bass. For best results,
the audio file should be normalised to
about 1dB below full-scale. This can
be achieved by using the “Amplify”
option from the “Effects” menu, then
reducing the dB level shown by 1dB
and clicking “OK”.
If you find the sound is too soft or
too loud, you can repeat these steps
later but turn the dB level up or down
as necessary. Keep in mind that as you
increase the amplification, you may
find the sound gets distorted due to
clipping. In some cases though, this
Fig.6: these are the
messages that appear
adjacent to the System
Tray in Windows 7 (top)
and Windows XP (bottom)
when the Digital Sound
Effects Module is initially
plugged into a USB port
(ie, before the USB driver
is installed).
September 2012 79
How To Install The USB Driver
F
OLLOW THESE steps to install the USB CDC driver in Windows 7 and XP (the
procedure is similar for Vista):
(1) Plug in the Module, open Device Manager and check that it has been detected
as an “Unknown device” or “USB device” in the “Other devices” category.
(2) If the Found New Hardware Wizard doesn’t automatically launch, right-click on
the device and select “Update driver”. You may be asked whether you want to check
Windows Update for a driver. If so, select “No, not at this time” or similar.
(3) In Windows XP, choose to install the software (driver) automatically. In Windows
7, select the “Browse my computer for driver software” option. For Windows XP, select “Install from a list or specified location”.
Then for both operating systems, browse for and select the folder containing the SILICON CHIP USB driver.
(5) You will now likely get a warning that the driver is not signed. Choose to continue and install the driver anyway.
(6) Verify that the driver installation is successful. The device should now show up under the “Ports” category in Device Manager. Make a note of the COM port number assigned.
may be acceptable – it depends on the
characteristics of your sound.
Saving the file(s)
Now use the Export function from
the File menu. If you want to upload
the file to the module with a 12-bit
resolution, select “WAV (Microsoft)
signed 16-bit PCM” from the dropdown at the bottom of the file dialog (it
will be converted from 16-bit to 12-bit
by the module).
For an 8-bit resolution, select “Other
uncompressed files”, then click the
Options button. For Header, choose
“WAV (Microsoft)” and for Encoding,
select “Unsigned 8 bit PCM”. Then
click OK.
You can now select a location, type
in a name and press the Save button.
The file is then ready to be uploaded.
Repeat this process if you are going to
upload multiple sound files.
With the files prepared, plug the
module into a spare USB port, load
80 Silicon Chip
up TeraTerm Pro (or if you prefer,
another XMODEM-capable terminal
program) and connect to the virtual
serial port, as described earlier. You
can now upload the files using the
“Send” command, as described in
the “Serial Commands For The Sound
Effects Module” panel.
Using the module
With the sounds uploaded and
the configuration set (don’t forget to
save it!), you are ready to hook up the
power, speaker and trigger inputs.
Connect the speaker across the two
solder pads. Any 8Ω speaker will do
and the more efficient it is, the better.
It will also sound much better if it is
baffled.
The simplest way to do this is to
mount the speaker in a timber box.
A tuned box will give the best sound
quality but in general, any baffle is
better than none.
You can connect the speaker to the
terminals either way around, since the
phase doesn’t matter.
There is also space to glue or tape a
small (~27mm) speaker to the back of
the PCB, next to the cell holder. You
can do this if you’re really pressed for
space and your sound requirements
are modest. However, it will limit the
volume and give poor bass response.
The trigger switches must be connected between the trigger input pads
and the nearby ground pad, either via
a pin header connector or by soldering
thin wires (eg, ribbon cable) directly to
the PCB. You can use microswitches,
pushbuttons, reed switches or even the
output of a microcontroller or digital
logic IC to trigger the unit.
Having done all that, it’s just a matter of inserting the lithium cell into its
holder or wiring up the external power
supply (AC or DC) to CON3. You can
then activate one of the trigger inputs
and check that the sound(s) play back
SC
as they should.
siliconchip.com.au
|