51单片机实现光源的跟踪
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()
{ TMOD=0x01; TF0=0; TH0=(65536-18433)/256;//10ms TL0=(65536-18433)%6; TR0=1; while(!TF0); TR0=0;
评论