Measuring frequency is one of the prime requirements for many applications. The most obvious method of measuring frequency is using CRO (or now a days using DSO). But this instrument is not handheld or available with all the students or hobbyists at any time. It is actually laboratory instrument and not the portable one. Also it is costlier. Also conventional CRO does not give the direct frequency value read out. One has to first set the waveform then find time/division and finely calculate time period and frequency. So it’s a long process that takes time. Another way is use frequency counter that will give us direct digital readout of frequency. In this no need of first adjusting the waveform into screen then find time / division and then calculate time period and frequency such as CRO. Just apply the signal input and get the read out of frequency. That’s why frequency counters finding their own place in measuring instruments.
Circuit Diagram & Connections
Working
Project Source Code
###
#include<reg51.h>
#include<string.h>
sbit rs = P2^7; // rs pin of LCD
sbit en = P2^6; // en pin of LCD
sbit led= P2^5;
sbit sw = P2^1;
sbit pin = P2^0;
unsigned int flag=0,d; // rpm flag
void delay(int a) // 1 sec or 5 sec delay
{
int k;
TL1 = 0xBF; // load value 15550=3CBF
TH1 = 0x3C;
TR1 = 1;
for(k=0;k<a;k++)
{
while(TF1==0);
TF1 = 0;
TL1 = 0xBF;
TH1 = 0x3C;
}
TR1 = 0;
}
void busy() // check busy flag
{
int x;
for(x=0;x<1500;x++);
}
void writecmd(unsigned char a) // send command byte to LCD
{
busy();
rs = 0;
P0 = a;
en = 1;
en = 0;
}
void writedata(unsigned char b) // send data byte to LCD
{
busy();
rs = 1;
P0 = b;
en = 1;
en = 0;
}
void writestr(unsigned char *s) // write string / message to LCD
{
unsigned char l,i;
l = strlen(s);
for(i=0;i<l;i++)
{
writedata(*s);
s++;
}
}
void int1(void) interrupt 2 // external interrupt 1 function
{
EA=0; // first disable interrupts
led=0;
TH0=0x00; // clear T0
TL0=0x00;
if(pin==0) d=20; // for freq counter delay is 1 sec and
else d=100; // for RPM counter delay is 5 sec
TR0=1; // start timer 0
delay(d); // give 1 sec or 5 sec delay
TR0=0; // stop timer
flag=1; // if not then set the flag
led=1;
}
void display() // convert hex to desimal and
{ // decimal to ascii
unsigned int tmp1,tmp2,tmp3,tmp4,t,t1,i,j;
unsigned char asci[5];
tmp1 = (TL0 & 0x0F);
tmp2 = TL0 >> 4;
tmp3 = (TH0 & 0x0F);
tmp4 = TH0>>4;
tmp2 = tmp2*16;
tmp3 = tmp3*256;
tmp4 = tmp4*4096;
t = tmp1+tmp2+tmp3+tmp4;
if(pin==1) t=t*12; // for RPM multiply the value by 12
i=4;
if(t>=10000)
{
while(t>=10)
{
t1=t%10;
asci[i]=t1+0x30;
t=t/10;
i--;
}
asci[0]=t+0x30;
}
else if (t>=1000)
{
while(t>=10)
{
t1=t%10;
asci[i]=t1+0x30;
t=t/10;
i--;
}
asci[1]=t+0x30;
asci[0]=0x20;
}
else
{
while(t>=10)
{
t1=t%10;
asci[i]=t1+0x30;
t=t/10;
i--;
}
asci[2]=t+0x30;
asci[1]=0x20;
asci[0]=0x20;
}
writecmd(0x01);
if(pin==0) writestr("frequency:"); // display current RPM and
else writestr("current RPM:");
writecmd(0xC0);
for(j=0;j<5;j++)
writedata(asci[j]); // all four digits one by one
if(pin==0) writestr(" Hz");
}
void main()
{
TMOD=0x15; // timer 0 in 16 bit counter and timer 1 in 16 bit counter
P0 = 0x00; // P0 as output port
rs=0; // clear all LCD control pins
en=0;
sw=1;
led=0;
led=1;
pin=1;
writecmd(0x3C); // initialize LCD
writecmd(0x0E);
writecmd(0x01);
if(pin==0)
{
writecmd(0x84); // initially display message
writestr("Frequency"); // frequency counter
}
else
{
writecmd(0x87);
writestr("RPM"); // or RPM counter
}
writecmd(0xC5);
writestr("Counter");
TH0=0x00; // clear T0
TL0=0x00;
IT1=1;
IE=0x84; // enable external interrupt 1
next: while(flag==0); // remain within loop till rpm flag is clear
display(); // when flag is set display value
flag=0; // reset the flag to 0 again
EA=1; // again enable interrupts
If(pin==0) {d=100;delay(d);} // for continuous freq measurement give
// delay of 5 sec
if(sw==0) goto next; // again go for next measurement
while(1);
}
###
Circuit Diagrams
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.