欢迎来到天天文库
浏览记录
ID:34173562
大小:70.30 KB
页数:8页
时间:2019-03-04
《《超声波智能小车》doc版》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include#defineucharunsignedchar#defineuintunsignedintsbitSignal=P1^7;sbitRX=P2^4;sbitTX=P2^5;sbitlcden=P3^1;sbitlcdrs=P3^0;unsignedinttime=0;//用于存放定时器时间值unsignedintS=0;//用于存放距离的值bitflag=0;unsignedchardisbuff[]="0000";unsignedcharcodetable1[]=
2、"!!!Outofrange";voidmotor_r_z(void);//右边电动机正转voidmotor_l_z(void);//左边电动机正转voidmotor_r_f(void);//右边电动机反转voidmotor_l_f(void);//左边电动机反转voidback(uchar,uchar);//小车后退voidgo(uchar,uchar);//小车前进voidstop(void);//小车停止voidleft(void);//小车左转voidright(void);//小车右转sbitPWM1=P1^5;sbitPWM
3、2=P1^4;sbitPWM3=P1^1;sbitPWM4=P1^2;sbitEN1=P1^0;sbitEN2=P1^3;sbitstart_k=P3^2;uchardatat_0;uchardatamotor_r;uchardatamotor_l;uchardataValue;uchardatamid;//**延时子程序**///voiddelay_1ms(uintn){uinti,j;for(j=n;j>0;j--)for(i=20;i>0;i--);}voidini(void){////T0初始化///TMOD=0x01;//T0
4、工作在方式1TH1=0xff;//装入T0初值TL1=0xf6;TR1=1;//开T0中断ET1=1;//T0允许中断EA=1;//////////////////////t_0=0;/////////////////////P2=0;}voidstart(void){uchara;aa:while(start_k);for(a=0;a<50;a++){delay_1ms(1);while(start_k)gotoaa;}go(0x30,0x30);}voidgo(ucharleft_motor,ucharright_motor)//直
5、行{Value=right_motor;motor_r_z();Value=left_motor;motor_l_z();}voidback(ucharleft_motor,ucharright_motor)//后退{Value=right_motor;motor_r_f();Value=left_motor;motor_l_f();}voidmotor_r_z(void)//右边电动机正转{motor_r=0x64+Value;EN1=1;}voidmotor_l_z(void)//左边电动机正转{motor_l=0x64-Value
6、;EN2=1;}voidmotor_r_f(void)//右边电动机反转{motor_r=0x64-Value;EN1=1;}voidmotor_l_f(void)//左边电动机反转{motor_l=0x64+Value;EN2=1;}voidstop(void){EN2=0;EN1=0;}voidtime0(void)interrupt3{TR1=0;TH1=0xff;TL1=0xf6;++t_0;ACC=t_0;CY=0;ACC-=motor_r;if(CY==1){PWM1=1;PWM2=0;gotoPWM_2;}PWM1=0;P
7、WM2=1;PWM_2:ACC=t_0;CY=0;ACC-=motor_l;if(CY==1){PWM3=1;PWM4=0;gotoHIGHT;}PWM3=0;PWM4=1;HIGHT:ACC=t_0;if(ACC!=0xc9)gotoEXIT;ACC=0;t_0=ACC;EXIT:TR1=1;}voidDelay400Ms(void){unsignedcharTempCycA=5;unsignedintTempCycB;while(TempCycA--){TempCycB=7269;while(TempCycB--);};}voidd
8、elay(uintz){uintx,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}voidwrite_com(ucharcom){lcdrs=0;P2=com;delay(5)
此文档下载收益归作者所有