Frequency Counter Gen2
667 words | Posted on July 24th, 2011
Scott was 25.83 years old when he wrote this!
Filed under: C/C++, Circuitry, General, Microcontrollers, Python, Radio
I’m working to further simplify my frequency counter design. This one is simpler than my previous design both in hardware and software! Here’s a video to demonstrate the device in its current state:
I utilize the ATMega48’s hardware counter which is synchronous with the system clock, so it can only measure frequency less than half of its clock speed. I solve this issue by dividing the input frequency by 8 and clocking the chip at 12mhz. This allows me to measure frequencies up to about 48MHz, but can be easily adapted to measure over 700MHz (really?) by dividing the input by 128. Division occurs by a 74HC590 8-bit counter (not a 74HC595 as I accidentally said in the video, which is actually a common shift register), allowing easy selection of input divided by 1, 2, 4, 8, 16, 32, 64, or 128. The following image shows the o-scope showing the original signal (bottom) and the divided-by-8 result (top)

The device outputs graphically to a LCD simply enough. That LCD is from eBay and is only $3.88 shipped! I’m considering buying a big box of them and implementing them in many more of my projects. They’re convenient and sure do look nice!

The signal I test with comes from an oscillator I built several months ago. It’s actually a SA612 style receiver whose oscillator is tapped, amplified, and output through a wire. It’s tunable over almost all of 40m with a varactor diode configuration. It was the start of a transceiver, but I got so much good use out of it as a function generator that I decided to leave it like it is!

THIS IS HOW THE PROGRAM WORKS: I don’t supply a schematic because it’s simple as could be. Divide the input frequency to something relatively slow, <1MHz at least. Configure the 16-bit counter to accept an external pin as the counter source (not a prescaled clock, as I often use in other applications). Then set the timer value to 0, _delay_ms() a certainly amount of time (1/10th second), and read the counter value. Multiply it by 10 to account for the 1/10th second, then multiply it by 8 to account for the divider, and it’s done! It will update 10 times a second, with a resolution down to 10*8 = 80 Hz. It’s well within the range of amateur radio uses! If you’re considering replicating this, read up on how to use hardware counters with ATMEL AVR microcontrollers. That should be enough to get you started! Here’s the code I used…
For the LCD, this code requires LCD library.
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"
#include "lcd.c"
int main(void)
{
TCCR1B=0b00000111; // rising edge trigger
char buffer[8];
long toshow=0;
char mhz=0;
int khz=0;
int hz=0;
lcd_init(LCD_DISP_ON);
for(;;){
lcd_clrscr();
lcd_gotoxy(0,0);
itoa(mhz , buffer, 10);
lcd_puts(buffer);
lcd_puts(".");
if (khz<100){lcd_puts("0");}
itoa(khz , buffer, 10);
lcd_puts(buffer);
itoa(hz/100 , buffer, 10);
lcd_puts(buffer);
lcd_puts(" MHz");
TCNT1=0;
_delay_ms(99);
_delay_us(312);
toshow=TCNT1;
toshow=(long)toshow*16*10; // tenth second period
mhz=toshow/1000000;
toshow=toshow-mhz*1000000;
khz=toshow/1000;
toshow=toshow-khz*1000;
hz=toshow;
}
}
This entry was posted on Sunday, July 24th, 2011 at 5:07 pmand is filed under C/C++, Circuitry, General, Microcontrollers, Python, Radio. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
6 Responses to “Frequency Counter Gen2”
| Anon wrote the following at 08:30:14 PM on July 24th, 2011 |
|
In the video you said you used a 74hc595 8 bit counter, I thought that the 74hc595 was a 8 bit shift register, could you explain how that is used as a counter? |
| Scott wrote the following at 09:38:39 PM on July 24th, 2011 |
|
You’re right Anon, I meant to say a 74HC590, an 8-bit binary counter! I use 74hc595s all the time so it must have just slipped out… |
| Anon wrote the following at 10:03:45 PM on July 26th, 2011 |
|
I figured, for a second I was thinking wow, is there some way you can use 74hc595’s as a counter? heh. Have you considered adding in a AD8307 to measure signal strength as well? |
| David S wrote the following at 12:19:01 AM on August 3rd, 2011 |
|
Scott,great projects, love the idea of simple builds using common parts and substitutions. Would you please consider releasing the full schematics for Frequency counter 1 and Gen2. I’m trying to use arduino to determine what frequencies my vhf super-regen is receiving. You have been blessed with talent and the drive to attempt things not been tried before, keep it up, great job. |
| Tony wrote the following at 03:36:17 AM on February 27th, 2012 |
|
I am trying to build one of these to measure from 10Hz to 30kHz. I have a Atmega168 that I’m using to do other things. It has an LCD connected to it. I am an analog design engineer trying to dabble in digital. The problem I have it that it seems the timer port I need is being used by my LCD. I’m using the LCD and LCD setup that comes with a NerdKit. Are you interested in re-writing your code for me in C+ for my micro for a fee? Please let me know. |
| Alex wrote the following at 12:50:54 PM on May 17th, 2012 |
|
Maybe I’m missing something simple, but the 74HC590 can only count up to 61MHz according to the datasheet. How do you manage to get it up to 700 MHz? |
