您好,欢迎访问三一刀客
#includereg51.H#includeintrins.h//#includelcd.hsbitTX=P2^7;sbitRX=P2^6;sbitPWM=P1^4;sbitBeep=P3^7;sbitled1=P3^3;sbitled2=P3^4;sbitled3=P3^5;/***********蓝牙切换按键**********/sbitk1=P3^6;/***自定义一些数据,注意这些数据对应“51智能车蓝牙无线遥控.exe”软件上的数据设置**/#defineleftdata0x11#definerightdata0x22#defineforwarddata0x33#definebackdata0x44#definestopdata0x55#defineucharunsignedchar#defineuintunsignedintunsignedcharreceiveData,high_velosity,low_velosity;voidUsartConfiguration();/***********电机端口定义************************/sbitIN1=P1^0;//为高电平时,左电机后转sbitIN2=P1^1;//为高电平时,左电机正转sbitIN3=P1^2;//为高电平时,右电机正转sbitIN4=P1^3;//为高电平时,右电机后转/**************无线遥控模块定义****************/sbitKey_A=P2^0;//B键信号端对应D0sbitKey_B=P2^1;//D键信号端对应D1sbitKey_C=P2^2;//C键信号端对应D2sbitKey_D=P2^3;//C键信号端voiddelay0(inta);voiddelay15us(void);voiddelay1ms(intz);voiddi();//蜂鸣器函数声明unsignedinttime;//用于存放定时器时间值unsignedintS;//用于存放距离的值charflag=0;//量程溢出标志位charf1,f2,f3,f4,n0;inta;intn;//运行次数判断标志ints_left,s_right;voidDelay10us(unsignedchari){unsignedcharj;do{j=10;do{_nop_();}while(--j);}while(--i);}/*********************************************************************************************//*****************电机程序********************//********************************************************************************************/voidloop(){IN1=1;//左电机IN2=0;IN3=1;//右电机IN4=0;}voidrunfront(){IN1=1;//左电机IN2=0;IN3=0;//右电机IN4=1;}//小车后退函数voidrunback(){IN1=0;//左电机IN2=1;IN3=1;//右电机IN4=0;}//小车左转voidrunleft(){IN1=0;//左电机IN2=0;IN3=0;//右电机IN4=1;}//小车右转voidrunright(){IN1=1;//左电机IN2=0;IN3=0;//右电机IN4=0;}////小车左后转//voidrunbackleft()//{//IN1=0;//左电机//IN2=0;//IN3=0;//右电机//IN4=1;//}////小车右后转//voidrunbackright()//{//IN1=1;//左电机//IN2=0;//IN3=0;//右电机//IN4=0;//}//小车停止函数voidstop(){IN1=0;//左电机IN2=0;IN3=0;//右电机IN4=0;}/*********************************************************************************************//***********蓝牙程序************//********************************************************************************************/voiddelay(unsignedinta){unsignedintb;for(;a0;a--)for(b=3;b0;b--);}//左电机转voidleft_motor_runfront(unsignedinta){IN1=1;//左电机IN2=0;delay(a);IN1=0;IN2=0;delay(100-a);}voidright_motor_runfront(unsignedinta){IN3=0;//右电机IN4=1;delay(a);IN3=0;IN4=0;delay(100-a);}voidleft_motor_back(unsignedinta){IN1=0;//左电机IN2=1;delay(a);IN1=0;IN2=0;delay(100-a);}voidright_motor_back(unsignedinta){IN3=1;//右电机IN4=0;delay(a);IN3=0;IN4=0;delay(100-a);}voidleft_motor_stop(){IN1=0;//左电机IN2=0;}voidright_motor_stop(){IN3=0;//左电机IN4=0;}//PWM调速voidPWM_RUNFRONT(unsignedinta,unsignedintb)//a属于0~100{left_motor_runfront(a);right_motor_runfront(b);}voidPWM_RUNBACK(unsignedinta,unsignedintb)//a属于0~100{left_motor_back(a);right_motor_back(b);}voidPWM_RUNLEFT(unsignedinta)//a属于0~100{right_motor_runfront(a);left_motor_stop();}voidPWM_RUNRIGHT(unsignedinta)//a属于0~100{right_motor_stop();left_motor_runfront(a);}voidbluestop(){left_motor_stop();right_motor_stop();}voidUsartConfiguration(){SCON=0X50;//设置为工作方式1TMOD=TMOD|0x21;//设置计数器工作方式2PCON=0X80;//波特率加倍TH1=0XFA;//计数器初始值设置,注意波特率是4800的TL1=0XFA;ES=1;//打开接收中断EA=1;//打开总中断TR1=1;TH0=0;TL0=0;ET0=1;//打开计数器}/********************************************************************************函数名:Delay(unsignedinti)*函数功能:延时函数*输入:无*输出:无*******************************************************************************/voidUsart()interrupt4{receiveData=SBUF;//出去接收到的数据RI=0;//清除接收中断标志位if(receiveData==0x66){low_velosity=1;high_velosity=0;//write_com(0x80);}if(receiveData==0x77){low_velosity=0;high_velosity=1;}}/*********************************************************************************************//***********舵机程序************//********************************************************************************************/voiddelayx(intt){charj;for(;t0;t--)for(j=19;j0;j--);}voiddegree0(){inti;for(i=0;i10;i++){PWM=1;delayx(10);//1msPWM=0;delayx(390);//19ms}}voiddegree90(){inti;for(i=0;i10;i++){PWM=1;delayx(24);//50PWM=0;delayx(376);//18.5ms}}voiddegree180(){inti;for(i=0;i15;i++){PWM=1;delayx(40);//1.5msPWM=0;delayx(360);//18.5ms}}/*********************************************************************************************//**************超声波程序**************//*********************************************************************************************//****A键被按下后,前进*****/voidCount1(void){time=TH1*256+TL1;TH1=0;TL1=0;S=(time*1.7)/100;n++;if(S=30&&n==1){led1=0;stop();degree0();di();}if(S30&&n==1){led1=1;runfront();n=0;}if(n==2){s_left=S;degree180();di();}if(n==3){s_right=S;degree90();di();n=0;//标志位清零if(s_lefts_right){led2=1;delay1ms(50);led2=0;delay1ms(50);runright();delayx(1250);}else{led3=1;delay(800);led3=0;delay(500);runleft();delayx(1250);}}}/********超声波高电平脉冲宽度计算程序***************/voidTimer_Count(void){TR1=1;//开启计数while(RX);//当RX为1计数并等待TR1=0;//关闭计数//Count1();//计算}/********************************************************/voidStartModule()//启动模块{TX=1;//启动一次模块Delay10us(2);TX=0;}/*********************************************************************************************//********************主程序***************
本文标题:智能小车源代码
链接地址:https://www.111doc.com/doc-7029865 .html