欢迎来到天天文库
浏览记录
ID:40567494
大小:42.50 KB
页数:8页
时间:2019-08-04
《PWM小车壁障循迹程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、3.程序清单#include#defineucharunsignedchar#defineuintunsignedint#defineMOTOR_CP1//P2口作为电机的控制口。#defineSIGNALP0//P3口的低两位为循迹传感器输入口。低后,高前。#defineSHELVES30//速度总档数。#defineBACK0xfa//后退。#defineFORWARD0xf5//前进。sbitlsig=P0^0;sbitrsig=P0^1;sbitrs=P3^2;sbitrw=P3^3;sbite=P3^4;sbithw
2、f=P1^6;//前壁障sbithwb=P1^7;//后壁障sbitPWM_R=P1^0;//右电机PWM输入口。sbitPWM_L=P1^2;//左电机PWM输入口。sbitPWM_br=P1^1;//后退右sbitPWM_bl=P1^3;//后退左ucharcodetable1[]="正向行驶";ucharcodetable2[]="反向行驶";ucharcodetable3[]="停止中...";ucharcodetab[]="v=cm/s";ucharnum;ucharpercent_l=0;//左轮占空比。ucharpercent_r
3、=0;//右轮占空比。ucharpercent_bl=0;//后退左ucharpercent_br=0;//后退右ucharrun_time=0;//车轮运行一步的时间。/**********lcd部分************/voiddelay(uintz){uintx,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}voidwrite_com(ucharcom){rw=0;rs=0;P2=com;delay(5);e=1;delay(5);e=0;}voidwrite_data(uchardate){rs=1;P
4、2=date;delay(5);e=1;delay(5);e=0;}voidinit(){e=0;write_com(0x01);write_com(0x02);write_com(0x06);write_com(0x0c);write_com(0x38);}/*********小车部分********/voidtimer_init(void){TMOD=0x22;TH0=0x38;TL0=0x38;TH1=0x38;TL1=0x38;EA=1;ET0=1;TR0=1;ET1=1;TR1=1;}voidright(void){MOTOR_C=F
5、ORWARD;percent_r=0;percent_l=6;}voidh_right(void){MOTOR_C=BACK;percent_br=0;percent_bl=6;}voidleft(void){MOTOR_C=FORWARD;percent_r=6;percent_l=0;}voidh_left(void){MOTOR_C=BACK;percent_br=6;percent_bl=0;}voidforward(void){MOTOR_C=FORWARD;percent_r=3;percent_l=3;}voidback(void
6、){MOTOR_C=BACK;percent_br=3;percent_bl=3;}voidstop(void){percent_r=0;percent_l=0;percent_br=0;percent_bl=0;}voidtrack(){stop();while(hwf){write_com(0x80);for(num=0;num<12;num++){write_data(table1[num]);delay(20);}if((rsig==0)&&(lsig==0))forward();elseif((rsig==1)&&(lsig==0))
7、left();elseif((rsig==0)&&(lsig==1))right();elseif((rsig==1)&&(lsig==1))stop();}}voidhw_track(){stop();while(hwb){write_com(0x80);for(num=0;num<12;num++){write_data(table2[num]);delay(20);}if((rsig==0)&&(lsig==0))back();elseif((rsig==1)&&(lsig==0))h_left();elseif((rsig==0)&&(
8、lsig==1))h_right();elseif((rsig==1)&&(lsig==1))stop();}}voidhwstop(void){wh
此文档下载收益归作者所有