This is only a preview of the May 2001 issue of Silicon Chip. You can view 33 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. Articles in this series:
Items relevant to "Using Linux To Share An Internet Connection; Pt.1":
Articles in this series:
Items relevant to "Powerful 12V Mini Stereo Amplifier":
Items relevant to "Two White-LED Torches To Build":
Items relevant to "PowerPak: A Multi-Voltage Power Supply":
Purchase a printed copy of this issue for $10.00. |
This article presents two simple, l
One will count up or down and
user-defined ways from a preset
the modules is the softw
By Peter Crowcroft
M
odern electronics allows
products – consumer, industrial and scientific – to be
produced with more features in
smaller packages at less cost than
ever before.
Not too long ago, the controller
for an appliance such as a washing
machine or microwave oven would
have been a mechanical timer, or perhaps discrete components (switches,
transistors and 4000 series logic, etc).
However, all these things take precious space and are costly to produce.
Often they’re difficult to update or
reuse for different product models or
revisions.
Today, these problems are neatly
and cheaply solved with microcon-trollers – single chip computers
complete with IO pins, RAM, pProgram storage (ROM) and sometimes
other useful features like ADCs,
UARTS and PWM drivers.
One simply arranges for relevant
inputs (switches and sensors) and
outputs (motor and solenoid drivers,
LEDs and displays) to be connected
to the microcontroller and then write
some software to manage the lot.
The space saving and cost effectiveness of these small wonders are reason
enough to use them. But when you
consider the flexibility they provide
to adapt the control system to changes
in the device or consumer demanded
functionality they are indispensable.
Changes are simple: you change the
software (which can often be done
in-circuit) and the same hardware will
perform the new task.
There are very few fields left in electronic engineering where microcontrollers have not made their mark. It
is becoming more and more important
to understand how micro-controllers
work and how they are applied in
designs – and how to develop and
debug their software.
Fortunately, there are many sources
on the Internet open to the engineer
and hobbyist alike that provide free
tools, examples and designs. Microcontroller manufacturers have lots
TECHNICAL SPECIFICATIONS – Up/Down Counter
Supply voltage
Operating modes
Count range
Count rate
Inputs
Output
Display
Physical size
Connection
38 Silicon Chip
9-15V DC (<40mA <at> 12V)
Count Up (default), Count Down, Count Disable, Overflow, Reset
0000 to 9999 or 0000 to 0001 (0000, 9999, 9998, ... 0001)
Maximum count rate of 30 to 35 counts per second
Reset, Count (negative edge triggered), Count down
NPN Transistor,100mA <at> 30V
14mm red LED, 7-segment common anode
51mm x 63mm
10-pin SIL header pins, 0.1”
of details in their datasheets and
application notes, so that is a good
place to start.
The counter circuits
The use of an ATMEL AVR microcontroller allows the circuit to be
greatly simplified. A larger range of
useful features can be provided than
could be achieved with conventional
logic circuits.
If we wanted to make a simple
counter with conventional logic, we
would need some components to condition the input and output signals, a
counter for each digit (say a 74LS192
BCD Decade Counter), and then we
would need to drive a 7-segment LED
display using a BCD to 7-segment
driver (74LS47). Straight away we
have eight ICs (two per digit). Then
we’d need some “glue logic” to hang
everything together.
And we’d get a counter that can only
count up. To fit this into a reasonable
space we’d have to use a double-sided board with plated-through holes
because of the large number of connections required between ICs.
We might even need to go to surface
mount components to reduce the size.
It begins to get very expensive and
complex, not to mention tedious (if
not impossible) for the hobbyist to
assemble.
(Yes, some hobbyists work with surface mount components but they are
very much the exception to the rule!)
With the microcontroller solution
low-cost, four-digit counter modules.
d the other will count down in several
value. The main difference between
ware in the microcontroller.
and Frank Crivelli.
presented here, this complexity is
reduced to one IC only and a handful
of discrete components to condition
the input and output signals, all on a
small (cheap!) single-sided PC board.
All the hardware complexity has van-
ished into the software where finding
and fixing errors is easy.
As we shall see, we also get the ability to change and add more useful features and modes of operation easily.
The Up/Down Counter has an
overflow output, allowing multiple
units to be “daisy-chained” together
for greater counter range. The unit
will count between 0000 and 9999,
producing the overflow pulse when
the count rolls over to 0000.
MAY 2001 39
Table 1: Up/Down Counter Inputs and Outputs
Name Description
Reset
Reset the current value of the counter to 0000.
Clock
Increment (or decrement) the value of the counter. If the counter rolls over
to 0000, an overflow pulse is generated. The clock input is debounced
in software to prevent extraneous counts when mechanical switches are
used. This is achieved by ensuring a high to low or low to high transition
remains valid for more then 15ms. This means the maximum count rate
is around 30 counts per second. The count is triggered on a high-to-low
transition (falling edge)
Down
Controls the direction of the counter. When unconnected, the counter
will increment; when driven low (grounded) it will decrement.
Disable
When grounded, the counter will not count even if the clock input is
being pulsed.
Overflow
This is an open collector output. When the count rolls over to 0000,
it is pulled to ground by the circuit for approximately 25ms. This may
be connected to the Clock input of the next module to create a counter
with a larger range or used to drive a relay, indicator or other circuit.
The Presettable Down Counter
allows the user to program a starting
count and select one of four different
operating modes which determine
what happens when the count reaches
0000.
Circuit description
The modules are almost identical;
in fact the display driver, the power
supply and the output are identical.
The differences are confined to the
inputs and their “meaning” to the
microcontroller. Let’s start by looking
at the identical parts of the modules.
The counter modules are designed
around an AT90S1200 AVR microcontroller from ATMEL (http://www.
atmel.com). A detailed product data-sheet is available from this website.
This particular device was chosen
because it has an internal R/C oscillator, eliminating the need for an external crystal. This simplifies the circuit
and further reduces component costs.
The display unit is a 4-digit, common anode, multiplexed, 7-segment
LED display. This means that the
LEDs in a single digit share a common
anode (positive) connection. The
cathodes (negative) of the segments
(a, b, … g & dp) are connected across
the four digits, forming a matrix.
Multiplexing results in fewer connections and less board space being
devoted to the display and reduces
the number of microcontroller outputs required to drive the display.
One negative is that the drive signals
become more complex but this is
40 Silicon Chip
relatively simple to achieve in the
microcontroller’s program.
Bits 1 to 7 of the microcontroller’s
Port B are connected via 270Ω current limiting resistors (R1-R7) to the
shared segment pins. Four of the Port
D bits are then connected to drive the
four common anodes via Q1-Q4, the
PNP transistors. Resistors R8-R11
(4.7kΩ) protect the transistors from
excessive base current which otherwise could destroy them.
To display the current count, the
microcontroller cycles through each
of the four digits one at a time, providing current to the anode of the digit by
turning on the appropriate transistor
(driving the base low).
It then arranges for outputs connected to the segments it wishes to
light to be driven low so that current
can flow from the transistor, through
the LEDs in the display and to ground
via the microcontroller port. The
segments it wishes to remain unlit
are driven high.
After approximately 1ms, the display is extinguished and another 1ms
delay occurs, then the next digit is lit.
This then continues for the remaining
digits and the cycle starts again.
Therefore it takes about 8ms to fully
display the current count, which is
much too fast for the human eye to
discern, so to us it looks like a constant display.
The software programmed into
the microcontroller uses a timer that
triggers an interrupt about every 1ms
to achieve this. When the interrupt
occurs the next display is set up or the
current display is extinguished. This
allows it to be monitoring the inputs
without constantly worrying about
handling the display, simplifying the
design of the software.
Transistor Q5, an NPN device,
provides an active low open collector
output for the overflow signal in the
up/down counter version and the
output signal in the presettable down
counter version. The remaining bit
(Bit 0) of Port B drives this transistor
via R18, a 1kΩ resistor.
Q5 is protected by Zener diode Z1
which will break down and conduct if
the voltage across Q5 exceeds 33V, or
it will conduct if a negative voltage is
applied to the collector. This is need-
Table 2: Presettable Down Counter Inputs and Outputs
Name Description
Reset
Reset the current value of the counter to the preset value.
Count
Decrement the value of the counter. If the counter rolls over to 0000, the
current operating mode determines the output pulse and new count value.
For more information see “Using the Modules”. The count is triggered
on the high to low transition. Software debouncing is optionally applied
to the count signal using the Rate input. If it is enabled, it is identical to
the Up/Down counter.
Rate
Select if software debouncing is applied to the count input signal. If
high (by default), debouncing is applied; if driven low (grounded),
debouncing is not applied. This is useful if the count is derived from
another logic circuit that doesn’t exhibit extraneous pulses like a switch
can do. If debouncing is disabled, the count input can be clocked a lot
faster. Note that this input is not debounced at all as it is meant to be
set permanently.
Output
This is an open collector output. When the count rolls over to 0000, the
current operating mode determines what this output does.
ed when driving inductive loads such
as relays, as the back EMF generated
by the collapsing magnetic field in
the coil when the current is turned
off can easily exceed the rating of the
transistor and destroy it.
Power for the circuit is provided by
an external 9-15V DC power supply
and is regulated by IC2, C4 and C5,
resulting in a 5V supply.
IC2 looks like another transistor
but is a 78L05 low-current voltage
regulator in a TO-92 case. This regulator needs about 2.2V of headroom
(ie, voltage in minus voltage out) to
ensure regulation.
Diode D1 provides reverse bias
protection in case the power supply
is connected the wrong way around.
As there is about 0.6V or so drop
across this diode, you must ensure
that the voltage supplied to the circuit
doesn’t drop below about 8V (5V +
2.2V +0.6V ~ 8V)
Now let’s look at the input circuits
for the different modules.
Up/Down counter
The Up/Down counter has four
inputs and one output. These are
detailed in Table 1.
The four inputs are all pulled high
TECHNICAL SPECIFICATIONS – Presettable Down Counter
Supply voltage
12VDC <at> 50mA
Operating modes Count Stop, Output Hold Over-Count, Output Hold
Auto-Reset, One-Shot Output Over-Count, One-Shot Output
Count range
0000 to 9999 (10,000 max)
Count speed
Low (selectable)
High 30 cps (15mS high, 15mS low) 30,000 cps (measured)
Inputs Reset, Count, Rate
Output NPN Transistor, 100mA <at> 30V
Display 14mm red LED, 7-segment common anode
Physical size
51mm x 63mm
Connection
10 pin SIL header pins, 0.1”
by the 1kΩ resistors and have a low
pass filter formed by a 27kΩ resistor
and .001µF capacitor to filter out high
frequency noise from the line to reduce the chance of false triggers. This
filter’s time constant is approximately
20µs and any pulses shorter then this
won’t make it to the microcontroller.
A 20µs time constant equates to a frequency of 50kHz. The inputs are also
debounced in software with the level
in the input needing to be constant
for 15ms before it is recognised as a
valid input.
Presettable Down counter
The Presettable Down counter
is a little more complex. It has two
push-button switches added to its
inputs. These are used to program
the preset value and operating mode.
This module has three inputs and one
output, as detailed in Table 2.
Like the other module, the inputs
are pulled high by 1KΩ resistors. The
Count and Reset inputs have the same
low pass filtering applied with the
27KΩ resistors and .001µF capacitors.
The SET switch (SW2) is connected
directly to Port D, Bit 4 with a 1kΩ
pull-up resistor. There is no need for
filtering on this input as the microcontroller will debounce it in software.
The INC (SW1) switch is interesting
as it is shared with the Count input.
This is an example of making efficient
use of the available inputs. This can
be done because in set-up mode, no
MAY 2001 41
4-Digit Up-Down Counter
Parts List - Up-down
1 PC board, 51 x 63mm, code
K129
1 20 pin IC socket
1 set male and female 10 pin right
angled connectors
1 2-pin SIL header
Semiconductors
1 AT90S1200-12PC preprogrammed microcontroller, (IC1)
1 78L05 5V regulator (IC2)
4 BC557 PNP transistors (Q1-4)
1 BC547 NPN transistor, (Q5)
1 1N4004 power diode (D1)
1 33V 1W zener diode (Z1)
1 LN5644R 4 digit, common anode
LED display (DISP1-4)
Capacitors
1 .001µF ceramic (C1,2,3,6)
1 0.1µF monobloc (C4)
1 10µF 25V electrolytic (C5)
Resistors (0.25W, 5%)
4 27kΩ (R13,15,17,20)
4 4.7kΩ (R8-11)
5 1kΩ (R12,14,16,18,19)
7 270Ω (R1-7)
Parts List – Presettable
1 PC board, 51 x 63mm, code
K54
1 20 pin IC socket
1 set male and female 10 pin
right angled connectors
2 PC mount pushbutton
switches (SW1, SW2)
Here is the component overlay and and matching photograph of the 4-digit
Up-Down Counter, reproduced same size so you can see exactly where all of
the components go. Note the 270Ω resistor is mounted under the IC socket.
counting in done. This also means
that the INC button can be used to
decrement the counter when it is
running.
Software
The software listing for the microcontroller is not supplied, however
this description is provided for those
who are curious or want to have a go
at creating their own.
The first thing the code does is set
up all the inputs and outputs and
initialises all the internal states. It
then sets the count to the default value
(0000 or the preset depending on the
module) and starts the internal timer.
The timer is set to trigger an interrupt every 200µs (observant readers
may notice I said 1ms earlier – I lied
for simplicity).
When the interrupt occurs, the handler routine updates various internal
counters used for debouncing inputs,
4-Digit Presettable Down Counter
Semiconductors
1 AT90S1200-12PC preprogrammed microcontroller (IC1)
1 78L05 5V regulator (IC2)
4 BC557 PNP transistors (Q1-4)
1 BC547 NPN transistor, (Q5)
1 1N4004 power diode (D1)
1 33V 1W zener diode (Z1)
1 LN5644R 4-digit, common
anode LED display (DISP1-4)
Capacitors
2 .001µF ceramic (C1,2)
1 0.1µF monobloc (C4)
1 10µF 25V electrolytic (C3)
Resistors (0.25W, 5%)
3 27kΩ (R13,15,18)
4 4.7kΩ (R8-11)
4 1kΩ (R12,14,16,17)
7 270Ω (R1-7)
42 Silicon Chip
There’s not a lot of difference between the Presettable Down Counter and
the Up/Down Counter above . . . but there are differences! Follow this component overlay and photo and you shouldn’t have any problems.
Table 3: Presettable Down Counter Modes
Name
Description
Mode A
(Default)
Count Stop, Output Hold.
When the count reaches 0000, the output goes low and stays low.
The counter stops counting. The counter must be reset to continue
counting again and to reset the output. When reset the count is set to
the preset value.
Mode B
Over-Count, Output Hold.
When the count reaches 0000, the output goes low and stays low.
The count will wrap around to 9999 on the next count input and
continue counting from there. The output will remain low until the
module is reset.
Mode C
Auto-Reset, One-Shot Output.
When the count reaches 0000, the counter automatically resets itself
to the preset value and the output pulses goes low until the next
count pulse occurs.
Mode D
Over-Count, One Shot Output.
When the count reaches 0000, the output goes low until the next
count pulse occurs. The count will wrap around to 9999 and continue
output pulse timing and the display
timer routines.
If any of these counters reach zero
they need attention and are processed.
For example, every 1ms the display
routine is called to update the display.
The main loop constantly monitors
the inputs and sets up the debounce
counters when they change. If a valid
clock pulse is detected and the count
isn’t disabled, a routine to either
count up or down is called.
The count is stored as four binary
coded decimal (BCD) values, so constant conversion is not required in the
display driver routine.
This is updated by the count up or
down routines and if the value changes to 0000, the overflow output of the
counter is activated and a counter
set up to turn it off in about 25ms.
In the Presettable Down Counter, the
output is determined by the current
operating mode.
The display update interrupt routine uses a BCD-to-7-segment conversion routine to map the 0-9 value of
the digit being displayed to the correct
output for driving the segments in
the display.
The Presettable Down Counter also
has a set-up mode that is entered
when a high-to-low transition is detected on the Set input. This allows
the preset count value to be set one
digit at a time and the mode to be
selected.
Construction
Both kits include all components,
a high quality PC board and a preprogram-med microcontroller. All
you will need is a power supply and
a clock source.
Start construction by separating out
all the components into values, using
the parts list as a guide. I’d suggest
a fine conical tip on your soldering
iron, as there are some small, closely
spaced pads especially for the transistors. The PC board is very good quality
and has a solder mask so it isn’t too
difficult to avoid solder bridges.
Start by installing the resistors.
Pay particular attention to R4 as it is
situated under the socket for the microcontroller. You may want to leave
it until last and ensure the socket fits
over it before soldering it and the IC
socket in.
Next put in the capacitors, paying
attention to C5 as it is polarised and
laid over on its side. I’d suggest that
you bend the leads at a right angle first
and then insert it into the board and
solder it, to avoid having the legs too
short to bend over later.
Install the two diodes next, ensuring that the cathode (striped) end
matches the stripe on the PC board
overlay.
Now install the transistors and
IC2. Don’t get these confused, there
are four BC557s (Q1-Q4), one BC547
(Q5) and the 78L05 (IC2). Use the
outline on the PC board as a guide
for orientation.
Q1-Q4 and IC2 are close together
and close to the edge of the LED display so get them as low as possible
and as straight as you can so they
wont get in the way. Double check
that you don’t have any solder bridges
across the transistor pins as they are
close together.
If you’re building the Presettable Down Counter, install the two
switch-es. They will fit with the pins
coming out towards the display and
the connector.
Install the LED display; the decimal
points go towards the microcontroller. Then install the 2-pin header for
power (Up/Down Counter only) and
the 10-pin 90° header for the inputs
and outputs. The kit also includes a
socket for this header; this doesn’t
mount on the PC board but can be
used to make connections to the
completed module.
Carefully install the microcontroller into its socket (noting its polarity)
and assembly is finished. After checking your board, apply power and you
should see 0000 displayed (this is the
power-on default for both modules.
If you short the two count pins (or
press the Inc button on the Presettable
Down Counter) the display should
increment (or decrement).
If it doesn’t work
Poor soldering (dry joints) is the
most common cause of problems.
Check all your joints under a good
light; they should all be smooth and
shiny. Resolder any suspicious ones.
Keep an eye out for solder bridges
and for any pads that you may have
forgotten to solder as well.
Make sure that you inserted the
diodes the correct way and that the
microcontroller is also the correct
way around and securely sitting in
the socket. Also check the orientation
of electrolytic capacitor C5. Make
sure that you didn’t mix any of the
transistors up and that they are in
their correct places and the right
way around – including the voltage
regulator.
Use a multimeter to check the
supply voltage. Measure it from the
cathode (stripe end) of D1 and 0V. It
should be at least 8V or the 5V regulator will have difficulties and not
operate correctly. The voltage from
the output of the regulator should be
MAY 2001 43
Table 4: Resistor Colour Codes
No. Value
3 27kΩ
4 4.7kΩ
4 1kΩ
7 270Ω
4-Band Code (5%)
red violet orange gold
yellow violet red gold
brown black red gold
red violet brown gold
within a few tens of millivolts of 5V.
If it’s much lower, then you probably have the regulator in back-to-front
or something (such as a solder bridge
or misplaced component) is causing
too much current to be drawn from
the regulator, shutting it down. If
it’s much higher, check for a solder
bridge across the regulator pads (or
the regulator itself might be shot).
Using the modules
The counter module has three or
four inputs and one output that are
accessed via a 10-way header. The
input lines are all active low, which
means that grounding them performs
their function. More correctly, each
of the inputs is normally pulled high
by the module circuitry and must be
pulled low to become active.
Each of the lines has a corresponding ground pin beside it, simplifying
the connection to a switch. The input
lines may be connected to simple
‘make’ contacts, switches, relays or
even open collector outputs from
other circuits.
The module requires a 9 to 15V DC
power supply and consumes between
20mA and 40mA, depending on the
number being displayed. A small
plugpack will easily supply enough
power for several modules. Alternatively, the module could be battery
powered.
The Up/Down Counter is fairly
straightforward. Just connect a switch
to the count input and set the direction on the Down input and you’re
ready to go. However, the Presettable Down Counter, is a little more
complex.
Connect the count input and output
as needed, and then apply power to
the unit. By default, it will display
0000. It will overflow to 9999 and
continue counting down with clock
inputs until it reaches 0000 again.
This is Mode A and it is the default
mode. A description of each of the
modes is given in Table 3.
The two pushbuttons marked, SET
and INC are used to configure both the
44 Silicon Chip
preset value and the operating mode.
The preset value is entered one digit
at a time starting at the thousands and
then the Mode is selected.
To enter the programming mode,
press the SET button. The display
will show the preset value for the
thousands digit and the rest of the
display shows a minus (-) sign. Use
the INC button to select the required
value then press the SET button to
advance to the next digit.
Continue setting each of the preset
digit values unit the last one is set.
The display will now show the current operating mode with the letters
A, b, C or d. Use the INC button to
select the desired mode and press
the SET button to accept it. This will
also exit programming mode and the
counter is ready for use.
Software flexibility
To illustrate the power of using a
microcontroller versus discrete logic
circuit the following “user requested”
modifications have been made to the
Up/Down counter at no cost to the
user since the change was very easy
to do in software (note these changes
are not included in the kit software –
they are mentioned only to illustrate
the ease of change).
1. Count by five instead of by one.
2. Show digits “upside down” so
the PC board could be placed in a
pre-designed box upside down.
3. Only display digits on a “keypress” so that the kit could be more
efficiently battery powered.
These were done by simply changing the software. Try doing that with
discrete logic circuits!!!
Further information
The following may be good starting
points to find more information:
• ATMEL (makers of the microcontroller used in this project) have
a website at www.atmel.com There
you will find data-sheets for all their
micro-controllers with detailed information about using and programming
them.
• DIY Electronics (the kit manufacturer for this project) have a website
at http://kitsrus.com
They also have an AVR Programmer kit (Kit 122) and BASCOM Basic
Compiler which are useful for people
wishing to experiment with AVR micro-controllers.
Questions or comment about the
PROGRAMMING THE
DOWN COUNTER
Two pushbutton switches, marked
“SET” and “INC”, are used to preset
the starting count and select the
operating mode. Presetting the count
value is done one digit at a time,
starting with the thousands digit.
Press the SET button to enter
programming mode. The display
shows the current preset value of
the thousands digit and the rest of
the display shows minus (–) signs.
Use the INC button to set the value
required. Press the SET button when
done.
The current preset hundreds digit
is shown. Use the INC button to set
the value required. Press the SET
button when done.
Repeat the above steps for the
tens and units digits.
After setting the units digit the
display shows the current operating
mode. The mode is indicated by the
letters “A, b, C or d”. Use the INC
button to set the operating mode then
press SET to exit programming mode.
The display will blank momentarily
to indicate that programming mode
has ended.
The counter is now ready for use.
As mentioned before the RESET
input resets the counter to its preset
value. It does not change the operating mode. If the counter loses power
it will restart in Mode A with a preset
value of “0000” (count = 10,000).
Kit can be directed to Peter Crowcroft,
peter<at>kitsrus.com, while technical
questions may be directed to the
kit’s designer, Frank Crivelli, frank<at>
ozi-tronics.com
Kit availability
Copyright of the kit designs, the
PC board patterns and the software
(residing in the microcontroller) is
retained by DIY Electronics (HK) Ltd.
A kit of parts for either of these
kits may be obtained from Jaycar
Electronics stores, Jaycar mail order
or via their online store at www.jaycar.com.au
Both kits sell for $39.95.
The 4-Digit Up/Down Counter is
Cat No KD-6084, while the 4-Digit
Presettable Down Counter is Cat No
SC
KD-6058.
|