標(biāo)題:
基于51單片機(jī)的智能小車設(shè)計(jì) 藍(lán)牙+避障源碼
[打印本頁]
作者:
123aaaq
時(shí)間:
2018-4-2 19:14
標(biāo)題:
基于51單片機(jī)的智能小車設(shè)計(jì) 藍(lán)牙+避障源碼
基于51單片機(jī)的智能小車設(shè)計(jì) 藍(lán)牙+避障 源碼。
單片機(jī)源程序如下:
//************************************************************************
// 名稱:基于51單片機(jī)的智能小車程序
// 功能:1、藍(lán)牙模式 2、避障模式
// 作者:YY
// 說明:上電后手機(jī)藍(lán)牙打開后搜索HC-06 配對密碼為1234
// 然后打開配套的Android軟件 點(diǎn)擊右上角的連接
// 點(diǎn)擊彈出的對話框 會(huì)提示 “ 連接成功 ” 就可以
// 通過手機(jī)控制小車了 “ 1 ”模式切換 “ 2 ”增加車速檔位“ 3 ”減小車速檔位
//************************************************************************
#include <REGX52.H> //包含51單片機(jī)相關(guān)的頭文件
typedef unsigned int uint;
typedef unsigned char uchar;
/************************************************************************/
/* PWM調(diào)制電機(jī)轉(zhuǎn)速定義 */
#define Left_moto_pwm P1_6 //接驅(qū)動(dòng)模塊ENA 使能端,輸入PWM信號調(diào)節(jié)速度
#define Right_moto_pwm P1_7 //接驅(qū)動(dòng)模塊ENB
unsigned char pwm_val_left =0;//變量定義
unsigned char push_val_left =0;// 左電機(jī)占空比N/10
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;// 右電機(jī)占空比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 當(dāng) P1_0=0,P1_1=1; 時(shí)左電機(jī)前進(jìn)
#define Left_moto_back {P1_0=1,P1_1=0;} //P1_0 P1_1 接IN1 IN2 當(dāng) P1_0=1,P1_1=0; 時(shí)左電機(jī)后退
#define Left_moto_t {P1_0=1,P1_1=1;} //P1_0 P3_5 接IN1 IN2 當(dāng) P1_0=1,P3_5=1; 時(shí)左電機(jī)停止
#define Right_moto_go {P1_2=0,P1_3=1;} //P1_2 P1_3 接IN3 IN4 當(dāng) P1_2=0,P1_3=1; 時(shí)右電機(jī)前轉(zhuǎn)
#define Right_moto_back {P1_2=1,P1_3=0;} //P1_2 P1_3 接IN3 IN4 當(dāng) P1_2=1,P1_3=0; 時(shí)右電機(jī)后退
#define Right_moto_t {P1_2=1,P1_3=1;} //P1_2 P1_3 接IN3 IN4 當(dāng) P1_2=1,P1_3=1; 時(shí)右電機(jī)停止
#define ShowPort P0
sbit LeftLed=P2^0; //定義左側(cè)避障傳感器接口 所用的傳感器為 未感知障礙時(shí)為高電平 遇到障礙變?yōu)榈碗娖?br />
sbit FontLed=P2^1; //定義前方避障傳感器接口
sbit RightLed=P2^2; //定義右側(cè)避障傳感器接口
sbit Speker=P2^3; //定義喇叭接口
sbit WEI1=P2^7; //定義顯示接口
sbit WEI2=P2^6;
sbit WEI3=P2^5;
sbit WEI4=P2^4;
sbit LeftIR=P3^1; //定義前方左側(cè)紅外探頭端口
sbit FontIR=P3^2; //定義前方正前方紅外探頭端口
sbit RightIR=P3^3; //定義前方右側(cè)紅外探頭端口
unsigned char temp = 1;
unsigned char code LedShowModeData[]={0xC0,0xF9,0xA4};//0,1,2共陽數(shù)碼管
unsigned char code LedShowPwmData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0,1,2,3,4,5,6,7,8,9共陽數(shù)碼管
bit run_flag;
/************************************************************************/
/* 藍(lán)牙定義 */
#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; //接收緩沖字節(jié)
unsigned char temppwm=8;
/************************************************************************/
/* 延時(shí)程序 */
void delay_nus(unsigned int i) //延時(shí):i>=12 ,i的最小延時(shí)單12 us
{
i=i/10;
while(--i);
}
void delay_nms(unsigned int n) //延時(shí)n ms
{
n=n+1;
while(--n)
delay_nus(900); //延時(shí) 1ms,同時(shí)進(jìn)行補(bǔ)償
}
/************************************************************************/
/* 顯示部分 */
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 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =2; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_t ; //左電機(jī)停止
Right_moto_t ; //右電機(jī)停止
}
void qianjin()
{
push_val_left =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_go ; //左電機(jī)前進(jìn)
Right_moto_go ; //右電機(jī)前進(jìn)
}
void houtui()
{
push_val_left =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_back ; //左電機(jī)后退
Right_moto_back ; //右電機(jī)后退
}
void zuozhuan()
{
push_val_left =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =1; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_back ; //左電機(jī)后退
Right_moto_go ; //右電機(jī)前進(jìn)
}
void weizuozhuan()
{
push_val_left =2; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =1; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_back ;
Right_moto_go ;
}
void weiyouzhuan()
{
push_val_left =1; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =2; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_go ;
Right_moto_back ;
}
void youzhuan()
{
push_val_left =1; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =4; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_go ; //左電機(jī)前進(jìn)
Right_moto_back ; //右電機(jī)后退
}
/************************************************************************/
/* PWM調(diào)制電機(jī)轉(zhuǎn)速 */
void Timer0_Init()
{
TMOD=0X01;
TH0= 0XF8; //1ms定時(shí)
TL0= 0X30;
TR0= 1;
ET0= 1;
EA = 1;
}
/************************************************************************/
/* 左電機(jī)調(diào)速 */
/*調(diào)節(jié)push_val_left的值改變電機(jī)轉(zhuǎn)速,占空比 */
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;
}
/******************************************************************/
/* 右電機(jī)調(diào)速 */
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中斷服務(wù)子函數(shù)產(chǎn)生PWM信號*/
void timer0()interrupt 1 using 2
{
TH0=0XF8; //1Ms定時(shí)
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(); //微右調(diào)
delay_nms (100);
goto NextRun;
}
if(LeftIR == 1 && FontIR == 0 && RightIR == 0)
{
youzhuan(); //右急轉(zhuǎn)彎
delay_nms (100);
goto NextRun;
}
if(LeftIR == 0 && FontIR == 1 && RightIR == 1)
{
weizuozhuan(); //微左調(diào)
delay_nms (100);
goto NextRun;
}
if(LeftIR == 0 && FontIR == 0 && RightIR == 1)
{
zuozhuan(); //左急轉(zhuǎn)彎
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 防止電機(jī)反相電壓沖擊 導(dǎo)致系統(tǒng)復(fù)位
{
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 防止電機(jī)反相電壓沖擊 導(dǎo)致系統(tǒng)復(fù)位
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 防止電機(jī)反相電壓沖擊 導(dǎo)致系統(tǒng)復(fù)位
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();
}
}
}
/************************************************************************/
/* 藍(lán)牙部分 */
/************************************************************************/
//字符串發(fā)送函數(shù)
void send_str( )
// 傳送字串
{
unsigned char i = 0;
while(str[i] != '\0')
{
SBUF = str[i];
while(!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++; // 下一個(gè)字符
}
}
void send_str1( )
// 傳送字串
{
unsigned char i = 0;
while(str1[i] != '\0')
{
SBUF = str1[i];
while(!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++; // 下一個(gè)字符
}
}
void send_str2( )
// 傳送字串
{
unsigned char i = 0;
while(str2[i] != '\0')
{
SBUF = str2[i];
while(!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++; // 下一個(gè)字符
}
}
void send_str3()
// 傳送字串
{
unsigned char i = 0;
while(str3[i] != '\0')
{
SBUF = str3[i];
while(!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++; // 下一個(gè)字符
}
}
void send_str4()
// 傳送字串
{
unsigned char i = 0;
while(str4[i] != '\0')
{
SBUF = str4[i];
while(!TI); // 等特?cái)?shù)據(jù)傳送
TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志
i++; // 下一個(gè)字符
}
}
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個(gè)字節(jié)
{
if(RI) //是否接收中斷
{
RI=0;
dat=SBUF;
if(dat=='O'&&(i==0)) //接收數(shù)據(jù)第一幀
{
buff[i]=dat;
flag=1; //開始接收數(shù)據(jù)
}
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') //第一個(gè)字節(jié)為O,第二個(gè)字節(jié)為N,第三個(gè)字節(jié)為控制碼
switch(buff[2])
{
case up : // 前進(jìn)
send_str();
push_val_left =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_go ; //左電機(jī)前進(jìn)
Right_moto_go ; //右電機(jī)前進(jìn)
break;
case down: // 后退
send_str1();
push_val_left =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_back ;
Right_moto_back ;
break;
case left: // 左轉(zhuǎn)
send_str3();
push_val_left =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_back ;
Right_moto_go ;
break;
case right: // 右轉(zhuǎn)
send_str2();
push_val_left =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =temppwm; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_go ;
Right_moto_back ;
break;
case stop: // 停止
send_str4();
push_val_left =2; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
push_val_right =2; //PWM 調(diào)節(jié)參數(shù)1-10 1為最慢,10是最快 改這個(gè)值可以改變其速度
Left_moto_t ;
Right_moto_t ;
break;
case switchmode : // 切換工作模式
temp++;
Speker=~Speker;
delay_nms(10);
Speker=~Speker;
break;
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
基于51單片機(jī)的智能小車設(shè)計(jì) 藍(lán)牙 避障 YY編寫 2017.11.13.zip
(49.29 KB, 下載次數(shù): 55)
2018-4-2 19:14 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
晴晴520
時(shí)間:
2019-5-1 21:28
if(dat=='O'&&(i==0)) //接收數(shù)據(jù)第一幀
為啥是O呢?
作者:
ycs89554171
時(shí)間:
2020-5-20 17:09
好資料,51黑有你更精彩
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1