基于51单片机的智能小车设计 蓝牙+避障 源码。
单片机源程序如下:
- //************************************************************************
- // 名称:基于51单片机的智能小车程序
- // 功能:1、蓝牙模式 2、避障模式
- // 作者:YY
- // 说明:上电后手机蓝牙打开后搜索HC-06 配对密码为1234
- // 然后打开配套的Android软件 点击右上角的连接
- // 点击弹出的对话框 会提示 “ 连接成功 ” 就可以
- // 通过手机控制小车了 “ 1 ”模式切换 “ 2 ”增加车速档位“ 3 ”减小车速档位
- //************************************************************************
- #include <REGX52.H> //包含51单片机相关的头文件
- typedef unsigned int uint;
- typedef unsigned char uchar;
- /************************************************************************/
- /* PWM调制电机转速定义 */
- #define Left_moto_pwm P1_6 //接驱动模块ENA 使能端,输入PWM信号调节速度
- #define Right_moto_pwm P1_7 //接驱动模块ENB
- unsigned char pwm_val_left =0;//变量定义
- unsigned char push_val_left =0;// 左电机占空比N/10
- unsigned char pwm_val_right =0;
- unsigned char push_val_right=0;// 右电机占空比N/10
- bit Right_moto_stop=1;
- bit Left_moto_stop =1;
- unsigned int time=0;
- /************************************************************************/
- /* 小车接口定义 */
- #define Left_moto_go {P1_0=0,P1_1=1;} //P1_0 P1_1 接IN1 IN2 当 P1_0=0,P1_1=1; 时左电机前进
- #define Left_moto_back {P1_0=1,P1_1=0;} //P1_0 P1_1 接IN1 IN2 当 P1_0=1,P1_1=0; 时左电机后退
- #define Left_moto_t {P1_0=1,P1_1=1;} //P1_0 P3_5 接IN1 IN2 当 P1_0=1,P3_5=1; 时左电机停止
- #define Right_moto_go {P1_2=0,P1_3=1;} //P1_2 P1_3 接IN3 IN4 当 P1_2=0,P1_3=1; 时右电机前转
- #define Right_moto_back {P1_2=1,P1_3=0;} //P1_2 P1_3 接IN3 IN4 当 P1_2=1,P1_3=0; 时右电机后退
- #define Right_moto_t {P1_2=1,P1_3=1;} //P1_2 P1_3 接IN3 IN4 当 P1_2=1,P1_3=1; 时右电机停止
- #define ShowPort P0
- sbit LeftLed=P2^0; //定义左侧避障传感器接口 所用的传感器为 未感知障碍时为高电平 遇到障碍变为低电平
- sbit FontLed=P2^1; //定义前方避障传感器接口
- sbit RightLed=P2^2; //定义右侧避障传感器接口
- sbit Speker=P2^3; //定义喇叭接口
- sbit WEI1=P2^7; //定义显示接口
- sbit WEI2=P2^6;
- sbit WEI3=P2^5;
- sbit WEI4=P2^4;
- sbit LeftIR=P3^1; //定义前方左侧红外探头端口
- sbit FontIR=P3^2; //定义前方正前方红外探头端口
- sbit RightIR=P3^3; //定义前方右侧红外探头端口
- unsigned char temp = 1;
- unsigned char code LedShowModeData[]={0xC0,0xF9,0xA4};//0,1,2共阳数码管
- unsigned char code LedShowPwmData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0,1,2,3,4,5,6,7,8,9共阳数码管
- bit run_flag;
-
- /************************************************************************/
- /* 蓝牙定义 */
- #define left 'C'
- #define right 'D'
- #define up 'A'
- #define down 'B'
- #define stop 'F'
- #define switchmode'1'
- #define CarSpeedPlus '2'
- #define CarSpeedMinus '3'
- char code str[] = "收到指令,向前!\n";
- char code str1[] = "收到指令,向后!\n";
- char code str2[] = "收到指令,向左!\n";
- char code str3[] = "收到指令,向右!\n";
- char code str4[] = "收到指令,停止!\n";
- bit flag_REC=0;
- bit flag =0;
- unsigned char i=0;
- unsigned char dat=0;
- unsigned char buff[5]=0; //接收缓冲字节
- unsigned char temppwm=8;
- /************************************************************************/
- /* 延时程序 */
- void delay_nus(unsigned int i) //延时:i>=12 ,i的最小延时单12 us
- {
- i=i/10;
- while(--i);
- }
- void delay_nms(unsigned int n) //延时n ms
- {
- n=n+1;
- while(--n)
- delay_nus(900); //延时 1ms,同时进行补偿
-
- }
- /************************************************************************/
- /* 显示部分 */
- void Display()
- {
- if(temppwm<10)
- {
- ShowPort = LedShowModeData[temp];
- WEI1=0;
- WEI2=1;
- WEI3=1;
- WEI4=1;
- delay_nms(2);
- ShowPort = LedShowPwmData[temppwm];
- WEI1=1;
- WEI2=1;
- WEI3=1;
- WEI4=0;
- delay_nms(2);
- }
- else
- {
- ShowPort = LedShowModeData[temp];
- WEI1=0;
- WEI2=1;
- WEI3=1;
- WEI4=1;
- delay_nms(1);
- ShowPort = LedShowPwmData[1];
- WEI1=1;
- WEI2=1;
- WEI3=0;
- WEI4=1;
- delay_nms(1);
- ShowPort = LedShowPwmData[0];
- WEI1=1;
- WEI2=1;
- WEI3=1;
- WEI4=0;
- delay_nms(1);
- }
- }
- /************************************************************************/
- /* 控制方向程序 */
- void tingzhi()
- {
- push_val_left =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_t ; //左电机停止
- Right_moto_t ; //右电机停止
- }
- void qianjin()
- {
- push_val_left =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_go ; //左电机前进
- Right_moto_go ; //右电机前进
- }
- void houtui()
- {
- push_val_left =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_back ; //左电机后退
- Right_moto_back ; //右电机后退
- }
- void zuozhuan()
- {
- push_val_left =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_back ; //左电机后退
- Right_moto_go ; //右电机前进
- }
- void weizuozhuan()
- {
- push_val_left =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_back ;
- Right_moto_go ;
- }
- void weiyouzhuan()
- {
- push_val_left =1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_go ;
- Right_moto_back ;
- }
- void youzhuan()
- {
- push_val_left =1; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =4; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_go ; //左电机前进
- Right_moto_back ; //右电机后退
- }
- /************************************************************************/
- /* PWM调制电机转速 */
- void Timer0_Init()
- {
- TMOD=0X01;
- TH0= 0XF8; //1ms定时
- TL0= 0X30;
- TR0= 1;
- ET0= 1;
- EA = 1;
- }
- /************************************************************************/
- /* 左电机调速 */
- /*调节push_val_left的值改变电机转速,占空比 */
- void pwm_out_left_moto(void)
- {
- if(Left_moto_stop)
- {
- if(pwm_val_left <= push_val_left)
- Left_moto_pwm=1;
- else
- Left_moto_pwm=0;
- if(pwm_val_left>=10)
- pwm_val_left=0;
- }
- else Left_moto_pwm=0;
- }
- /******************************************************************/
- /* 右电机调速 */
- void pwm_out_right_moto(void)
- {
- if(Right_moto_stop)
- {
- if(pwm_val_right<=push_val_right)
- Right_moto_pwm=1;
- else
- Right_moto_pwm=0;
- if(pwm_val_right>=10)
- pwm_val_right=0;
- }
- else Right_moto_pwm=0;
- }
- /***************************************************/
- ///*TIMER0中断服务子函数产生PWM信号*/
- void timer0()interrupt 1 using 2
- {
- TH0=0XF8; //1Ms定时
- TL0=0X30;
- time++;
- pwm_val_left++;
- pwm_val_right++;
- pwm_out_left_moto();
- pwm_out_right_moto();
-
- }
- /************************************************************************/
- /* 寻迹程序 */
- void xunji()
- {
- Start:
- if(LeftIR == 0 && FontIR == 1 && RightIR == 0)
- {
- qianjin();
- delay_nms (100);
- goto NextRun;
- }
-
- if(LeftIR == 1 && FontIR == 1 && RightIR == 0)
- {
- weiyouzhuan(); //微右调
- delay_nms (100);
- goto NextRun;
- }
-
- if(LeftIR == 1 && FontIR == 0 && RightIR == 0)
- {
- youzhuan(); //右急转弯
- delay_nms (100);
- goto NextRun;
- }
- if(LeftIR == 0 && FontIR == 1 && RightIR == 1)
- {
- weizuozhuan(); //微左调
- delay_nms (100);
- goto NextRun;
- }
- if(LeftIR == 0 && FontIR == 0 && RightIR == 1)
- {
- zuozhuan(); //左急转弯
- delay_nms (100);
- goto NextRun;
- }
- goto Start;
- NextRun:
- tingzhi();
- }
- /************************************************************************/
- /* 避障程序 */
- void bizhang()
- {
- static uchar i;
- run_flag=1;
- qianjin();
- if(LeftLed == 0) //如果前面避障传感器检测到障碍物
- {
- tingzhi(); //停止
- for(i=0;i<50;i++) //停止300MS 防止电机反相电压冲击 导致系统复位
- {
- delay_nms(3);
- Display();
- }
- houtui(); //后退
- // delay_nms (500); //后退1500MS
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- zuozhuan(); //
- // delay_nms (500);
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- }
-
- else if(FontLed == 0) //如果前面避障传感器检测到障碍物
- {
- tingzhi(); //停止
- // delay_nms (300); //停止300MS 防止电机反相电压冲击 导致系统复位
- for(i=0;i<50;i++)
- {
- delay_nms(3);
- Display();
- }
- houtui(); //后退
- // delay_nms (500); //后退1500MS
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- zuozhuan(); //
- // delay_nms (500);
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- }
- else if(RightLed == 0) //如果前面避障传感器检测到障碍物
- {
- tingzhi(); //停止
- // delay_nms (300); //停止300MS 防止电机反相电压冲击 导致系统复位
- for(i=0;i<50;i++)
- {
- delay_nms(3);
- Display();
- }
- houtui(); //后退
- // delay_nms (500); //后退1500MS
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- youzhuan(); //
- // delay_nms (500);
- for(i=0;i<80;i++)
- {
- delay_nms(5);
- Display();
- }
- }
- }
- /************************************************************************/
- /* 蓝牙部分 */
- /************************************************************************/
- //字符串发送函数
- void send_str( )
- // 传送字串
- {
- unsigned char i = 0;
- while(str[i] != '\0')
- {
- SBUF = str[i];
- while(!TI); // 等特数据传送
- TI = 0; // 清除数据传送标志
- i++; // 下一个字符
- }
- }
- void send_str1( )
- // 传送字串
- {
- unsigned char i = 0;
- while(str1[i] != '\0')
- {
- SBUF = str1[i];
- while(!TI); // 等特数据传送
- TI = 0; // 清除数据传送标志
- i++; // 下一个字符
- }
- }
- void send_str2( )
- // 传送字串
- {
- unsigned char i = 0;
- while(str2[i] != '\0')
- {
- SBUF = str2[i];
- while(!TI); // 等特数据传送
- TI = 0; // 清除数据传送标志
- i++; // 下一个字符
- }
- }
-
- void send_str3()
- // 传送字串
- {
- unsigned char i = 0;
- while(str3[i] != '\0')
- {
- SBUF = str3[i];
- while(!TI); // 等特数据传送
- TI = 0; // 清除数据传送标志
- i++; // 下一个字符
- }
- }
- void send_str4()
- // 传送字串
- {
- unsigned char i = 0;
- while(str4[i] != '\0')
- {
- SBUF = str4[i];
- while(!TI); // 等特数据传送
- TI = 0; // 清除数据传送标志
- i++; // 下一个字符
- }
- }
- void Timer1_Init()
- {
- TMOD=0x20;
- TH1=0xFd; //11.0592M晶振,9600波特率
- TL1=0xFd;
- SCON=0x50;
- PCON=0x00;
- TR1=1;
- ES=1;
- EA=1;
- }
- void sint() interrupt 4 //中断接收3个字节
- {
- if(RI) //是否接收中断
- {
- RI=0;
- dat=SBUF;
- if(dat=='O'&&(i==0)) //接收数据第一帧
- {
- buff[i]=dat;
- flag=1; //开始接收数据
- }
- else
- if(flag==1)
- {
- i++;
- buff[i]=dat;
- if(i>=2)
- {i=0;flag=0;flag_REC=1 ;} // 停止接收
- }
- }
- }
- void lanya()
- {
-
- if(flag_REC==1) //
- {
- flag_REC=0;
- if(buff[0]=='O'&&buff[1]=='N') //第一个字节为O,第二个字节为N,第三个字节为控制码
- switch(buff[2])
- {
- case up : // 前进
- send_str();
- push_val_left =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_go ; //左电机前进
- Right_moto_go ; //右电机前进
- break;
-
- case down: // 后退
- send_str1();
- push_val_left =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_back ;
- Right_moto_back ;
- break;
-
- case left: // 左转
- send_str3();
- push_val_left =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_back ;
- Right_moto_go ;
-
- break;
-
- case right: // 右转
- send_str2();
- push_val_left =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =temppwm; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_go ;
- Right_moto_back ;
- break;
-
- case stop: // 停止
- send_str4();
- push_val_left =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- push_val_right =2; //PWM 调节参数1-10 1为最慢,10是最快 改这个值可以改变其速度
- Left_moto_t ;
- Right_moto_t ;
- break;
-
- case switchmode : // 切换工作模式
- temp++;
- Speker=~Speker;
- delay_nms(10);
- Speker=~Speker;
- break;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
基于51单片机的智能小车设计 蓝牙 避障 YY编写 2017.11.13.zip
(49.29 KB, 下载次数: 55)
|