系统程序:
#include <c8051f000.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
sbit P1_0=P1^0;
sbit P1_2=P1^2;
sbit P0_6=P0^6;
sbit P0_4=P0^4;
sbit P0_7=P0^7;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
#define CON 28585756
uint t[4]={0,0,0,0};
uint i=0,a=0,t1=0,t2=0,e=0,tt=0;
char d=1,n=1,jj=0,j=0,m=0;
union tcfint16{
uint myword0;
struct{uchar hi;uchar low;}bytes0;
}myint16;
union tcfint116{
uint myword1;
struct{uchar hi;uchar low;}bytes1;
}myint116;
void pca1() //设置脉冲捕捉
{
EIE1=0x00; //禁止脉冲捕捉中断
PCA0MD=0x00; //系统时钟12分频,禁止pca中断
PCA0CN=0x00; //CR=0
PCA0CPM0=0x20;
PCA0CPM1=0x20; //正沿捕捉
}
void DAC0(uint DAChl)
{
myint16.myword0=DAChl;
DAC0L=myint16.bytes0.low;
DAC0H=myint16.bytes0.hi;
}
void delay()
{
uint xxxx,xxx=999,xx,x;
for(x=0;x<400;x++)
for(xx=0;xx<x;xx++)
{
xxxx=xxx/1000;
}
}
main() //主函数
{
uint t0;
uint dk1=0,dk2=0;
WDTCN=0xde;
WDTCN=0xad; //关看门狗
XBR0=0x12;
XBR2=0x40; //交叉开关设置,外部中断无引脚
XBR1=0x00;
OSCICN=0x95; //内部时钟4MHz
DAC1CN=0; //DCA1无效
PRT0CF=0xc0;
PRT1CF=0x18;
CKCON=0xe7; //系统时钟12分频
TMOD=0x11;
TCON=0x00; //外部中断请求及标志位0
TH0=0x00;
TL0=0x00;
TH1=0x00;
TL1=0x00;
pca1(); //pca初始化
REF0CN=0x03;
DAC0CN=0x84; //DCA0使能,左对齐
P1_0=0;
P1_2=1;
P0_6=0;
P0_7=0;
P1_3=0;
P1_4=0;
EA=1; //中断设置
ET0=1;
PT0=1;
ET1=1;
PT1=1;
a=0x91A0;
//a=0xF2A0;
DAC0(a);
delay();
//****************************测频*****************************//
while(1){
for(i=1;i<4;i++){
CCF0=0; //变频上升沿标志
while(CCF0==0)
{}
TR1=1;
CCF0=0; //变频上升沿标志
while(CCF0==0)
{}
TR1=0;
myint116.bytes1.low=TL1;
myint116.bytes1.hi=TH1;
TH1=0;
TL1=0;
if(myint116.myword1>6000&&myint116.myword1<30000) //剔除粗大误差值
{m++;
t[m]=myint116.myword1;
}
else
i--;}
for(i=1;i<=2;i++) //中值滤波
for(jj=1;jj<=3-i;jj++)
if(t[jj]>t[jj+1])
{t[0]=t[jj];
t[jj]=t[jj+1];
t[jj+1]=t[0];
}
dk1=t[2];
for(i=0;i<4;i++)
t=0;
n=1; //进入鉴频鉴相
//***************************鉴频*****************************//
while(d==1){ //测量变频周期
for(i=1;i<4;i++){
CCF1=0; //变频上升沿标志
while(CCF1==0)
{}
TR1=1;
CCF1=0; //变频上升沿标志
while(CCF1==0)
{}
TR1=0;
myint116.bytes1.low=TL1;
myint116.bytes1.hi=TH1;
TH1=0;
TL1=0;
if(myint116.myword1>6000&&myint116.myword1<30000)
{m++;
t[m]=myint116.myword1;
}
else
i--; }
for(i=1;i<=2;i++) //中值滤波
for(jj=1;jj<=3-i;jj++)
if(t[jj]>t[jj+1])
{t[0]=t[jj];
t[jj]=t[jj+1];
t[jj+1]=t[0];
}
dk2=t[2];
for(i=0;i<4;i++)
t=0;
if(dk2>dk1+2) //鉴频
{a=a+192;
DAC0(a);}
else
d=0;
}
a=0xF2A0;
DAC0(a);
//****************************鉴相*****************************// TR0=1;
while(n==1)
{
CCF1=0;
while(CCF1==0)
{}
TR1=1;
CCF0=0;
while(CCF0==0)
{}
TR1=0;
myint116.bytes1.low=TL1;
myint116.bytes1.hi=TH1;
TH1=0;
TL1=0;
t1=myint116.myword1;//假设变频超前测t1
if(t1>=(dk1-700))
t1=t1-(dk1-700);
else
t1=700+t1; //补偿
CCF0=0;
while(CCF0==0)
{}
TR1=1;
CCF1=0;
while(CCF1==0)
{}
TR1=0;
myint116.bytes1.low=TL1;
myint116.bytes1.hi=TH1;
TH1=0;
TL1=0;
t2=myint116.myword1; //假设工频超前测t2
f(t2>=700)
t2=t2-700;
else
t2=dk1-(700-t2); //补偿
if(t1<=t2) //变频超前工频
{if(t1<250)
{a=CON/dk1*16-t1;
DAC0(a);}
else{
a=CON/dk1*16-250;
DAC0(a);}
n=0; //转入测频
}
else //工频超前变频
{if(t2<250)
{a=CON/dk1*16+t2;
DAC0(a);}
else{
a=CON/dk1*16+250;
DAC0(a);}
n=0; //转入测频
}
}
}
}
void t0_ISR()interrupt 1 //中断服务程序
{TH0=0x00;
TL0=0x00;
TF0=0;
j++;
if(j==80)
{j=0;
P0_7=1;}
}
void t1_ISR()interrupt 3 //中断服务程序
{TH1=0x00;
TL1=0x00;
TF1=0;
}