藍牙控制小車的,藍牙是HC-06,功能是用藍牙控制小車前后左右和尋跡功能,現在功能能夠實現,但是在尋跡過程中好像并沒有調到速,我明明寫了PWM,有點奇怪
#include "reg52.h"
#include <intrins.h>
sbit IN1=P1^0; //定義驅動模塊IN1為P1.0 左上電機 M2 OUT1 OUT2
sbit IN2=P1^1; //定義驅動模塊IN2為P1.1
sbit IN3=P1^2; //定義驅動模塊IN3為P1.2 左下電機 M3 OUT3 OUT4
sbit IN4=P1^3; //定義驅動模塊IN4為P1.3
sbit IN5=P1^4; //定義驅動模塊IN5為P1.4 右上電機 M1 OUT5 OUT6
sbit IN6=P1^5; //定義驅動模塊IN6為P1.5
sbit IN7=P1^6; //定義驅動模塊IN7為P1.6 右下電機 M4 OUT7 OUT8
sbit IN8=P1^7; //定義驅動模塊IN8為P1.7
sbit left_led=P2^5; //定義左路尋跡為P2.5
sbit middle_led=P2^6; //定義中路尋跡為P2.6
sbit right_led=P2^7; //定義右路尋跡為P2.7
#define Left_moto_go {IN1=1,IN2=0,IN3=1,IN4=0;} //左邊兩個電機向前走
#define Left_moto_back {IN1=0,IN2=1,IN3=0,IN4=1;} //左邊兩個電機向后轉
#define Left_moto_Stop {IN1=0,IN2=0,IN3=0,IN4=0;} //左邊兩個電機停轉
#define Right_moto_go {IN5=1,IN6=0,IN7=1,IN8=0;} //右邊兩個電機向前走
#define Right_moto_back {IN5=0,IN6=1,IN7=0,IN8=1;} //右邊兩個電機向后轉
#define Right_moto_Stop {IN5=0,IN6=0,IN7=0,IN8=0;} //右邊兩個電機停轉
#define Left_moto_go1 {IN1=1,IN3=1;} //左邊兩個電機向前走
#define Left_moto_back1 {IN1=0,IN3=0;} //左邊兩個電機向后轉
#define Right_moto_go1 {IN5=1,IN7=1;} //右邊兩個電機向前走
#define Right_moto_back1 {IN5=0,IN7=0;} //右邊兩個電機向后走
#define M2_moto_pwm IN2 //PWM信號端 左上電機M2
#define M3_moto_pwm IN4 // 左下電機M3
#define M1_moto_pwm IN6 // 右上電機M1
#define M4_moto_pwm IN8 // 右下電機M4
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 timer0=0;
#define left 'C' //左轉
#define right 'D' //右轉
#define up 'A' //前進
#define down 'B' //后退
#define stop 'F' //停止
#define Car '1' //手機軟件1號鍵
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;
bit flag_xj =0;
unsigned char i=0;
unsigned char dat=0;
unsigned char buff[5]=0; //接收緩沖字節
//延時函數
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
//全速前進
void run(void)
{
Left_moto_go ; //左電機往前走
Right_moto_go ; //右電機往前走
}
//前速后退
void backrun(void)
{
Left_moto_back ; //左電機往前走
Right_moto_back ; //右電機往前走
}
//左轉
void leftrun(void)
{
Left_moto_back ; //左電機往前走
Right_moto_go ; //右電機往前走
}
//右轉
void rightrun(void)
{
Left_moto_go ; //左電機往前走
Right_moto_back ; //右電機往前走
}
//停止
void stoprun(void)
{
Left_moto_Stop ; //左電機往前走
Right_moto_Stop ; //右電機往前走
}
//PWM前進函數
void run_pwm(void)
{
push_val_left=7; //速度調節變量 0-9。。。9最小,0最大
push_val_right=7; //速度調節變量 0-9。。。9最小,0最大
Left_moto_go1 ; //左電機往前走
Right_moto_go1 ; //右電機往前走
}
//PWM后退函數
void backrun_pwm(void)
{
push_val_left=7; //速度調節變量 0-9。。。9最小,0最大
push_val_right=7; //速度調節變量 0-9。。。9最小,0最大
Left_moto_back1 ; //左電機往后走
Right_moto_back1 ; //右電機往后走
}
//PWM左轉函數
void leftrun_pwm(void)
{
push_val_left=7; //速度調節變量 0-9。。。9最小,0最大
push_val_right=7; //速度調節變量 0-9。。。9最小,0最大
Left_moto_Stop ; //左電機往前走
Right_moto_go1 ; //右電機往后走
}
//PWM右轉函數
void rightrun_pwm(void)
{
push_val_left=7; //減低一半速度 速度調節變量 0-9。。。9最小,0最大
push_val_right=7; //減低一半速度 速度調節變量 0-9。。。9最小,0最大
Left_moto_go1 ; //左電機往前走
Right_moto_Stop ; //右電機往后走
}
void xunji()
{
if(right_led==1&&middle_led==1&&left_led==1) //三路檢測到黑線
{
run_pwm();//前進
}
else
{
if(right_led==1&&middle_led==0&&left_led==0) //右邊檢測到黑線
{
rightrun_pwm();//右轉
}
if(right_led==0&&middle_led==0&&left_led==1) //左邊檢測到黑線
{
leftrun_pwm();//左轉
}
}
}
//左電機調速
void pwm_out_left_moto(void)
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
{
M2_moto_pwm=1; //M2的PWM
M3_moto_pwm=1; //M3的PWM
}
else
{
M2_moto_pwm=0;
M3_moto_pwm=0;
}
if(pwm_val_left>=10)
pwm_val_left=0;
}
else
{
M2_moto_pwm=0;
M3_moto_pwm=0;
}
}
//右電機調速
void pwm_out_right_moto(void)
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
{
M1_moto_pwm=1; //M1
M4_moto_pwm=1; //M4
}
else
{
M1_moto_pwm=0;
M4_moto_pwm=0;
}
if(pwm_val_right>=10)
pwm_val_right=0;
}
else
{
M1_moto_pwm=0;
M4_moto_pwm=0;
}
}
void Time0Config()
{
TMOD|= 0x01; //設置定時計數器工作方式1為定時器
//--定時器賦初始值,12MHZ下定時0.5ms--//
TH0 = 0xFF;
TL0 = 0x9C;
ET0 = 1; //開啟定時器1中斷
EA = 1;
TR0 = 1; //開啟定時器
}
void Time0(void) interrupt 1 //3 為定時器1的中斷號 1 定時器0的中斷號 0 外部中斷1 2 外部中斷2 4 串口中斷
{
TH0 = 0xFF;
TL0 = 0x9C;
timer0++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
//串口初始化函數
void init()
{
TMOD=0x20; //定時器T1工作模式2,8位自動重裝
TH1=0xF3; // 4800波特率
TL1=0xF3;
SCON=0x50;
PCON=0x80;
TR1=1; //啟動定時器T1
REN=1; //允許串口通訊
EA =1; //打開總中斷
ES =1; //串口中斷打開
}
//字符串發送函數
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 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 main(void)
{
TMOD|= 0x01; //設置定時計數器工作方式1為定時器
//--定時器賦初始值,12MHZ下定時0.5ms--//
TH0 = 0xFF;
TL0 = 0x9C;
ET0 = 1; //開啟定時器0中斷
TR1 = 1; //開啟定時器
TMOD=0x20; //定時器T1工作模式2,8位自動重裝
TH1=0xF3; // 4800波特率
TL1=0xF3;
SCON=0x50;
PCON=0x80;
TR1=1; //啟動定時器T1
REN=1; //允許串口通訊
EA =1; //打開總中斷
ES =1; //串口中斷打開
delay(100);
while(1) /*無限循環*/
{
while(flag_xj) //循線標志位
{
if(flag_REC==1)
{
stoprun();
break;
}
if(right_led==1&&middle_led==1&&left_led==1) //三路檢測到黑線
{
Right_moto_stop=1;
Left_moto_stop =1;
run_pwm(); //前進
}
else
{
if(right_led==1&&middle_led==0&&left_led==0) //右邊檢測到黑線
{
Right_moto_stop=1;
Left_moto_stop =1;
rightrun_pwm(); //右轉
}
if(right_led==0&&middle_led==0&&left_led==1) //左邊檢測到黑線
{
Right_moto_stop=1;
Left_moto_stop =1;
leftrun_pwm(); //左轉
}
}
}
if(flag_REC==1) //
{
flag_REC=0;
if(buff[0]=='O'&&buff[1]=='N') //第一個字節為O,第二個字節為N,第三個字節為控制碼
switch(buff[2])
{
case up : // 前進
send_str( );
flag_xj=0;
run();
break;
case down: // 后退
send_str1( );
flag_xj=0;
backrun();
break;
case left: // 左轉
send_str2( );
flag_xj=0;
leftrun();
break;
case right: // 右轉
send_str3( );
flag_xj=0;
rightrun();
break;
case stop: // 停止
send_str4( );
flag_xj=0;
stoprun();
break;
case Car : //
flag_xj=1;
break;
}
}
}
}
|