#include
#include
#include
void
ADC_init();
int
get_ADC_result(char ch);
unsigned int
show_ADC_result_ch0();
unsigned int
show_ADC_result_ch1();
void
Send(unsigned char dat);
void
uart_init();
void
delay(unsigned int a);
void
delay_10ms();
sbit
P2_0=P2^0;
sbit
P2_1=P2^1;
sbit
P1_3=P1^3;
void
positive_turn(unsigned int count); //順時針
void
negative_turn(unsigned int count); //逆時針
void
stop_turn();
void
main()
{
unsigned
int ch0,ch1;
int
value,chazhi,z1,z2,z3,z4,gap;
gap=30;
while(1)
{
uart_init();
ADC_init();
//顯示左右兩傳感器電壓
ch0=show_ADC_result_ch0();
//通道ch0電壓
Send(0x20); //四個空格符
Send(0x20);
Send(0x20);
Send(0x20);
ch1=show_ADC_result_ch1();
//通道ch1電壓
Send(0x20); //四個空格符
Send(0x20);
Send(0x20);
Send(0x20);
//顯示差值電壓
value=ch0-ch1;
if(ch0>ch1)
{chazhi=ch0-ch1;}
else
{chazhi=ch1-ch0;}
z1=chazhi/1000;
z2=chazhi00/100;
z3=chazhi000/10;
z4=chazhi000;
Send(0x43); //'C'
Send(0x3D); //'='
if(ch0
{Send(0x2D);}
else
{Send(0x2B);}
Send(z1+48);
Send(0x2E); //'.'
Send(z2+48);
Send(z3+48);
Send(z4+48);
if((ch0+gap)
{Send(0x52);}
if(ch0>(ch1+gap))
{Send(0x4C);}
if(-gap<(ch0-ch1) || (ch0-ch1)
{Send(0x20);}
//Send(0xD);
//回車
//Send(0xA);
//換行
//光源跟蹤
if(-gap
stop_turn();
else
if(value<-gap)
negative_turn(10);
else
if(value>gap)
positive_turn(10);
//for(p=0;p<50;p++)
//延時1s刷新一次
//{delay_10ms();}
}
}
void
ADC_init()
{
P1ASF=0x03;//設置P1.0,P1.1為AD轉換口
ADC_CONTR=0x80;//打開AD電源、選擇轉換速度、
ADC_RES=0;
ADC_RESL=0;
AUXR1&=0xfb;
//設置AD轉換結果存放方式,此設為10位
delay(5);
}
int
get_ADC_result(char ch)
{
unsigned int
result;
ADC_CONTR=0x88|ch;
//開啟AD,選擇通道
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR &
0x10));//等待轉換完成,FLAG標志位置位
ADC_CONTR&=0x67;
//關AD,清FLAG 01100111
result=ADC_RES*4+ADC_RESL;
//獲取10位結果
return(result);
}
unsigned int show_ADC_result_ch0()
{
unsigned
long int VCC1;
unsigned
int A1,B1,C1,D1,i;
VCC1=0;
for(i=0;i<8;i++)
{
VCC1=VCC1+get_ADC_result(0);}
VCC1=VCC1/8;
VCC1=get_ADC_result(0);
VCC1=(long
int)VCC1*10000/1024*5;
A1=VCC1/10000;
B1=VCC1000/1000;
C1=VCC100000/100;
D1=VCC1000000/10;
Send(0x56); //'V'
Send(0x31); //'1'
Send(0x3D); //'='
Send(A1+48);
Send(0x2E); //'.'
Send(B1+48);
Send(C1+48);
Send(D1+48);
//Send(0xD);
//回車
//Send(0xA);
//換行
return(A1*1000+B1*100+C1*10+D1);
}
unsigned int show_ADC_result_ch1()
{
long int
VCC2;
unsigned
int A2,B2,C2,D2,i;
VCC2=0;
for(i=0;i<8;i++)
{
VCC2=VCC2+get_ADC_result(1);}
VCC2=VCC2/8;
VCC2=get_ADC_result(1);
VCC2=(long
int)VCC2*10000/1024*5;
A2=VCC2/10000;
B2=VCC2000/1000;
C2=VCC200000/100;
D2=VCC2000000/10;
Send(0x56); //'V'
Send(0x32); //'1'
Send(0x3D); //'='
Send(A2+48);
Send(0x2E); //'.'
Send(B2+48);
Send(C2+48);
Send(D2+48);
//Send(0xD);
//回車
//Send(0xA);
//換行
return(A2*1000+B2*100+C2*10+D2);
}
void
uart_init()
{
TMOD=0x20;
//TMOD=0010
0000B,定時器T1工作于方式2
SM0=0;
//選擇串行口工作方式1
SM1=1;
TH1=0xfa;
//根據規定給定時器T1賦初值 9600波特率
TL1=0xfa;
//根據規定給定時器T1賦初值
TR1=1;
//啟動定時器T1
}
void
Send(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void
positive_turn(unsigned int count)
{
P2_0=1;
P2_1=0;
CMOD =
0x00;//設置PCA時鐘周期 系統時鐘12分頻 fosc/12
CL = 0x00;
//初值設置
CH =
0x00;
CCAP0L =
count; //設置初始占空比為25%
CCAP0H =
count;//占空比 設置的是低電平的時間
CCAPM0 =
0X42;//PWM模式
CR =
1;//開啟PCA計數器
}
void
negative_turn(unsigned int count)
{
P2_0=0;
P2_1=1;
CMOD =
0x00;//設置PCA時鐘周期 系統時鐘12分頻 fosc/12
CL = 0x00;
//初值設置
CH =
0x00;
CCAP0L =
count; //設置初始占空比為25%
CCAP0H =
count;//占空比 設置的是低電平的時間
CCAPM0 =
0X42;//PWM模式
CR =
1;//開啟PCA計數器
}
void
stop_turn()
{
P1_3=1;
P2_0=1;
P2_1=1;
}
void
delay(unsigned int a)
{
unsigned int b,i,tt;
b=a;
for(i=1;i
{tt=60000;
while(tt--);}
}
void
delay_10ms()
{