Frequency counter circuit

Simple Frequency Counter

You may have already seen various projects over many websites named Frequency counter, Digital Frequency Counter etc. I’m posting just another of them. Showing the use of timer/counter of AVR micro controller (Atmega8) in one of it’s form. This circuit can be used as a simple micro controller project for your engineering courses. Frequency of a periodic signal is the number of cycles it repeats per second!   So If we count the number of cycles recorded in a second it will directly read the frequency. So what we are going to make is a frequency counter circuit, which can also be called as a frequency meter. 

To make this frequency meter 1) we need a signal (whose frequency has to be counted) 2) Atmega8 micro controller from Avr 3) An LCD to display the counted frequency. I assume that you are familiar with Avr Atmega8 and you know how to program it. You also need to know – How to interface LCD with Avr

Now let’s get into the details of the project – Simple Frequency Counter or otherwise Frequency Meter!

Take a look at the circuit diagram given below and also skim through the program given towards the end of this article.

Frequency counter circuit

Frequency counter schematic circuit diagram

Description of circuit:-

            So what I have done here is; Set the counter to zero, waited for 1S, and read  the counter again. But remember,you need to read the value immediately after the delay loop ends. It is simple. Just assign a variable and copy the count to that. The data type of the variable is essentially an unsigned integer. You can try floating point data type too! But here you need to typecast it! That’s all!  To read about floating point conversion in Avr – read this article carefully – String Formatting of Avr

                And yes! It’s better that you apply a conditioned signal for counting the frequency. i.e. a square wave or a trail of pulses. You may use a suitable signal conditioning circuit like Comparator;   Schmitt trigger, sine wave to square wave converter, whatever suits you. If the signal is of low power, then use a conditioning circuit . You can get lots of signal conditioning circuits in this website – check here - Signal Conditioner Circuits

Now here is the Technical details of my project. I hope you’ll have not much problem to make this.

The program [Embedded C, AVR Studio]:

#define F_CPU 1000000
#include
#define SMP 1

int main(void)
{ unsigned int i;
stdout=&lcd_str;
initLCD();

_delay_ms(50);

while(1)
{ TCNT1 =0;
_delay_ms(1000/SMP);
i=TCNT1;
LCDcmd(0x01);
printf("Freq:%uHz",i*SMP);
_delay_ms(500);
}
return 0;
}

The internal code of the Header file “ATmega8LCDcfg1.h”

Note: the Location of the Headr file is: “C:\Program Files \Atmel\AVR Tools\AVR Toolchain\avr\include\user”

#ifdef _LCD_CFG_H
#warning "LCD configuration file already loaded. ATmega8LCDcfg.h inclusion skipped."
#endif

#ifndef _LCD_CFG_H
#define _LCD_CFG_H 1

#ifndef _AVR_IO_H_
#include
#endif

#ifndef _UTIL_DELAY_H_
#include
#endif

#ifndef _STDIO_H_
#include
#endif

#define DEL1 10
#define DEL2 40

#ifndef DPORT
#define DPORT PORTB
#endif

#ifndef CPORT
#define CPORT PORTD
#endif

#ifndef DPDDR
#define DPDDR DDRB
#endif

#ifndef CPDDR
#define CPDDR DDRD
#endif

#ifndef RS
#define RS PD6
#endif

#ifndef EN
#define EN PD7
#endif

int LCD(char ch, FILE *fp);
// Function To send a single char. to LCD

void LCDcmd(char ch);
void initLCD();

static FILE lcd_str= FDEV_SETUP_STREAM(LCD,NULL,_FDEV_SETUP_WRITE);
/*
// Above: A stream is set up to give a formatted output
// to LCD using a macro FDEV_SETUP_STREAM(). The stream
// includes the function 'LCD()' that describes how a
// single character is sent to LCD. But the function
// prototype of 'LCD()' must be as per demand by the macro
// 'FDEV_SETUP_STREAM()'. See the prototype defined. This
// prototype is fixed. If this prototype differs. The program
// will not get complied.
// In the argument Field of 'FDEV_SETUP_STREAM(A,B,C)'. First
// field(A) holds the name of the function/routine that is to be
// used for sending a single character. Next field defines
// function to receive characters. The next one defines if
// the stream to be set up can be used for reading characters,
// writing characters or both.
// to know more, visit:
*/

void LCDcmd(char ch)
{ DPORT = ch;
CPORT = (1< _delay_us(DEL1);
CPORT = (0< _delay_us(DEL2);
if(ch==0x01||ch==0x02)
_delay_ms(10);
}

int LCD(char ch, FILE *fp)
{ DPORT =ch;
CPORT = (1< _delay_us(DEL1);
CPORT = (0< _delay_us(DEL2);
return 0;
}

void initLCD()
{ CPDDR = (1< DPDDR = 0xff;
LCDcmd(0x38);
LCDcmd(0x0f);
LCDcmd(0x01);
LCDcmd(0x02);
}

#endif // _LCD_CFG_H

Download the HEX File

Frequency Meter Hex File

Custom Search

Leave a Reply