The circuit designers and developers needs different measuring equipments like digital multimeter, CRO, DSO, frequency counter, logic analyser etc for testing their circuits. When they are designing any oscillator or PWM generator or they have generated any waveform, there is a need to measure either frequency or time period. Especially in case of PWM generator there is a need to measure frequency, ON & OFF time and the most required thing is duty cycle. And during testing and troubleshooting they have to measure all these parameters again and again.
Hardware Section
Software Section
2. writedata function sends data byte to be displayed on LCD. It also takes one argument byte and sends it to P2
- 3. writestr function writes whole string (message) on LCD. It takes pointer as an argument that points address of first character of string. then through pointer it sends all the character one by one to P2
- 4. busy function generated delay every time before any byte is sent to LCD
- 5. display function takes two arguments. It converts HEX numbers into decimal and then decimal to ASCII so that they can be displayed on screen

Project Source Code
###
#include<reg51.h>
#include<string.h>
sbit rs = P1^0; // rs pin of LCD
sbit en = P1^1; // en pin of LCD
sbit led= P1^2; // indication LED connected to P0.2
sbit sw = P1^7; // SPDT switch is connected to P1.7
sbit pin = P3^4; // input signal is applied to P3.4
unsigned int flag=0;
unsigned char offtim1, offtim2, ontim1, ontim2,f1,f2;
unsigned int ton,toff,z;
void delay() // generates 1 sec delay
{
int k;
TL1 = 0xBF; // load value in timer 1
TH1 = 0x3C;TR1 = 1; // start timer
for(k=0;k<20;k++) // rotate loop 20 times
{
while(TF1==0);
TF1 = 0;
TL1 = 0xBF;
TH1 = 0x3C;
}
TR1 = 0; // stop timer
}
void busy() // wait until LCD is busy
{
int x;
for(x=0;x<1500;x++);
}
void writecmd(unsigned char a) // send command byte to LCD
{
busy();
rs = 0;
P2 = a;
en = 1;
en = 0;
}
void writedata(unsigned char b) // send data byte to LCD
{
busy();
rs = 1;
P2 = 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); // display all the characters
s++; // one by one
}
}
void frequency() interrupt 2 // external interrupt 1 function
{
EA=0; // first disable interrupts
led=0; // indicate on LED
TH0=0x00; // clear count
TL0=0x00;
TR0=1; // start timer 0
delay(); // give 1 sec delay
TR0=0; // stop timer
f1=TL0; // save count
f2=TH0;
flag=1; // set the flag
led=1; // indication off
}
void period()
{
while(pin==1); // wait to complete one
while(pin==0); // cycle
TR1=1; // start timer 1
while(pin==1); // wait until pulse amplitude is high
TR1=0; // stop timer
offtim1=TL1; // save the content
offtim2=TH1;
TH1=0x00; // clear timer again
TL1=0x00;
while(pin==0); // wait again to complete one
while(pin==1); // cycle
TR1=1; // start timer 1 again
while(pin==0); // wait until pulse amplitude is low
TR1=0; // stop timer
ontim1=TL1; // save the content
ontim2=TH1;
}
void dutycycle()
{
int d,d1;
if((ton>100) || (toff>100)) // scales down values of Ton and Toff
{
ton=ton/10;
toff=toff/10;
}
else if((ton>1000) || (toff>1000))
{
ton=ton/100;
toff=toff/100;
}
d = ton*100/(ton+toff); // calculate duty cycle
d1 = d%10; // separate two digits and convert
d1=d1+0x30; // in to ascii
d=d/10;
d=d+0x30;
writedata(d); // display them
writedata(d1);
writedata(0x25); // display %
}
void display(unsigned char time1, time2) // convert hex to decimal and
{ // decimal to ascii
unsigned int tmp1,tmp2,tmp3,tmp4,t,t1,i,j;
unsigned char asci[5];
tmp1 = (time1 & 0x0F); // tmp1 as lower nibble of lower byte
tmp2 = time1 >> 4; // tmp2 as upper nibble of lower byte
tmp3 = (time2 & 0x0F); // same for tmp3 and tmp4 for
tmp4 = time2 >> 4; // upper byte
tmp2 = tmp2*16; // convert them into decimal
tmp3 = tmp3*256;
tmp4 = tmp4*4096;
t = tmp1+tmp2+tmp3+tmp4; // get the final value in t
if(z==1) ton=t; // for z=1 its Ton and
else toff=t; // vice versa
i=4;
if(t>=10000) // convert all digits into ASCII
{
while(t>=10)
{
t1=t%10; // separate all digits
asci[i]=t1+0x30; // add 30h in all digits to get ASCII value
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; // mask all leading values with space
}
else if (t>=100)
{
while(t>=10)
{
t1=t%10;
asci[i]=t1+0x30;
t=t/10;
i--;
}
asci[2]=t+0x30;
asci[0]=0x20; // mask all leading values with space
asci[1]=0x20;
}
else
{
t1=t%10;
asci[4]=t1+0x30;
t=t/10;
asci[3]=t+0x30;
asci[2]=0x20;
asci[1]=0x20;
asci[0]=0x20;
}
for(j=0;j<5;j++) // display
writedata(asci[j]); // all four digits one by one
}
void main()
{
P2 = 0x00; // P2 as output port
rs=0; // clear all LCD control pins
en=0;
sw=1; // sw as input
led=0;
led=1;
writecmd(0x3C); // initialize LCD
writecmd(0x0E);
writecmd(0x01);
writecmd(0x84);
writestr("Frequency & Measurement"); //display message in 1st and 3rd line of LCD
writecmd(0xC4); // move to 2nd line
writestr("Pulse Width"); // and display message
TH0=0x00; // clear count in T0
TL0=0x00;
IT1=1; // edge trigger interrupt
IE=0x84; // enable external interrupt 1
TMOD=0x15; // timer 0 in 16 bit counter and timer 1 in 16 bit timer
next: while(flag==0); // remain within loop till flag is clear
writecmd(0x01); // clear LCD
writestr("Frequency:"); // display frequency
display(f1,f2);
writestr(" Hz ");
TH1=0x00; // clear T1
TL1=0x00;
period(); // measure time periods
z=0; // for Toff clear flag
writestr("Toff:"); // display Toff
display(offtim1,offtim2);
writestr(" microsec");
z=1; // for Ton set flag
writecmd(0xC0);
writestr("Ton:"); // display Ton
display(ontim1,ontim2);
writestr(" microsec ");
writestr("duty cycle:");
dutycycle(); // calculate and display duty cycle
flag=0; // reset the flag to 0 again
EA=1; // again enable interrupts
if(sw==1) goto next; // if repeat mode again go for next measurement
while(1); // otherwise continuous loop
}
###
Circuit Diagrams
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.