Converting a Clock to a Frequency Counter

I
thought converting a Maplin LED clock kitset to a frequency counter
would be simple, fast and tidy. The final outcome was anything but
that.
Introduction
Each
time I travel outside Saudi Arabia where I live and work these days, I
try to visit a few electronics component shops. I buy parts for
upcoming projects, and I often get more ideas for new projects as I
browse the shelves.
On a visit to the UK, I visited one of the Maplin stores. Depending on
the store, they can be great places to browse and buy, or they can be a
total waste of time with only a few odds and ends to be found in an
overstocked pile of consumer chaff.
As
I walked around this time, buying a few things I needed (or thought I
might need), I came across a 4-digit LED alarm clock kit. An assembled
clock board is shown at the top of the page. The instant I saw that the
kit used an Atmel ATtiny2313 microcontroller, it sparked an idea.
At the time, I was building a new transceiver, testing some
bidirectional amplifier stages. I had just completed a
permeability-tuned VFO for it. (It’s described on another page on this
website) I have written some frequency counter software in the past for
this chip, and I thought I might be able to quickly convert the clock
kit into a compact 4-digit LED frequency counter and reuse that old
code.
Just how hard could it be? Probably take just a few hours. Whip it all
up in a jiffy. This documents the result. Oh, it all worked, no problem
there. It's just....Well, you'll see.
Multiplexing with LED Displays
Displaying
numbers on a multi-digit 7-segment LED display is usually done using
multiplexing. This method reduces the number of pins required on the
microprocessor. To do this, all of the same segments in each LED digit
are connected to a common output line on the microprocessor. Then, each
LED digit is turned on separately and sequentially. If this
process is done quickly enough, it appears as if all of the display
digits are individually driven.
In this
case, this reduces the required pin count from 32 (to select 7 segments
plus one decimal point independently per digit x 4 LED digits) to just
12 (to select 7 segments plus one decimal point connected in parallel
plus 4 LED digit selection lines). Perhaps the diagram below will make
this a bit clearer. (Another possible approach which reduces the
required pin count even further is “charlieplexing” but it’s a little
more complex)
In almost every LED counter and clock design you can find, the eight
segment lines (7 for the digit plus one for the decimal point in each
digit) are driven from pins on a single 8-bit port on the
microprocessor. My earlier software was written for this logical
approach to segment connections. However, as I discovered when I opened
the kit and explored this design in more detail, the Maplin clock was
arranged completely differently! In this design, the display segments
were each connected to random ports and pins scattered around the
ATtiny2313. This was probably done to simplify the PCB layout but it
makes the software significantly more complex.
Here’s the circuit diagram of the Maplin 4-digit frequency counter.
Incidentally, the circuit diagram was not included in the kit
instructions, unfortunately, and I couldn’t find a circuit diagram for
this counter online either. I ended up drawing the schematic shown here
based on my analysis of the kit. Of course, a week or so later, I
stumbled across the circuit diagram online. Anyway, here is my version:
Figure 1 : Schematic of Maplin LED clock kitset
Aside from the strange method used to connect the LED segment
connections across two different ports on the microprocessor, the other
thing I noticed was that the LED display was not a generic 4-digit
display. Instead, it was one specifically designed for clocks. Well,
that’s hardly any surprise, I suppose, but it did present an issue for
me. I had assumed each digit would have its own decimal point. That
increases the flexibility of the frequency display format. Instead, the
options were immediately reduced because the display only had a single
inter-digit colon in the centre of the display. Sigh.
Constructing the Clock Kit
The
next step was to build the kit. This was the easiest way to check that
all the parts worked. It was a very easy kit to build, as expected,
with a very easy to follow PCB layout. When I powered it up, everything
worked just fine.
And then, of course, I started to pull it apart to start the modifications.
Hardware Modifications
Here is
the schematic of the frequency counter. If you compare this with the
clock circuit diagram above, you can see the various modifications
required.
Figure 2 : Schematic shows the revised design and changes required to implement the frequency counter
These changes included:
- Several tracks needed to be cut to allow reassignment of some pins. These are shown marked "X" on the schematic above.
- 4n7 capacitor was added on the frequency input for DC isolation.
- R7
(100 ohms) was removed. This was connected to the display's inter-digit
colon but was removed to free up the pin for the kHz/MHz display switch.
- S2 and S2 pushbuttons are not required and are not fitted.
- In
the final version, I also added a single transistor amplifier to allow
the counter to detect the low RF voltage coming from the VFO (See
below)
- The alarm alert LED is rewired directly to a port pin. This LED was coloured green in the photo from Maplin's catalog, but turned out to be a red LED in my kit.
- Alarm clock piezo sounder was not required and was removed. A jumper wire was fitted in its place.
- New
20MHz crystal fitted – The original 3.64 MHz crystal limited the
frequency counter’s range to about 1.5MHz, too low for most
applications. The new crystal increases this to about 8.5MHz. (If a
higher frequency range is required, you'll need to add another divider,
such as a 74HC4040 or 74HC74, and make some changes to the software.
These devices will extend the counter's limit to 30MHz, or to as much
as 90MHz, depending on the IC used and its manufacturer.
- A 30pF trimmer capacitor replaced one of the fixed capacitors on the crystal to allow accurate alignment of the counter.

The RF
input required is relatively high, at least 2.5Vpp. My VFO had a much
lower level than this, so I added a small single transistor
preamplifier. The schematic is shown opposite. The components were
mounted under the board.
I also
moved the 5V regulator to the underside of the board. This will allow
me to bolt it to a solid heatsink (the chassis of the transceiver) to
keep it cool during operation. The counter draws 70mA so there is a
fair amount of heat given off during operation.
Given the number of modifications required to this clock kit, I’m
certain I would have been better off just building a counter from
scratch. However, you don't know that at the start of a project. In any
case, by documenting this design here and providing the software, maybe
someone else will find some part of it useful.
Software
I had
intended to just reuse some code I’d previously written. Like the
hardware, it turned out to require more changes than I'd expected, largely due to those strange segment connections. In the end, I was able to reuse some software written by others, and add some new code myself.

As
usual, the source code is provided below, just in case you want to add
new features or change something. The Hex file (the output file from
the Atmel compiler, and the file you need if you are just going to
program your ATtiny2313 directly) is also available for download at the
bottom of this webpage.
Aside from simply counting the frequency, the software also supports a
Push-to-talk (PTT) input to detect if the transceiver is in receive or
transmit mode. In some cases, the VFO frequency is offset, usually by
the IF frequency, in just one of these modes and this must be factored
into the calculations before the frequency is displayed. In my case, I
didn’t have an Tx/Rx VFO offset so the software simply reads the PTT
input and reflects its state on the board’s large green LED.
For my transceiver, I also had to allow for an IF offset of 8.6436 MHz.
My VFO is being used to tune across the 20m amateur band (14.0 to 14.5
MHz). Because my VFO produces an output 8.6436 MHz below
the transceiver’s operating frequency, the counter software must add
this IF offset to the frequency it measures so it can display the
correct frequency.
Let’s give an example. If the VFO frequency measured by the counter is
5.4434 MHz then the counter software must then ADD the IF offset
(8.6436 MHz) to this value. In this case, the display will then read
14.0870 MHz which is where the transceiver is actually tuned. Actually,
the four digits on this counter (in the usual “kHz” mode) will display
the last four digits of this frequency i.e. 0870, and in the
alternate “MHz” display mode, the LEDs will display 1408.
A small preamp was added due
to the low RF level from my VFO
(Schematic above)
Two
interrupt routines are operating in the background. These use the two
ATtiny2313 internal counter/timers. Counter/Timer 1 (C/T1) is
configured as a counter, and counts the input frequency. It is
incremented each time a falling edge is detected on the frequency
input. Counter/Timer 0 is configured as a 100mS timer. This starts and
stops C/T1, and the same routine clears the counted value for the next
count. This timer limits counter resolution to 10Hz, but that’s ten
times the resolution of the display, so that’s more than enough.
One of the existing momentary pushbuttons is used to select the
“MHz/kHz” display format. The display format is normally (and at
power-up) set to XXX.X, the last digit being the 'hundreds of Hz'
digit. If the MHz/kHz button is pressed, the display format changes to
XX.XX where the last digit is the 'tens of kHz' digit. Of course, since
we have no digit-specific decimal points with this kit’s display, you
just have to imagine those on the display. Pressing the button again
will revert to the original display format.
Programming the ATtiny2313
I had
made yet another assumption that the clock software programmed on the
ATtiny2313 could be simply erased and the chip reprogrammed with my new
software. No, this kit was going to fight me every step of the way –
The chip had its programming interface disabled when the clock software
was programmed, to prevent copying of the code. That was annoying.
However, if you’ve done any significant programming of AVR chips,
you’ll have already have encountered similar problems, especially if
you program the fuses in the processor to allow the reset pin to be
used as an extra I/O pin, or if you’ve accidentally select the
incorrect clock settings. The microprocessor just acts like a brick and
won’t accept any new program. Just like this chip was doing.
Fortunately, the solution was sitting right on my bench. I built this Fusebit Doctor
some time ago for several other projects where I was using the reset
pin for I/O. It took me just seconds to unbrick the ATtiny2313. A few
seconds more and the ATtiny2313 was programmed with the new frequency
counter software.
Perfect.
Prototype Construction
The
photo shown here says it all. It’s rough, and hardly pretty, but that
was all I needed to get it going. The two short wires hanging off the
board are for temporary connection to clip leads from my bench power
supply. There was a DC power socket supplied in the kit, but I didn't
bother to install it.

If I
end up using this counter in the final version of my transceiver, the
board will be directly wired to the transceiver power supply rail, and
the counter will be mounted deep inside the chassis. That will ensure
that all of this ugly stuff will be completely out of sight.
That's probably just as well.
Underside of the modified Maplin clock PCB shows the extent of modifications required.
Downloads
Software: Four_Count.zip contains both Hex and source code (*.asm) files
Credits
Thanks must go to Andy Palm N1KSN whose software, originally used in his 80m receiver,
lies at the heart of this counter. Significant portions of the original
software have been changed, with some unnecessary code for this
application removed and other new code added by me. Don’t blame Andy
for this code not working to your expectations. Without any doubt,
that’ll be my fault…
And credit must also go to Maplin and to
Middlesex University Teaching Resources in the UK for the original
clock and kitset design, now decidedly hacked about by my efforts here.
For that, my apologies.
Want to go back to the main page? Click
here to
return directly.