Build
your 40 MHz Frequency meter!
By Ibrahim
Kamal
Last update:
9/5/08
This article shows how to build a small, cheap
and simple frequency meter, without any fancy, out of reach
components. The simple proposed design can measure frequencies
up to 40 Mhz with errors below 1%! This degree of precision
will be more than enough to debug most of your analog and
digital circuits, and will give you the ability to analyze
many aspects that you were unable to see before.
The frequency meter is build on a veroboard, using only 3
components and 8 resistors. It is designed to be plugged into
any standard bread-board. |
|
1.The
hardware
 |
figure 1.a |
As you can see in figure 1.a, the design
is built on a ATMEGA16 micro controller. There are no crystal
oscillators, as we are using the internal RC oscillator, calibrated
to 8 MHz.
The frequency measurement probe is connected to W1 terminal, which
is then fed to the pin PB0 and the clock input of the 74191 4-bit
counter. The 4-bit counter will be used to divide the measured
frequency by 16 before feeding it to the microcontroller. As you
can see, all the features of the counter aren't used, only the
Q3 output is used, whose frequency will always be equal to the
input frequency divided by 16.
The
7 segments display used is composed of 4 cells all integrated
into one package. This reduces the number of wires to
enable the different cells.
The display we used is common anode type,
and had its leads arranged according to the diagram in
figure 1.b, and most of the 7 segment cells of this type
will follow those standard connections. However, if you
use different type of 7 segment display, the software
can be very easily adapted to accommodate the changes.
The pins E1 to E4 lets you enable one of the 4 cells,
E1 enables the right most one.
Since we are using an ATMEGA16 microcontroller that can
source up to 40mA of current per I/O pin, we don't need
transistors, so the 4 enable signals of the 7 segment
display is directly connected to the microcontroller.
The wire connection W2 is to connect to ground, to allow
measurement of frequencies on devices that do not share
the same power supply as the frequency meter itself. (Remark,
for one circuit to be able to measure an signal on another
circuit, they must share the same ground voltage, se we
connect them together.) |
figure 1.b |
Finally, J1 is a connection for the ISP programmer
(In System Programmer). In need, after you finish the project
you will spend 10 minutes doing nothing by calibrating your frequency
meter, and adjusting some variables to make the display clearer
or to reduce the flickering of the numbers being displayed. That's
why we added this ISP connector, because we will need to update
the code of the microcontroller often.
The whole is assembled on a veroboard, and, as you can see in
figure 1.c, it is designed to be plugged directly into the power
supply rails of a standard bread board. You can also notice that
there is a protection diode added (though it is not present in
the schematic), it prevents any damage to occur in case the frequency
meter is plugged in the wrong direction.
figure 1.c |
Next figure (1.d) shows the two probes that are connected to W1
and W2 (see the schematic in figure 1.a). The ground probe is
only used when the power supply of the frequency meter is different
from the power supply of the device being tested, to connect both
grounds together. the frequency measurement probe is made of a
single pin header, covered with a piece of "Heat shrink tube"
(or also called thermo-retractable tube).
figure 1.d |
2.
Frequency measurement algorithm
 |
