超聲波防撞。。。
發射和接受原理圖:
單片機源程序如下(主機):
- #include <reg52.h>//需要注意的是藍牙信號端不能接電源!!!否則會燒壞!
- #define uchar unsigned char
- #define uint unsigned int
- #include"delay.h"
- /*----------藍牙的RXD端口接單片機的TXD端口,藍牙的TXD端口接單片機的RXD端口,因為不是自收自發-----------*/
- //----------------------------------------按鍵端口------------------------------------------------------
- sbit S1=P1^3;
- sbit S2=P1^4;
- sbit S3=P1^5;
- sbit S4=P1^7;
- void uart_init()
- {
- TMOD|=0x20;
- TH1=0xfd;
- TL1=0xfd; //波特率9600
- SM0=0;
- SM1=1;
- REN=1;
- TR1=1;
- }
- void send_bluetooth(uchar b) //藍牙發送
- {
- ES= 0; //為防止干擾 關閉串口
- SBUF=b; //將采集的數據送入緩沖區等待發送
- while(!TI); // 等特數據傳送 (TI發送中斷標志)
- TI = 0; //標志位清零
- ES= 1; //打開串口 等待接收數據
- DelayMs(50);
- }
- void set()
- {
- if(S1==1 && S2==1 && S3==1 && S4==1)
- {
- send_bluetooth(0x00);
- }
- //////////////////////////////////// ///前進
- if(S1==0 )
- {
- send_bluetooth(0x11);
- }
- //////////////////////////////////////左轉
- if(S2==0 )
- {
- send_bluetooth(0x12);
- }
- //////////////////////////////////////右轉
- if(S3==0 )
- {
- send_bluetooth(0x13);
- }
- //////////////////////////////////////后退
- if(S4==0 )
- {
- send_bluetooth(0x14);
- }
- }
- void main()
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
藍牙遙控+超聲波避障.rar
(2.14 MB, 下載次數: 113)
2017-11-3 13:49 上傳
點擊文件名下載附件
sd
|