This is only a preview of the May 2000 issue of Silicon Chip. You can view 32 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 "Building The Ultra-LD 100W Stereo Amplifier; Pt.2":
Items relevant to "Build A LED Dice":
Items relevant to "Low-Cost AT Keyboard Translator":
Purchase a printed copy of this issue for $10.00. |
Over the years, many LED Dice circuits have been
published – but none are as simple as this one!
With just one PIC micro and a handful of other
components it’s cheap and easy to build, too!
By DOUG JACKSON
F
IRST OF ALL, let’s settle an
argument before it starts. Die
or Dice? Sure, the venerable
Oxford would have us say one die,
two dice. But every man and his dog
uses the word “dice” for both singular
and plural. So we’ll stick with Fido
and use dice.
But just in case you still want to
argue, we’re correct either way with
this circuit because it contains not one
but two dice. So it’s perfect for all of
those games which require the roll of
two dice at once.
By the way, if you only want a single
version, that’s easy too: just leave out
one set of LEDs and driver resistors.
The PIC micro will never know!
Ahh, the PIC micro. We were getting to that. Using a PIC allows us to
significantly simplify our dice circuit.
Previous designs have typically used
at least two ICs, four or more transistors and many resistors and capacitors.
And they’ve been fairly current
56 Silicon Chip
hungry, discharging batteries far too
quickly.
Using a single microcontroller not
only allows simplification, it also lets
us add features that previously haven’t
been available: the ability to recall the
last roll, for example.
This is the first in a short series of articles we hope to publish over
the next few months
which will use PICs
in a variety of simple
applications.
What makes
this series a little
different is that we
intend to guide you
through the hardware
and
software design step-by-step so that
you get a better idea of the design
process.
It’s an ideal way for a beginner in
micros to get a grasp on the fundamentals.
We are not planning to print de-
tailed software descriptions, though
– magazine space simply does not
allow this.
However, a web site has been set
up to provide detailed software discussions of all the projects presented
in the series.
Before we start our design, let’s look
at the basis for all of these projects, the
PIC microcontroller.
Pick a PIC
The PIC microcontroller family
covers a wide variety of devices
incorporating embedded peripherals, such as: integrated timers;
analog-to-digital converters; digital-to-analog converters; RAM and
Electrically Erasable ROM (EEROM).
Our project will use a Microchip PIC
16F84 microcontroller. This device
has 1K of on-board flash programmable ROM, 68 bytes of RAM, 13 I/O lines
and an internal counter/timer. Each
I/O line can source or sink approxi-
The PC board version of the LED Dice was housed in a zippy
box with the LEDs and switch emerging through the front
panel. The red LEDs form one dice while the orange LEDs
form the second (yes, we know we said we used green ones!).
The second version of the LED Dice is the same circuit but
is built on Veroboard and this forms the lid of a zippy box.
Some components are mounted on the other side of the
board. Note that some component values have been altered.
mately 50mA making it ideally suited
terms are not interchangeable – a creep’ interfering with the completion
to directly driving a LED display.
microcontroller actually contains a
of our project.
The 16F84 has enjoyed significant microprocessor but it also contains
The specifications for our project
popularity in the hobbyist market re- memory, I/O (input/output) lines and are simple – we will design an eleccently. A major reason for this is that often other features.
tronic simulation of two dice, using
it has a flash ROM, making it easily
14 LEDs. A single pushbutton switch
The project
re-programmable. The advantage of
will control the rolling of the dice in
the flash ROM is that it doesn’t rethe following manner:
Before we start designing our Dice,
quire an ultraviolet eraser to erase we need to decide exactly what it does
When the button is pushed for a
the device.
and how it does it. In doing this, we short period (say less that 0.5 sec), the
M i c r o c h i p ’ s w e b s i t e a t reduce the likelihood of ‘specification dice turn on and display the result of
the last roll.
(http://www.microchip.com)
If the button is pushed
provides full documentation
for greater than about
for the entire range of PIC
0.5 seconds, both dice
devices, as well as a full
are cleared then roll indevelopment environment
dependently, eventually
(MPLAB).
slowing and stopping afA simple PIC programmer
ter the button is released.
was published in the March
In all cases, the result
1999 issue of SILICON CHIP
is displayed for 20 sec(back issues are available for
onds and then the dice
$7.00 including postage and
turns itself off.
packing [$7.70 after June]).
It would be desirable to
This programmer is suitable
have no power switch, so
for programming the devices
we have to minimise curwe will use in this series.
rent consumption while
You may have noticed we
the project is ‘off’.
use the word “microconNow that we have detroller” where many people Fig.1: all six faces of a dice with the standard patterns
cided (and written down)
use “microprocessor”. The shown.
May 2000 57
Fig.2: driving LEDs from a PIC is easy!
All you need to do is limit the current
from the PIC to a level which the LEDs
can handle – and tell the PIC to light
them up!
what we will build, let’s start the fun
stuff.
The hardware
Lets look at a good old-fashioned
dice. As we all know, it has six sides,
with one, two, three, four, five or six
“spots” or dots on each. (Did you know
that adding the opposite sides of a dice
always equals 7?)
If we analyse the various dot patterns in Fig.1, we can see that the
following rules apply:
The central dot (7) operates inde-pendently.
Opposing corner dots (1) and (3)
appear simultaneously.
Opposing corner dots (2) and (4)
appear simultaneously.
Middle dots (5) and (6) appear
simultaneously.
Fig.3: providing an on/off switch
is also simple with the right
instructions in the program.
Therefore we can actually drive all
seven LEDs from only four I/O pins
on the microcontroller.
Remembering that our goal is to emulate the operation of a standard dice
using LEDs, let’s start by connecting
some LEDs to the microcontroller.
Driving LEDs with a PIC microcontroller is a simple exercise. Because
the PIC outputs can drive up to 50mA
and LEDs typically require only 1020mA, we can drive each LED directly
via a suitable series current limiting
resistor.
Fig. 2 shows typical connection
details. But what are the values of the
current limiting resistors?
Ohm’s Law tells us that one:
R = E/I
We know that “I” is 20mA max.
and that “E” in this case is the supply
Fig.4: if timing accuracy is not
important, a simple R/C circuit
attached to the PIC’s “OSC” input
is all you need.
voltage (5.4V) less the forward voltage
drop across each LED (typically 2.1V).
So for a single LED:
R = 3.3/.02 = 165Ω.
Where there are two LEDs in series
the forward voltage drop doubles so
the formula becomes:
R = 1.2/.02 = 60Ω.
To save drain on the battery (and
therefore give it more life), we’ll be a
bit conservative and go for slightly less
current through the LEDs, resulting in
resistor values of 220Ω for the single
LEDs and 100Ω for the double LEDs.
Now that we have designed the
output, we need to consider our input;
something to “roll” the dice.
This can be done simply by connecting a pushbutton switch between the
supply voltage (VCC) and one of the
PIC inputs that provides an interrupt
Fig.5: the PIC drives
the LEDs for about 20
seconds and then goes
to sleep to conserve
the batteries.
58 Silicon Chip
Parts List –
PC Board Version
1 PC board, code 08105001,
58 x 73mm
1 130 x 67 x 44 plastic case
(Jaycar HB-6013)
1 front panel label, 124.5 x 62mm
1 4 x AA square battery holder
1 PC-mount SPST pushbutton
switch (Jaycar SP-0722)
4 9mm untapped spacers
4 M3 x 15mm CSK steel or nylon
cheese-head screws
4 M3 nuts
Fig.6: this code tells the PIC to determine a random number and store it in a
certain location, then display the result.
capability (we’ll look at interrupts
later).
Fig.3 shows an example. Note that
the input is held low by a 4.7kΩ resistor to ensure that random noise picked
up on the input pin does not cause an
input to be recorded.
Clock and power supply
All that remains is to add a power
supply and provide some sort of clock
circuit to the microcontroller. A clock
circuit, by the way, has little to do with
telling the time. It provides pulses at
a specific rate which cause the microcontroller to undertake certain tasks.
First, though, the supply: the most
simple power supply we can have is
four AA batteries. This provides 6.0V
(4 x 1.5V).
If a series diode is placed between
the batteries and the PIC, the available
supply voltage drops to about 5.4V.
This is due to the nominal 0.6V voltage
drop across a forward-biased silicon
diode. 5.4V is within the PIC’s rated
input voltage range of 4-6V whereas
6V from the batteries would be right
on the upper limit.
The series diode also protects the
PIC from damage if the battery is
accidentally connected back to front.
Traditionally, microcontroller systems have used some sort of 3-terminal
voltage regulator to ensure that 5V is
available to the CPU.
We decided not to use a 78L05 or
similar 3-terminal voltage regulator,
as the 4mA standby current drawn by
the regulator would swamp the sleep
current of the PIC (about 7µA), giving
poor battery life.
So in theory, a set of four ‘AA’ alkaline batteries with a capacity of about
800mA.h should be able to last about
114,000 hours while in sleep mode.
(That’s about 13 years . . . we suspect
that the batteries will die of their own
accord LONG before this time!).
Of course, current consumption
will increase to about 120mA during
operation.
PIC microcontrollers can use a variety of clock circuits, ranging from crystal controlled oscillators if accurate
timing is required, through to simple
RC (resistor/capacitor) networks. In
our application, we are not concerned
about speed and clock accuracy, so we
use an RC oscillator.
This is shown in Fig.4. This works
simply by charging the 100pF capacitor through the 10kΩ resistor until the
microcontroller’s threshold voltage is
reached, at which time the capacitor
discharges quickly through the microcontroller. When the voltage falls
to the micro’s lower threshold it goes
high, allowing the capacitor to start
charging once again.
The final circuit
Tying all of this together, we come
up with the circuit for the hardware
of our LED Dice simulation. This is
shown in Fig.5.
Semiconductors
1 PIC16F84 programmed
microcontroller (IC1)
1 1N4004 diode (D1)
7 5mm red LEDs (LED1 - LED7)
7 5mm LEDs, another colour
(LED8 - LED14)
Capacitors
1 10µF 16VW PC electrolytic
1 .001µF ceramic disc
Resistors (0.25W, 5%)
2 10kΩ
1 4.7kΩ
6 100Ω
2 220Ω
Parts List –
Veroboard Version
1 piece of Veroboard or other
strip board, 107 x 57mm
1 112 x 60 x 27mm plastic case
4 AA batteries
1 PC-mount SPST pushbutton
switch (Jaycar SP-0722)
Semiconductors
1 PIC16F84 programmed
microcontroller (IC1)
1 1N4004 diode (D1)
7 5mm red LEDs (LED1 - LED7)
7 5mm LEDs, another colour
(LED8 - LED14)
Capacitors
1 10µF 16VW PC electrolytic
1 .001µF ceramic disc
Resistors (0.25W, 5%)
2 10kΩ
1 4.7kΩ
6 100Ω
2 220Ω
Miscellaneous
Hook-up wire, bubble-wrap plastic
or other suitable insulation.
May 2000 59
Fig.7: here’s how to mount the PC board to the front panel. Note the
distance from the board to the LEDs and also the fact that the
electrolytic capacitor will need to be bent over to allow clearance.
Now you can see the simplicity of
using a single chip microcontroller.
The total circuit contains just one IC
and a handful of discrete components!
Random numbers
One item that we will look at from
the software is the generation of a
random number.
Mathematically, generating a truly
random number is a very complex
exercise. In our simple PIC circuit,
we can generate a random-enough
number in a couple of ways:
A seemingly random number can
be obtained by timing how long the
button is held down, using a timer
that is incremented VERY quickly.
(It would be a very rare person who
could hold the button down for exactly 2243ms every time).
Alternatively, we could implement a mathematical pseudo-random
number generator. This requires the
use of multiplication and division. A
pseudo-random generator generates a
very long sequence of numbers that
eventually repeats, after many cycles.
In our project, we use the first
method. We sample the internal
timer (TMR0) which is constantly
increment
ing at one quarter of the
clock speed (about 256kHz) and store
the sample in a variable, as long as the
button is held down.
A short code routine to perform this
function is shown in Fig.6.
As previously mentioned, the microcontroller will be spending most
of its time in sleep mode (especially
while it is sitting majestically on the
mantelpiece!). In sleep mode, the
internal oscillator is stopped and the
device consumes about 7µA.
Interrupts
In order to wake up from sleep
mode, we need to have an ‘interrupt’
Fig.8: the front panel for the PC board
occur. Interrupts can be effected from
version mates with the PC board
a variety of sources but they always
underneath.
signal some external change.
The LED Dice project that we are
building has the pushbutton connected to bit 1 of Port B (RB0). This pin
also functions as an ‘interrupt’ input.
When the voltage level on
this pin changes, an interrupt
is generated, causing the PIC
to stop whatever it was doing
and to do something else. It
is this interrupt that causes
the PIC to wake up from its
sleep mode.
Interrupts in the PIC can
be ‘global’ in nature (Global Interrupt Enable [GIE]
bit set) or localised. In our
example, we would like to
continue executing instructions immediately following
the ‘sleep’ command, so we
need to ensure that the GIE
bit is clear. Global interrupts
cause program execution to
branch to location 4, which is
useful for a more traditional
Fig.9: this is the component overlay for the PC board version. Compare this with the
vectored interrupt approach
photograph alongside. Note that two of the LEDs (labelled LED3 and LED13) mount the
which we will cover in later
other way around to the rest. The second colour LEDs can be green, orange or yellow.
60 Silicon Chip
articles.
Code to implement the interrupt
functionality would look like that
shown in Fig.11.
Note that once the microcontroller
has received an interrupt, it wakes and
immediately disables any further interrupts. Multiple levels of interrupts
can cause unexpected program errors,
so we stop any further interrupts from
occurring.
Now that we have examined how
to implement input, output, random
number generation and interrupts, we
can tie all of this together and produce
the code that will actually run the dice.
There is a small amount of ‘glue
code’ around these functions to
produce actual running code. I recommend that you obtain the program
listings and study them for more
information.
When you study the listings, you
may find that there are faster, more elegant ways to do what has been done.
Remember that there are commercial realities as to the time spent on
producing a particular solution and
that some times, doing something the
‘no brain’, long way is actually faster to
develop. This is an embedded system
and in a simple system like this, the
emphasis is on producing a result, not
on producing the most elegant code
available.
(Have you actually looked at the
code in your microwave oven controller? Believe it or not, many of these
Fig.10: full-size PC board pattern
for those wishing to make their own
boards. Otherwise, use this pattern
to check commercial boards before
commencing construction.
This photo of the Veroboard version is reproduced slightly larger than actual
size, so you can see exactly where the components go. Note that some of the
components are on the other side of the board. The black object below the IC is
a header pin set with a shorting link, used as an on-off switch in the prototype.
However, this is considered unnecessary and has not been specified in the
parts list.
contain microcontrollers!)
As previously mentioned, in an article of this length it is not appropriate
to include bulk source code listings,
so the source code and corresponding
hex file to supply to the PIC programmer are available on my web site
(http://www.dougzone.com).
PIC programming
To make the LED Dice operate you
need to load the LED Dice program
into a PIC. You can either purchase
a pre-programmed PIC or you can
program one yourself. Programming
one yourself allows you to enter the
world of PIC software design.
In order to program the PIC, you
need some basic tools. First, you need
the Microchip assembler and simulator (MPLAB), available as a 9MB
download from the Microchip web site
(http://www.micro-chip.com). This is
a HUGE download but you only need
it once. Remember to make a backup.
In addition to the assembler, you
need a programmer. The PIC programmer that I use is based on a design
Fig.11: this code will implement the interrupt function.
May 2000 61
with a multimeter to minimise errors.
When bending component leads,
remember that using a pair of needle
nose pliers will minimise stress while
performing the bend.
Continue the assembly by soldering
in the 18-pin IC socket, ensuring that
the indentation on the socket agrees
with the position shown on Fig.9.
Next, solder in the 14 LEDs. Be careful
with their orientation, as they will
not operate if they are installed backwards. The short leg is the cathode.
Note that two of the LEDs are
mount
ed the opposite way around
to the rest!
Mount the pushbutton switch directly to the PC board, ensuring its
straight edge is aligned as shown.
Finally, connect the battery holder,
ensuring that the batteries are not
installed.
Veroboard version
Here’s what it looks like assembled and opened out. The batteries were simply
soldered together and placed in the bottom of the case, with a piece of bubblewrap plastic to stop them moving around or shorting to the copper tracks.
by Michael Covington, which was
described in the May 1999 issue of
SILICON CHIP.
Initially, I had a some trouble getting
the published programmer to operate
with my particular parallel port, so
I built the NOPPP-2 (Experimental)
version that used a 74HC08 in place of
the diode logic that was present in the
initial version. It worked flawlessly.
The programmer software (noppp)
is available from the SILICON CHIP
web site or from Michael’s web site
(http://www.covingtoninnovations.
com/noppp/).
Once you have the tools, you
need to create a .hex file to feed to
the programmer. Start by loading up
the MPLAB software and creating a
project by selecting ‘Project’, ‘New
Project’ from the menu and typing the
name of the project (LED Dice) into
the file name box, ensuring that the
default directory is in a reasonable
location for your system.
You need to add a source (.asm) file
by clicking on the ‘Add files’ button
in the ‘Edit Project’ menu.
Now that the project has a source
file associated with it, you can assemble it by pressing F10. The build
62 Silicon Chip
process will start and a .hex file will
be produced in the default directory
specified above.
Once the program has been assembled, exit the MPLAB environment
and start the programmer (noppp).
Specify the type of PIC (16F84) and
load the .hex file.
Insert the PIC into the programmer
and select Program. The PIC will be
programmed in about six seconds. Exit
the programmer and remove the PIC
from the socket.
Construction
Two versions are presented, one on a
PC board and the other on Veroboard.
In the first, all the components
mount directly on the PC board, which
measures 58 x 73mm.
It is always wise to carefully examine any PC board prior to assembly
to ensure that there are no shorts, or
breaks present. It saves a significant
amount of time to spot them now.
A component layout for the board
is shown in Fig.9. Start the assembly
by installing the passive components
first, such as the resistors and capacitors. You may find that it is beneficial
to measure the values of the resistors
The Veroboard version is designed
to mount on the top of a medium sized
plastic zippy box, replacing the lid.
This is to allow the simplicity of the
circuit to be displayed to any curious
onlookers. If desired, the project can
be mounted inside a slightly larger
case, with the LEDs and pushbutton
mounted on the lid in a more conventional manner.
Building on Veroboard also allowed
a fast development time to be achieved
on the hardware. If you use Veroboard,
be very careful to support the board
while cutting the hole for the pushbutton switch, otherwise, the board will
snap in half (been there, done that . .
.). File the edges if the board is slightly
too large (you will probably have to
file the corners round, too).
No component overlay is shown for
the Veroboard version but the photographs will give a very good idea of
component placement. Some of the
components are mounted on the copper (strip) side of the board.
Take care when cutting the Vero
board tracks that the cut is complete
and no copper swarf shorts to an
adjacent track.
The easiest way to cut Veroboard
tracks is to take a twist drill bit about
5mm or so and simply twist it in the
hole to be cut with your fingers. If
the drill is sharp it results in a clean,
quick hole.
You may like to install a small piece
of clear Perspex sheeting over the top
of the project to protect it from small
which incidentally, is where those 265
other dice went.
Have fun. And remember, unless
you create some code to allow you to
cheat, it is very hard to force the dice
to roll a particular way.
Remember also that the one disadvantage of this project over the real
dice is that it isn’t built to survive
20G’s of deceleration, so throwing it
would be bad.
Troubleshooting
There wasn’t room for a battery hold
er: a piece of bubble-wrap held the
batteries in place and stopped any
possibility of shorts.
prying fingers. This can be mounted
on 12mm brass standoffs on the top of
the Veroboard, with a suitable hole for
the pushbutton.
Testing
Examine the PC board or Veroboard
to ensure there were no shorts created
during assembly and then install the
batteries. Note that the PIC microcontroller is NOT installed yet. Verify that
+5.4V is present on pins 4 & 14 (with
respect to pin 5 [GND]).
Finally, disconnect the batteries,
install the pre-programmed PIC
(16F84) and re-install the batteries.
(Don’t insert the PIC with the power
applied!). You should be rewarded
with a self-test pattern.
Verify that the unit operates when
the button is pressed as described earlier in this article. When you release
the button, the display should ‘slow
down’ and then display the result
for approximately 20 seconds before
turning itself off. Quickly pushing and
releasing the pushbutton should recall
the last roll.
If the unit operates correctly, carefully mount the PC board in the top
of the zippy box.
All that remains now is to instruct
the kids on how to operate it and to
chain it to the table so that it doesn’t
end up at the bottom of the toy box,
If for some reason the project fails
to work, check all soldering carefully.
Verify that all the LEDs have been
installed correctly. You can check the
hardware by removing the PIC and
placing a 10Ω resistor between pin 14
(VCC) and each of the LED drive lines
(pins 1, 2, 10, 11, 12, 13, 17 and 18)
one at a time. The LEDs should light.
You can verify that the pushbutton
switch operates correctly by monitoring pin 6 with a logic probe, or
multimeter while pushing the button.
It should go to +5.4V when the button
is down.
Finally, if you have a CRO, you can
verify that the internal PIC oscillator
is running by examining pin 15 (CLK
OUT). This pin is not used by our
circuit but from it you should see a
1MHz square wave for three seconds
after the device is powered up and for
20 seconds after the button is pressed.
Remember that the device spends most
of its time in sleep mode, with the CPU
clock turned off to conserve power.
If all of the hardware checks out,
you should try re-programming the
PIC. Perhaps it has the wrong code
installed.
Good Luck. And remember that this
SC
is supposed to be fun!
Want to know more?
As mentioned in the text, source
code for the PIC microcontroller
and other information is available
for those interested in this project.
You can log in direct to:
www.dougzone.com
or you can access it via the SILICON CHIP website,
www.siliconchip.com.au
and follow the link from the selection bar on the left side of the
opening page.
May 2000 63
|