1个回答
展开全部
用比较器将正弦波变为方波,用单片机中的T0或T1口配合测频率,T0可以用来计时,T1用来计算脉冲的个数,当定时1时间到了,计算T1的脉冲个数。calc()函数就是计算频率的函数。
#include "reg51.h"
#define uchar unsigned char
uchar disp[8]={0,0,0,0,0,0,0,0};
uchar T0count,T1count;
void delay(void)
{
uchar i;
for(i=250;i>0;i--);
}
void display()
{
//uchar i,j,k=0x80;
uchar dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar i,k;
k=0x80;
for(i=0;i<8;i++)
{
P2=0;
P0=dispcode[disp[i]];
P0=~P0;
P2=k;
k=k>>1;
delay();
}
P2=0;
}
void calc()
{
uchar i;
long frequency;
frequency=(T0count*256+TH0)*256+TL0;
for(i=7;i>0;i--)
{
disp[i]=frequency%10;
frequency=frequency/10;
}
disp[0]=frequency;
}
void init()
{
T0count=0;
T1count=0;
TH0=0;
TL0=0;
}
void main()
{
init();
TMOD=0x15;
TH1=(65536-5*110592/12)/256;
TL1=(65536-5*110592/12)/256%10;
ET1=1;
ET0=1;
EA=1;
TR1=1;
TR0=1;
//以下四句的作用是在P1.0引脚上形成1000Hz的脉冲,用导线连接到P3.4作为测试用,如果是AT89S51,则四句不用。将其中
//高8位和低8位的初始值更改后可输出不同频率的脉冲。
/*
T2MOD=0x2;
RCAP2H=245;
RCAP2L=74;
TR2=1;
*/
while(1)
{
display();
}
}
void time0() interrupt 1
{
T0count++;
}
void time1() interrupt 3
{
TH1=(65536-5*110592/12)/256;
TL1=(65536-5*110592/12)/256%10;
if(T1count==19)
{
calc();
init();
}
else T1count++;
}
#include "reg51.h"
#define uchar unsigned char
uchar disp[8]={0,0,0,0,0,0,0,0};
uchar T0count,T1count;
void delay(void)
{
uchar i;
for(i=250;i>0;i--);
}
void display()
{
//uchar i,j,k=0x80;
uchar dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar i,k;
k=0x80;
for(i=0;i<8;i++)
{
P2=0;
P0=dispcode[disp[i]];
P0=~P0;
P2=k;
k=k>>1;
delay();
}
P2=0;
}
void calc()
{
uchar i;
long frequency;
frequency=(T0count*256+TH0)*256+TL0;
for(i=7;i>0;i--)
{
disp[i]=frequency%10;
frequency=frequency/10;
}
disp[0]=frequency;
}
void init()
{
T0count=0;
T1count=0;
TH0=0;
TL0=0;
}
void main()
{
init();
TMOD=0x15;
TH1=(65536-5*110592/12)/256;
TL1=(65536-5*110592/12)/256%10;
ET1=1;
ET0=1;
EA=1;
TR1=1;
TR0=1;
//以下四句的作用是在P1.0引脚上形成1000Hz的脉冲,用导线连接到P3.4作为测试用,如果是AT89S51,则四句不用。将其中
//高8位和低8位的初始值更改后可输出不同频率的脉冲。
/*
T2MOD=0x2;
RCAP2H=245;
RCAP2L=74;
TR2=1;
*/
while(1)
{
display();
}
}
void time0() interrupt 1
{
T0count++;
}
void time1() interrupt 3
{
TH1=(65536-5*110592/12)/256;
TL1=(65536-5*110592/12)/256%10;
if(T1count==19)
{
calc();
init();
}
else T1count++;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询