系統(tǒng)程序:
#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() //設(shè)置脈沖捕捉
{
EIE1=0x00; //禁止脈沖捕捉中斷
PCA0MD=0x00; //系統(tǒng)時(shí)鐘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() //主函數(shù)
{
uint t0;
uint dk1=0,dk2=0;
WDTCN=0xde;
WDTCN=0xad; //關(guān)看門(mén)狗
XBR0=0x12;
XBR2=0x40; //交叉開(kāi)關(guān)設(shè)置,外部中斷無(wú)引腳
XBR1=0x00;
OSCICN=0x95; //內(nèi)部時(shí)鐘4MHz
DAC1CN=0; //DCA1無(wú)效
PRT0CF=0xc0;
PRT1CF=0x18;
CKCON=0xe7; //系統(tǒng)時(shí)鐘12分頻
TMOD=0x11;
TCON=0x00; //外部中斷請(qǐng)求及標(biāo)志位0
TH0=0x00;
TL0=0x00;
TH1=0x00;
TL1=0x00;
pca1(); //pca初始化
REF0CN=0x03;
DAC0CN=0x84; //DCA0使能,左對(duì)齊
P1_0=0;
P1_2=1;
P0_6=0;
P0_7=0;
P1_3=0;
P1_4=0;
EA=1; //中斷設(shè)置
ET0=1;
PT0=1;
ET1=1;
PT1=1;
a=0x91A0;
//a=0xF2A0;
DAC0(a);
delay();
//****************************測(cè)頻*****************************//
while(1){
for(i=1;i<4;i++){
CCF0=0; //變頻上升沿標(biāo)志
while(CCF0==0)
{}
TR1=1;
CCF0=0; //變頻上升沿標(biāo)志
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; //進(jìn)入鑒頻鑒相
//***************************鑒頻*****************************//
while(d==1){ //測(cè)量變頻周期
for(i=1;i<4;i++){
CCF1=0; //變頻上升沿標(biāo)志
while(CCF1==0)
{}
TR1=1;
CCF1=0; //變頻上升沿標(biāo)志
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;//假設(shè)變頻超前測(cè)t1
if(t1>=(dk1-700))
t1=t1-(dk1-700);
else
t1=700+t1; //補(bǔ)償
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; //假設(shè)工頻超前測(cè)t2
f(t2>=700)
t2=t2-700;
else
t2=dk1-(700-t2); //補(bǔ)償
if(t1<=t2) //變頻超前工頻
{if(t1<250)
{a=CON/dk1*16-t1;
DAC0(a);}
else{
a=CON/dk1*16-250;
DAC0(a);}
n=0; //轉(zhuǎn)入測(cè)頻
}
else //工頻超前變頻
{if(t2<250)
{a=CON/dk1*16+t2;
DAC0(a);}
else{
a=CON/dk1*16+250;
DAC0(a);}
n=0; //轉(zhuǎn)入測(cè)頻
}
}
}
}
void t0_ISR()interrupt 1 //中斷服務(wù)程序
{TH0=0x00;
TL0=0x00;
TF0=0;
j++;
if(j==80)
{j=0;
P0_7=1;}
}
void t1_ISR()interrupt 3 //中斷服務(wù)程序
{TH1=0x00;
TL1=0x00;
TF1=0;
}