基于51單片機的智能小車設計 藍牙+避障 YY編
單片機源程序如下:
- //************************************************************************
- // 名稱:基于51單片機的智能小車程序
- // 功能:1、藍牙模式 2、避障模式
- // 作者:YY
- // 時間:2017/10/25
- // 說明:上電后手機藍牙打開后搜索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.22.rar
(79.79 KB, 下載次數: 34)
2017-12-10 17:53 上傳
點擊文件名下載附件
|