ϵ½y(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() //ÔO(sh¨¨)ÖÃÃ}›_²¶×½
{
EIE1=0x00; //½ûÖ¹Ã}›_²¶×½ÖДà
PCA0MD=0x00; //ϵ½y(t¨¯ng)•r(sh¨ª)çŠ12·Öîl£¬½ûÖ¹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; //êP(gu¨¡n)¿´éT¹·
XBR0=0x12;
XBR2=0x40; //½»²æé_êP(gu¨¡n)ÔO(sh¨¨)ÖÃ,ÍⲿÖДàŸoÒýÄ_
XBR1=0x00;
OSCICN=0x95; //ƒÈ(n¨¨i)²¿•r(sh¨ª)çŠ4MHz
DAC1CN=0; //DCA1ŸoЧ
PRT0CF=0xc0;
PRT1CF=0x18;
CKCON=0xe7; //ϵ½y(t¨¯ng)•r(sh¨ª)çŠ12·Öîl
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¨¬)ýR
P1_0=0;
P1_2=1;
P0_6=0;
P0_7=0;
P1_3=0;
P1_4=0;
EA=1; //ÖДàÔO(sh¨¨)ÖÃ
ET0=1;
PT0=1;
ET1=1;
PT1=1;
a=0x91A0;
//a=0xF2A0;
DAC0(a);
delay();
//****************************œy(c¨¨)îl*****************************//
while(1){
for(i=1;i<4;i++){
CCF0=0; //׃îlÉÏÉýÑؘË(bi¨¡o)Ö¾
while(CCF0==0)
{}
TR1=1;
CCF0=0; //׃îlÉÏÉýÑؘË(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++) //ÖÐÖµžV²¨
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; //ßM(j¨¬n)ÈëèbîlèbÏà
//***************************èbîl*****************************//
while(d==1){ //œy(c¨¨)Á¿×ƒîlÖÜÆÚ
for(i=1;i<4;i++){
CCF1=0; //׃îlÉÏÉýÑؘË(bi¨¡o)Ö¾
while(CCF1==0)
{}
TR1=1;
CCF1=0; //׃îlÉÏÉýÑؘË(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++) //ÖÐÖµžV²¨
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) //èbîl
{a=a+192;
DAC0(a);}
else
d=0;
}
a=0xF2A0;
DAC0(a);
//****************************èbÏà*****************************// 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;//¼ÙÔO(sh¨¨)׃îl³¬Ç°œy(c¨¨)t1
if(t1>=(dk1-700))
t1=t1-(dk1-700);
else
t1=700+t1; //Ña(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; //¼ÙÔO(sh¨¨)¹¤îl³¬Ç°œy(c¨¨)t2
f(t2>=700)
t2=t2-700;
else
t2=dk1-(700-t2); //Ña(b¨³)ƒ”
if(t1<=t2) //׃îl³¬Ç°¹¤îl
{if(t1<250)
{a=CON/dk1*16-t1;
DAC0(a);}
else{
a=CON/dk1*16-250;
DAC0(a);}
n=0; //ÞD(zhu¨£n)Èëœy(c¨¨)îl
}
else //¹¤îl³¬Ç°×ƒîl
{if(t2<250)
{a=CON/dk1*16+t2;
DAC0(a);}
else{
a=CON/dk1*16+250;
DAC0(a);}
n=0; //ÞD(zhu¨£n)Èëœy(c¨¨)îl
}
}
}
}
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;
}