资源描述:
《循迹小车完整程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、程序#include//********驱动芯片L298管脚位声明*****sbitIN1=P1^0;sbitPWM1=P1^1;sbitIN2=P1^2;sbitIN3=P1^3;sbitPWM2=P1^4;sbitIN4=P1^5;//********传感器TCRT5000管脚位声明****sbitXL=P1^6;//左侧第一个传感器sbitXR=P1^7;//右侧第一个传感器sbitYL=P2^0;//左侧第二个传感器sbitYR=P2^1;//右侧第二个传感器//********用于定时计
2、数的两个全局变量位声明******intcount1=0;intcount2=0;//********左边电机前进*******voidforward_turn1(){IN1=0;IN2=0;}5//*********左边电机后退******voidreverse_tuen1(){IN1=1;IN2=0;}//*********右边电机前进*******voidforward_turn2(){IN3=0;IN4=1;}//**********右边电机后退********voidreverse_turn2(){IN3
3、=1;IN4=0;}//***********左边电机速度控制函数******voidspeed1(intct,intsd){if(ct<=sd)PWM1=1;elsePWM1=0;}//************右边电机速度控制函数******viodspeed2(intct,intsd){if(ct<=sd)5PWM2=1;elsePWM2=0;//*************小车直线前进函数*********voidadvance(intct1,intsd1,intct2,intsd2);{forward_tur
4、n1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//**********小车左转********voidleft_turn1(intct1,intsd1,intct2,intsd2);{forward_turn1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//************小车右转*********viodriht_turn1(intct1,intsd1,intct2,intsd2);{
5、forward_turn1();forward_turn2();speed1(ct1,sd1);speed2(ct2,sd2);}//**************主函数**********main()5{TMOD=ox11;TH0=(65536-1000)/256;TL0=(65536-1000)%256;EA=1;ET0=1;TR0=1;TH1=(65536-1000)/256;TL1=(65536-1000)%256;EA=1;ET1=1;TR1=1;while(1){if(XL==0&&XR==0&&YL==
6、0&&YR==0)//传感器未检测到直线,小车直行{advance(count1,500,count2,500);}if(XL==1&&XR==0&&YL==0&&YR==0)//左边内侧传感器检测到黑线,小左转{Left_turn1(count1,200,count2,500);}if(XL==0&&XR==0&&YL==1&&YR==0)//左边外侧传感器检测到黑线,大左转{Left_turn1(count1,200,count2,700);}if(XL==0&&XR==1&&YL==0&&YR==0)//右边
7、内侧传感器检测到黑线,小右转{right_turn1(count1,500,count2,200);}if(XL==0&&XR==0&&YL==0&&YR=1)//右边外侧传感器检测到黑线,大右转{right_turn1(count1,700,count2,200);}if(XL==1&&XR==0&&YL==1&&YR=0)//左侧两个传感器均检测到黑线,中左转{5Left_turn1(count1,200,count2,600);}if(XL==0&&XR==1&&YL==0&&YR=1)//右侧两个传感器均检
8、测到黑线,中右转{right_turn1(count1,600,count2,200);}}}//******中断服务程序*******viodtime0()interrupt1;{TH0=(65536-1000)/256;TL0=(65536-1000)%256;count1++;if(count1>=1000)count1=0}viodtime1()in