还剩10页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
#in cludereg
51、H#i nc1u de intri n s h//#in eludelcd hns b i t TX=P2A7;sbi tRX=P2人6;sbit PWM=P14;sbit Bee p=P3A7;sbi tled1=P3A3;**********蓝牙切换按键**********/*s bitle d3=P3A5;sbi tk1=P3A6;/***自定义一些数据,注意这些数据对应“51智能车蓝牙无线遥控、exe”软件上得数据设置**/#d efine le f tda t a Oxll#d efi n e rightda t a0x22#de fine f0rward data0x33#dcfi n e b a ck data0x44#d efi n estop data0x55#de fi ne u c har unsign e d char#def i neui n t unsign e d intu ns igne dchar reC eiveData^hig h_v e1o Sity,1ow_velosity;vo i d UsartC Onfig ur a tion;/***********电;]^端口定义************************sbit IN1=P1A0;//为高电平时,左电机后转bsb it IN2=Pl八1;//为高电平时,左电机正转sbi tIN3=P1A2;//为高电平时,右电机正转****************无线遥控模块定义sbit IN4二Pl人3;//为高电平时,右电机后转sbi tIey_A=P2人0;//B键信号端对应D0s bitKey_B=P27;//D键信号端对应DIs bitK ey_C=P2A2;//C键信号端对应D2sbit K ey_D=P2A3;//C键信号端void d e lay0i n t a;void cle hyl5us v o id;vo id dela y1ms intz;void diQ;//蜂鸣器函数声明uns igne d in tt ime;//用于存放定时器时间值unsigned intS;//用于存放距离得值char f1ag=0;//量程溢出标志位char f1,f2,f3,f4,n0;i nt a;int n;//运行次数判断标志in ts_le f t,s_righ t;〃2键被按下fl=0;f2=0;f3=1;f4=0;di;else ifIe y_D==1K ey_B==0Key_A==0Iey—C==0//D键被按下00{fl=0;f2=0;f3=0;f4=l;di00o j-0{b3o Start Module;fora=951;a0;a-------B00{b b o ifRX==l{b Timer_Count;e osC ountlQ;iff200j3b runba ckQ;00}iff3{loo p;o o o j.b3®if f4{stopO;°e}b o j.b bi fn0==16{03i f1ow_vel osity30{3s witch rece i veDat a00{case le ft dat a:PWM_RUN LEFT30;break;case righ tdata:PWM_RUNRIGHT30;b reak;3case for\v ar ddat a:PWM_RUNFRONT64,70;br e ak;b bc ase6backdatePWM_R UNBACK64,70;bre ak;3o ca seS topda ta:bluest0p;br eak;000}00}3o i f h igh_vel osi ty00{swi tchr ec eiveData{cas eleftdataPWM_RUNL EFT50;break;333case r ightdata:PWM_RUNRIGHT50;bre ak;c aseforwarddata:PWM_RU NF RONT94,100;b reak;6o caseb ackdata:PWM_RUNBACK94,100;b reak;o ca sest opda ta:stop;bre ak;0oj.o e}/*****************J^口鸟*********************void dib int i;3f ori=0;i150;i++{Beep=1;dela y020;°}B ee p=0;}v o id de1a y0i n t achar j;in ti;f ori=a;i0;i--forj=200;j0;j-;}void del a yIm si ntz〃延时1ms{int x,y;for x=z;x0;x-fory=ll0;y0;y-;vo id Dela y10usu nsi g ned ch ari uns i gnedchar j;b do{J=10;d O{_n op_;}while--j;}whi1e--i;******************************************************************************************************************/*********************************************o o/*****************电;序****************************************void1oopO{INl=l;o//左电机IN2=0;I N3=l;//右电机IN4=0;void runfFont{IN1=1;//左电机IN2=0;IN3=0;//右电机IN4=1;//小车后退函数void runbaC kIN1=O;//左电机I N2=l;I N3=1;//右电机IN4=0;//小车左转void run1eftQI Nl=0;//左电机IN2二0;IN3=0;//右电机IN4=1;//小车右转v oid ru n rightIN1=1;//左电机IN2=0;IN3=0;//右电机I N4=0;////小车左后转//v oidr u n ba ckleft//{//IN1=0;//左电机//IN2=0;//I N3=0;//右电机//IN4=1;//}〃//小车右后转//v oidrun ba ckrig h tQ//{//IN1=1;//左电机//IN2=0;//IN3=0;//右电机//IN4=0;//}//小车停止函数v Oid s t0p{INI=0;//左电机IN2=0;IN3=0;//右电机IN4=0;/********************************************************************************************************蓝牙:序************/*****************************************************************k*****************k*********voi d de1ayunsign ed in tauns i gned int b;fo r;a0;a--f o rb=3;bO;b—;//左电机转v Oid left_mo t or_runfrontuns i gned intaI Nl=l;//左电机IN2=0;delay a;IN1=O;IN2=0;del a y100-a;}v oid righ t_mo t or_runfro nt unsignedint aIN3=0;//右电机IN4=l;d eh ya;IN3=0;I N4=0;dcla y100-a;0v oid left_m ot or_back uns igned inta{IN1=O;//左电机IN2=1;de1a ya;I Nl=0;IN2=0;del a y100-a;3bbvoid right_motor_back unsignedintaIN3=1;^//右电机IN4=0;d elay a;IN3=0;IN4=0;delaylOO-a;®void lef t_moto r_stopO{INl=0y//左电机IN2=0;ovoid right_motor_s top Q1N3=0;//左电机IN4=0;0//PWM调速v oid PWM_R UNFRO NT un signedin ta,unsign edint b//a属于〜100lcft_mo tor_runfro nta;e rig ht_m otor_r unfr ontb;voi dPWM_RUNBACKu nsign ed inta,unsigne d int b°//a属于0〜100{le ft_motor_back a;b right_mot or_back b;0void PWM_R UNLEFTunsigned inta//a属于0〜100{righ t_mo tor_r unfr onta;left_mo tor_stopQ;v oidPWM_RUNRIGHTu nsigned inta°0°//a属于0〜100right_mot or_st opQ;3left_m otor_run fro nta;v0id bluestO pleft_m oto r_s top;right_mo tor_s top;v oid Usa r tC0n figurati onQSCON=0X50;b//设置为工作方式1b TMOD=TMOD|Ox21;//设置计数器工作方式2PCON=0X80;//波特率加倍TH1=0XFA;o//计数器初始值设置,注意波特率就就是4800得T Ll=0XFA;ES=1;°…//打开接收中断EA=1;e3//打开总中断TR1=1;TH0=0;TL0=0;ET0=l;//打开计数器/********************************************************************************函数名:Dela yunsign edinti延时函数*函数功能无*输入:无*输出*******************************************************************/v oidUsart0int errupt4{0re ceive Data=SBUF;//出去接收到得数据R I=0;//清除接收中断标志位if re ceiveData==0x660e1ow_v elo sity=1;o high_v elosity=0;//write_0x80;3i frecei veD ata==0x771ow_ve1os ity=0;high_ve1osity=1;}/xL*xL*sL**1*sL*sixsixsixxL**1*U-*1*sL-xlz*1*sixxL*xL**1*U-*JL«xL,wL.xL.xL*\Lsixsixsixslz*1*slz sixxlz»X*xL*sL*sixsL-slz*1**1**1*sixsixxL**1*xL«*1*xL«si-xL«xL*xL*/r r»r r\r\r rr\********************************/***********舵机程^序************I/*****************************************************************v oidde1ayxint tcharj;f or;tO;t--for尸19;jO;j--;}v oid d egre e03in ti;for i=0;i10;i++6{PWM=1;3delayx10;//1msPWM=0;d clayx390;//19msvo id degree90Q1nti;for i=0;i10;i++°PWM=1;6delayx24;//50PWM=0;del ayx376;//
18、5msvoid degre e180Q{einti;for i=0;i15;i++PWM=1;d elay x40;//I、5msb PWM=0;…delay x360;//185ms******************************************************************/*********************************************************************************************//****A键被按下后,前进*****/v oidCoun t1voi d{b time=TH1*256+TLl;TH1=O;TL1=O;b S=time*ls7/100;n++;3i fS=30n==1led1=0;stopQ;3degree0;di;66}ifS30n=二1•o ledl=1;3o runfrontO;n=0;66}ifn==2{s1eft=S;deg reel80Q;d iQ;}b ifn==3°3b s_right=S;o degree90;di;n=0;〃标志位清零eifs_left s_right633o1ed2=1;de1a ylms50;led2=0;delay1ms50;b r unrigh t;B6dela yx1250;°}3e1se°{1cd3=1;dclay800;lcd3=0;dclay500;run1eft;delayx1250;6}°}}/********超声波高电平脉冲宽度计算程序***************/voi dTime r_Co unt voideTRI=1//开启计数wh ile RX;//当RX为1计数并等待TR1=O;//关闭计数voi dSt artM oduleQ//启动模块//C ountl//计算TX=1;//启动一次模块De1ay1Ous2;TX=0;************************************************/*********************************************I******************y^****************************************************************************************************************void mainvoid UsartConfiguratio n;w hilelif k1==0n0==0b e{3delaylms20;oif k1==0000{b bn0++;o°}0}0else ifkl==0n0二二1606de1aylms20;if k1==000{n0—;}°}ifn0==0{ifKey_B=二0K ey_D==0Key_C==0Ke y_A==1//A键被按下bo{b fl=l;f2=0;f3=0;f4=0;di;00}o e1se ifIey_A==0I ey_D==0Iey_B==lIey_C==0®//B键被按下°fl=0;f2=l;f3=0;f4=O;diQ;el se ifKe y_B二二0Key_D==0Key—A==0Iey_C==1。
个人认证
优秀文档
获得点赞 0