We all know - at least most of the visitors that
made it to that website! - that "Frequency is a measure
of the number of occurrences of a repeating event per unit time".
but measuring frequencies with digital tools such as this microcontroller
that have its limitations involve some further studies to achieve
the required results.
The maximum frequency that can be sampled by one of the counters
of the ATMEGA16 cannot exceed the CPU clock divided by 2.5. Let's
call this frequency F_max. So, assuming the ATMEGA16's CPU is
clocked at 8 MHz, we can directly measure frequencies up to 3.2
Mhz. Frequencies above that limit will be measured as 3.2 MHz
or less, since not all the pulses will be sampled. To be able
to measure frequencies above F_max, we use a 4 bit counter as
a frequency divider, dividing the measured frequency by 16. This
way we can also measure frequencies up to 16 times F_max, but
due to the limitation of the 74191 counter, the actual maximum
measurable frequency wont exceed 40 MHz.
The algorithm that we developed measures both the original frequency
(let's call it F_o) and divided frequency (F_d). in normal conditions,
when the frequency is below F_max, the following relation is true:
But as F_o approaches to F_max, more and more pulses wont be sampled,
and the the relation above will obviously become:
And hence the limit of the microcontroller can be automatically
detected.
The frequency meter starts by selecting the original frequency
for processing and display, and as soon as it detects that it
reaches F_max (using the method described above), the divided
frequency is selected instead.
This algorithm can be summarized in the following flow chart (figure
2.a)
figure 1.a |
3.The
software
 |
The C source code for this frequency meter can be downloaded here.
The source code is detailed with as much comments as I could,
but you may need some more explanations to understand the code:
The code is made
such that the number being displayed is in KHz. For example, if
you see on the 7 segments the number "325.8" that means
325.8 KHz, "3983" would mean 3983 KHz (or 3.983 MHz).
If the number is in tenth of megahertz, it is displayed with an
"m" at the end like "22.3m" meaning 22.3 MHz.
Timer/Counter 0
is used to count incoming pulses directly
Timer/Counter 1
is used to count incoming pulses divided by 16
Timer/Counter 2
is configured as timer with a 1024 prescaller (counting CPU frequency
divided by 1024). It is used to call the "frequency calculation
and selection algorithm" every timer period T. T is defined
as "1024*256/(F_cpu)".
The constant "factor"
defined in the top of the program as "31.78581" have
to be calibrated by measuring a known frequency. This factor was
initially calculated as the following:
| factor
= F_cpu / (1024*256) = 8.E6 / (1024*256) = 30.51757 |
But due to the fact that there is some delay between the time
the Timer_2 overflows and the time where the counted frequency
is actually processed, this factor need some calibration to reflect
more accurately the reality of the frequency being measured.
The Anti-flickering
algorithm is complicated but very effective, specially in measuring
frequencies that are unstable. It will totally prevent the display
from quickly switching between various values, while still showing
accurate values, and quickly change if the frequency being measured
"really" changes.
One last note, the
ATMEGA16 is chipped with a 1 MHz internal oscillator enabled.
To set the internal oscillator to 8Mhz, you have to use some programmer
(like our ISP programmer) to change the
fuse bits of the micro controller to adjust the internal oscillator's
frequency. (For 8MHz, the CKSEL3..0 fuses have to be set to '0100').
Download
the zip file for the project.
containing the C code and complied HEX
file. |
I hope this article was useful. Any comments and further questions
are welcome in the forum below.
Preview of the last 15
messages discussing this page. Messages are sorted from the newest to
the oldest. |
Posted
by:
ikalogic
on:
27 May 2008 |
Re: 40 MHz frequency meter |
|
 |
Quoting mohamad: hi Khizer Naeem i think that you could get answer from this prog. but i can not , i get about 25 error when i want to run code.can i have your code and setting of parameter of codevision please and how i can chenge the program if i want use a LCD for out put instead of 7segment thanks |
One thing at a time, first you have to realize that the code is written for winAVR equipped IDEs. CodeVision is independent apparently differnet Maybe the only thing you need to change is the location of the header files.
WinAvr is used in may IDEs like VMLAB (which we are using, and is freeware), or also AVRStudio from ATMEL.. Why not try to use VMLAB.. ? it is very powerful..
Check the parts [1] and [2] of this article: http://www.ikalogic.com/tut_8051_to_avr.php
|
|
|
|
 |
Posted
by:
mohamad
on:
26 May 2008 |
Re: 40 MHz frequency meter |
|
 |
hi Khizer Naeem i think that you could get answer from this prog. but i can not , i get about 25 error when i want to run code.can i have your code and setting of parameter of codevision please and how i can chenge the program if i want use a LCD for out put instead of 7segment thanks
|
|
|
|
Posted
by:
hys8312
on:
11 May 2008 |
Re: 40 MHz frequency meter |
|
 |
Quoting ikalogic:
| Quoting hys8312: Is internal RS oscillator sufficiently accurate for this application? |
Well that's from where the 1% error comes from. For me it is acceptable, may you need more precision, use a crystal oscillator! |
From my understanding, 1% error range is not guaranteed unless calibrate the internal oscilator. Well its calibrated to 5V/25°C at factory, that's probably enough for laboratory application.
|
|
|
|
Posted
by:
ikalogic
on:
10 May 2008 |
Re: 40 MHz frequency meter |
|
 |
Quoting nura100: Hello IKA,
you have used and AVR, but can it be done using 8051 ?
Arun |
Yes and no.. with a 8051 you would need to cascade multiple 74191 to achieve 40 Mhz readings...
|
|
|
|
Posted
by:
boobka
on:
09 May 2008 |
|
|
Posted
by:
ikalogic
on:
09 May 2008 |
Re: 40 MHz frequency meter |
|
 |
| Quoting hys8312: Is internal RS oscillator sufficiently accurate for this application? |
Well that's from where the 1% error comes from. For me it is acceptable, may you need more precision, use a crystal oscillator!
|
|
|
|
Posted
by:
cry_wolf
on:
08 May 2008 |
40 MHz frequency meter |
|
 |
Nice work Ibrahim, It was no doubt an amazing effort. Thanks for sharing.
Regards Khizer Naeem
|
|
|
|
Posted
by:
ikalogic
on:
08 May 2008 |
40 MHz frequency meter |
|
 |
Be the first one to comment on this page! Feel free to to correct any scientific mistake or to give your opinion. Ask your questions and you can get an answer in the coming hours, as i am online most of the time!
See the 40Mhz frequency meter page: [link]
|
|
|
|
You have
to be a member to post replies. |
|
|
|