Frequency counter circuit
Simple Frequency Counter
You may have seen several projects like Frequency counter, Digital Frequency Counter, and so on on various websites. I’m going to post another one of them. The use of an AVR microcontroller (Atmega8) timer/counter in one of its forms. This circuit can be used in your engineering classes as a simple microcontroller project. A periodic signal’s frequency is the number of cycles it repeats each second! As a result, counting the number of cycles recorded in a second yields the frequency. So, what we’re going to build is a frequency counter circuit, also known as a frequency metre.
This frequency metre was created using 1) We require a signal (whose frequency has to be counted) 2) Avr’s Atmega8 microcontroller 3) An LCD that shows the frequency that has been counted. I’m assuming you’ve worked with the Avr Atmega8 and know how to programme it. You should also be aware of how to connect an LCD to an AVR.
Now let’s get into the guts of the project – the Simple Frequency Counter, or Frequency Meter as it’s also known.
Examine the circuit schematic below, as well as the programme at the end of this post.
Description of circuit:-
So here’s what I did: I reset the counter to zero, waited 1S, and then read the counter again. But keep in mind that you must read the value as soon as the delay loop ends. It’s straightforward. Simply create a variable and transfer the count into it. The variable’s data type is effectively an unsigned integer. You can also use the floating point data type! However, you must typecast it in this case! That’s it! To learn more about Avr’s floating point conversion, read this article attentively. Avr String Formatting
Also, yay! It is preferable to use a conditioned signal to count the frequency. i.e. a square wave or a pulse trace You can use any signal conditioning circuit you like, such as a comparator, Schmitt trigger, or sine wave to square wave converter. Use a conditioning circuit if the signal is low in power. This website has a number of signal conditioning circuits – check it out – Signal Conditioner Circuits.
Here are the technical specifications for my project. I’m hoping that making this won’t be too difficult for you.
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<<EN);
_delay_us(DEL1);
CPORT = (0<<EN);
_delay_us(DEL2);
if(ch==0x01||ch==0x02)
_delay_ms(10);
}
int LCD(char ch, FILE *fp)
{ DPORT =ch;
CPORT = (1<<EN)|(1<<RS);
_delay_us(DEL1);
CPORT = (0<<EN)|(0<<RS);
_delay_us(DEL2);
return 0;
}
void initLCD()
{ CPDDR = (1<<EN)|(1<<RS);
DPDDR = 0xff;
LCDcmd(0x38);
LCDcmd(0x0f);
LCDcmd(0x01);
LCDcmd(0x02);
}
#endif // _LCD_CFG_H