您的位置:首頁>正文

手把手教你做藍牙小車(模組化搭建)

準備材料

小車底盤

電機驅動

藍牙模組HC-06

12V鋰電池(供電)

單片機最小系統

藍牙小車安卓app

電路連接簡介

電機驅動:8個控制端接51單片機P0口, 驅動PWM使能端接P2.0-P2.3。

因為電機驅動上有5V輸出電壓所以可以恰好給單片機供電, 所以很好的解決了單片機要與驅動共地的問題。

藍牙模組:藍牙模組的RXD接單片機的P3.1, 藍牙模組的TXD接單片機的P3.0.藍牙模組的電源接單片機的電源。

獲得手機發送的資料

用PL2303燒寫器和藍牙相連, 接法和單片機相連的接法一樣。

TXD-RXD

RXD-TXD

注意電源別接反哦!!如果不行就再反過來接!!總有一個順序可以

沒有PL2303也可以用單片機開發板。

看看眼熟不燒寫軟體上面有串口助手哦!功能強大吧!!!

燒寫軟體自帶串口助手,打開串口,

埠怎麼找?

右鍵電腦-裝置管理員-埠

手機連接上藍牙發資料,資料出來了吧!!

注意點

1.一開始最小系統的晶振是12Mhz可能導致系統不正常,建議使用11.0592的晶振呀!

2.調小車輪子轉動的方向一定要一個一個調,切記!切記!

3.最後就是程式,調的時候一個功能一個功能的實現,沒有一下子都把程式寫出來的,因為涉及到串口,有必要還要編寫一個發送串口資料的子函數,這樣在電腦上可以隨時看到你發送的資料!

程式

#include "reg51.h"

sbit PWM1 = P2^0;//右後輪電機驅動使能

sbit PWM2 = P2^1;//左後輪電機驅動使能

sbit PWM3 = P2^2;//右前輪電機驅動使能

sbit PWM4 = P2^3;//左前輪電機驅動使能

sbit motor_control_1 = P0^0;//右後輪後退

sbit motor_control_2 = P0^1;//右後輪前進

sbit motor_control_3 = P0^2;//左後輪後退

sbit motor_control_4 = P0^3;//左後輪前進

sbit motor_control_5 = P0^4;//右前輪後退

sbit motor_control_6 = P0^5;//右前輪前進

sbit motor_control_7 = P0^6;//左前輪後退

sbit motor_control_8 = P0^7;//左前輪前進

unsigned char ucBluetoothData = 230;//設置初始速度最大值為255最小為125

unsigned char ucLock = 0;//互斥量,俗稱原子鎖

unsigned int uiPWMCnt1 = 0;

unsigned int uiPWM1 = 230;

unsigned int uiPWMCnt2 = 0;

unsigned int uiPWM2 = 230;

unsigned int uiPWMCnt3 = 0;

unsigned int uiPWM3 = 230;

unsigned int uiPWMCnt4 = 0;

unsigned int uiPWM4 = 230;

unsigned char ucTempPWM;//設置中間變數

void initial_myself();

void initial_peripheral();

void T0_time();

void usart_service(void);

//void usart_send(unsigned char ucSendData);

void delay_long(unsigned int uiDelayLong);

void go_forward(void);//前進

void fall_back(void);//後退

void turn_left(void);//左轉

void turn_right(void);//右轉

void stop();//刹車

void main()

{

initial_myself();

delay_long(100);

initial_peripheral();

while(1)

{

usart_service();

// delay_long(500);

}

}

//串口服務函數

void usart_service()

