超声波防撞。。。
发射和接受原理图:
单片机源程序如下(主机):
- #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)
|