{

switch(ucBluetoothData)

{

case 0x04://前進

ucBluetoothData = 0x02;//避免一直觸發

go_forward();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x05://左轉

ucBluetoothData = 0x02;//避免一直觸發

turn_left();

ucLock = 1;

uiPWM2 = uiPWM4= ucTempPWM / 4;

uiPWM3 =uiPWM1 = ucTempPWM;

ucLock = 0;

break;

case 0x06://右轉

ucBluetoothData = 0x02;//避免一直觸發

turn_right();

ucLock = 1;

uiPWM2 = uiPWM4 = ucTempPWM;

uiPWM3 =uiPWM1 = ucTempPWM / 4;

ucLock = 0;

break;

case 0x07://後退

ucBluetoothData = 0x02;//避免一直觸發

fall_back();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x01:

ucBluetoothData = 0x02;//避免一直觸發

stop();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x00:

ucBluetoothData = 0x02;//避免一直觸發

stop();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

default :

break;

}

delay_long(100);

// usart_send(ucBluetoothData);

//速度調節的資料

if(ucBluetoothData!=0x00&&ucBluetoothData!=0x01 && ucBluetoothData!=0x02 && ucBluetoothData!=0x04 && ucBluetoothData!=0x05 && ucBluetoothData!=0x06 && ucBluetoothData!=0x07)

{

ucLock = 1;

ucTempPWM = uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucBluetoothData;

ucLock = 0;

}

}

void T0_time() interrupt 1

{

TF0 = 0;//清除中斷標誌

TR0 = 0;//關計時器

uiPWMCnt1 ++;

uiPWMCnt2 ++;

uiPWMCnt3 ++;

uiPWMCnt4 ++;

if(ucLock == 0)

{

if(uiPWMCnt1 > 255)

{

uiPWMCnt1 = 0;

}

if(uiPWMCnt1

{

PWM1 = 1;

}

else

{

PWM1 = 0;

}

if(uiPWMCnt2 > 255)

{

uiPWMCnt2 = 0;

}

if(uiPWMCnt2

{

PWM2 = 1;

}

else

{

PWM2 = 0;

}

if(uiPWMCnt3 > 255)

{

uiPWMCnt3 = 0;

}

if(uiPWMCnt3

{

PWM3 = 1;

}

else

{

PWM3 = 0;

}

if(uiPWMCnt4 > 255)

{

uiPWMCnt4 = 0;

}

if(uiPWMCnt4

{

PWM4 = 1;

}

else

{

PWM4 = 0;

}

}

TH0 = 0xff;

TL0 = 0x28;

TR0 = 1;///開計時器

}

void initial_myself()

{

TMOD = 0x01;//設置計時器0為工作方式1

TH0 = 0xff;

TL0 = 0x28;

//配置串口

SCON = 0x50;

TMOD = 0x21;

TH1 = TL1 = -(11095200L/12/32/9600);

IP = 0x10;

stop();

PWM1 = 1;

PWM2 = 1;

PWM3 = 1;

PWM4 = 1;

}

void initial_peripheral()

{

EA = 1;//開總中斷

ES = 1;//允許串口中斷

ET0 = 1;//允許計時器中斷

TR0 = 1;//啟動計時器

TR1 = 1;//

}

void usart_receive(void) interrupt 4

{

if(RI == 1)

{

RI = 0;

ucBluetoothData = SBUF;

// uiSendCnt = 0;

}

else

{

TI = 0;

}

}

//void usart_send(unsigned char ucSendData)

//{

// ES = 0;

// TI = 0;

// SBUF = ucSendData;

// TI = 0;

// ES = 1;

//

//}

void delay_long(unsigned int uiDelayLong)

{

unsigned int i;

unsigned int j;

for(i = 0 ; i

{

for(j = 0; j

}

}

void stop()//停止

{

motor_control_1 = 0;

motor_control_2 = 0;

motor_control_3 = 0;

motor_control_4 = 0;

motor_control_5 = 0;

motor_control_6 = 0;

motor_control_7 = 0;

motor_control_8 = 0;

}

void fall_back()

{

motor_control_1 = 1;

motor_control_2 = 0;

motor_control_3 = 1;

motor_control_4 = 0;

motor_control_5 = 1;

motor_control_6 = 0;

motor_control_7 = 1;

motor_control_8 = 0;

}

void go_forward()

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

void turn_left()//左轉

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

void turn_right()//右轉

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

本文素材來自電子發燒友論壇。

【 】單片機C語言程式設計實訓100例:基於8051+Proteus模擬(第2版) ¥55.1 購買

燒寫軟體自帶串口助手,打開串口,

埠怎麼找?

右鍵電腦-裝置管理員-埠

手機連接上藍牙發資料,資料出來了吧!!

注意點

1.一開始最小系統的晶振是12Mhz可能導致系統不正常,建議使用11.0592的晶振呀!

2.調小車輪子轉動的方向一定要一個一個調,切記!切記!

3.最後就是程式,調的時候一個功能一個功能的實現,沒有一下子都把程式寫出來的,因為涉及到串口,有必要還要編寫一個發送串口資料的子函數,這樣在電腦上可以隨時看到你發送的資料!

程式

#include "reg51.h"

sbit PWM1 = P2^0;//右後輪電機驅動使能

sbit PWM2 = P2^1;//左後輪電機驅動使能

sbit PWM3 = P2^2;//右前輪電機驅動使能

sbit PWM4 = P2^3;//左前輪電機驅動使能

sbit motor_control_1 = P0^0;//右後輪後退

sbit motor_control_2 = P0^1;//右後輪前進

sbit motor_control_3 = P0^2;//左後輪後退

sbit motor_control_4 = P0^3;//左後輪前進

sbit motor_control_5 = P0^4;//右前輪後退

sbit motor_control_6 = P0^5;//右前輪前進

sbit motor_control_7 = P0^6;//左前輪後退

sbit motor_control_8 = P0^7;//左前輪前進

unsigned char ucBluetoothData = 230;//設置初始速度最大值為255最小為125

unsigned char ucLock = 0;//互斥量,俗稱原子鎖

unsigned int uiPWMCnt1 = 0;

unsigned int uiPWM1 = 230;

unsigned int uiPWMCnt2 = 0;

unsigned int uiPWM2 = 230;

unsigned int uiPWMCnt3 = 0;

unsigned int uiPWM3 = 230;

unsigned int uiPWMCnt4 = 0;

unsigned int uiPWM4 = 230;

unsigned char ucTempPWM;//設置中間變數

void initial_myself();

void initial_peripheral();

void T0_time();

void usart_service(void);

//void usart_send(unsigned char ucSendData);

void delay_long(unsigned int uiDelayLong);

void go_forward(void);//前進

void fall_back(void);//後退

void turn_left(void);//左轉

void turn_right(void);//右轉

void stop();//刹車

void main()

{

initial_myself();

delay_long(100);

initial_peripheral();

while(1)

{

usart_service();

// delay_long(500);

}

}

//串口服務函數

void usart_service()

{

switch(ucBluetoothData)

{

case 0x04://前進

ucBluetoothData = 0x02;//避免一直觸發

go_forward();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x05://左轉

ucBluetoothData = 0x02;//避免一直觸發

turn_left();

ucLock = 1;

uiPWM2 = uiPWM4= ucTempPWM / 4;

uiPWM3 =uiPWM1 = ucTempPWM;

ucLock = 0;

break;

case 0x06://右轉

ucBluetoothData = 0x02;//避免一直觸發

turn_right();

ucLock = 1;

uiPWM2 = uiPWM4 = ucTempPWM;

uiPWM3 =uiPWM1 = ucTempPWM / 4;

ucLock = 0;

break;

case 0x07://後退

ucBluetoothData = 0x02;//避免一直觸發

fall_back();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x01:

ucBluetoothData = 0x02;//避免一直觸發

stop();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

case 0x00:

ucBluetoothData = 0x02;//避免一直觸發

stop();

ucLock = 1;

uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucTempPWM;

ucLock = 0;

break;

default :

break;

}

delay_long(100);

// usart_send(ucBluetoothData);

//速度調節的資料

if(ucBluetoothData!=0x00&&ucBluetoothData!=0x01 && ucBluetoothData!=0x02 && ucBluetoothData!=0x04 && ucBluetoothData!=0x05 && ucBluetoothData!=0x06 && ucBluetoothData!=0x07)

{

ucLock = 1;

ucTempPWM = uiPWM1 = uiPWM2 = uiPWM3 = uiPWM4 = ucBluetoothData;

ucLock = 0;

}

}

void T0_time() interrupt 1

{

TF0 = 0;//清除中斷標誌

TR0 = 0;//關計時器

uiPWMCnt1 ++;

uiPWMCnt2 ++;

uiPWMCnt3 ++;

uiPWMCnt4 ++;

if(ucLock == 0)

{

if(uiPWMCnt1 > 255)

{

uiPWMCnt1 = 0;

}

if(uiPWMCnt1

{

PWM1 = 1;

}

else

{

PWM1 = 0;

}

if(uiPWMCnt2 > 255)

{

uiPWMCnt2 = 0;

}

if(uiPWMCnt2

{

PWM2 = 1;

}

else

{

PWM2 = 0;

}

if(uiPWMCnt3 > 255)

{

uiPWMCnt3 = 0;

}

if(uiPWMCnt3

{

PWM3 = 1;

}

else

{

PWM3 = 0;

}

if(uiPWMCnt4 > 255)

{

uiPWMCnt4 = 0;

}

if(uiPWMCnt4

{

PWM4 = 1;

}

else

{

PWM4 = 0;

}

}

TH0 = 0xff;

TL0 = 0x28;

TR0 = 1;///開計時器

}

void initial_myself()

{

TMOD = 0x01;//設置計時器0為工作方式1

TH0 = 0xff;

TL0 = 0x28;

//配置串口

SCON = 0x50;

TMOD = 0x21;

TH1 = TL1 = -(11095200L/12/32/9600);

IP = 0x10;

stop();

PWM1 = 1;

PWM2 = 1;

PWM3 = 1;

PWM4 = 1;

}

void initial_peripheral()

{

EA = 1;//開總中斷

ES = 1;//允許串口中斷

ET0 = 1;//允許計時器中斷

TR0 = 1;//啟動計時器

TR1 = 1;//

}

void usart_receive(void) interrupt 4

{

if(RI == 1)

{

RI = 0;

ucBluetoothData = SBUF;

// uiSendCnt = 0;

}

else

{

TI = 0;

}

}

//void usart_send(unsigned char ucSendData)

//{

// ES = 0;

// TI = 0;

// SBUF = ucSendData;

// TI = 0;

// ES = 1;

//

//}

void delay_long(unsigned int uiDelayLong)

{

unsigned int i;

unsigned int j;

for(i = 0 ; i

{

for(j = 0; j

}

}

void stop()//停止

{

motor_control_1 = 0;

motor_control_2 = 0;

motor_control_3 = 0;

motor_control_4 = 0;

motor_control_5 = 0;

motor_control_6 = 0;

motor_control_7 = 0;

motor_control_8 = 0;

}

void fall_back()

{

motor_control_1 = 1;

motor_control_2 = 0;

motor_control_3 = 1;

motor_control_4 = 0;

motor_control_5 = 1;

motor_control_6 = 0;

motor_control_7 = 1;

motor_control_8 = 0;

}

void go_forward()

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

void turn_left()//左轉

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

void turn_right()//右轉

{

motor_control_1 = 0;

motor_control_2 = 1;

motor_control_3 = 0;

motor_control_4 = 1;

motor_control_5 = 0;

motor_control_6 = 1;

motor_control_7 = 0;

motor_control_8 = 1;

}

本文素材來自電子發燒友論壇。

【 】單片機C語言程式設計實訓100例:基於8051+Proteus模擬(第2版) ¥55.1 購買

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示