如何利用51单片机设计一个蓝牙控制风扇系统

电子说

1.2w人已加入

描述

这个是我大二才学51单片机的时候做的一个项目,其实这个项目比较简单,只不过是我们自己把它想得太难了,下面我来总结一下具体设计过程,

首先买一个蓝牙模块,我买的是HC-06,在手机上下载一个蓝牙串口调试助手,等同于单片机的UART串口通信,利用BUFF来获取接受到的数据,这里只需要重视一个问题,就是测试编码,如果APP发送0xFF,(APP蓝牙串口调试助手已经支持手动设置按钮名字和发送的字符了)但是蓝牙模块很有可能接受到的是0xF8,0xF2,0xFE。所以这里我们首先应该测试编码。HC-06一般有5个端口,RX,TX,VCC,GND,AT,AT这里我们没有用到,是指更改密码,默认密码一般是1234或者0000,把VCC和GND接好,然后RX和TX分别和单片机的TX和RX相连接(就是P3^0和P3^1,切记要相反连接),然后我们按照一般的UART串口通信就可以啦,检测接受到BUFF数据,然后switch就可以实现相应的功能。

#include

#define uchar unsigned char

#define uint unsigned int

sbit PWM=P1^0;

sbit DSPORT=P3^7;

void Ds18b20ReadTemp();

void UsartConfiguration();

unsigned int temp=0;

unsigned char zhuanshu=20;

unsigned int cnt=0;

unsigned char receiveData;

unsigned int dingshi=0;

unsigned char min=1;

void main(void)

{

unsigned int i=3000;

UsartConfiguration();

while(1)

{

if(cnt>=3000)

{

cnt=0;

Ds18b20ReadTemp(); //3s检测一次温度

}

switch(receiveData)

{

case 177:ET0=1;zhuanshu=20;receiveData=0;break; // 数字1 风扇启动

case 178:zhuanshu=0;while(i--);ET0=0;receiveData=0;break; //数字2 风扇停止 由于IO口默认高电平 所以要让zhuanshu=0

case 179:zhuanshu=30;break;//数字3 低挡

case 180:zhuanshu=50;break;//数字4 中挡

case 181:zhuanshu=70;break;// 数字5高挡

case 182: // 数字6自动根据温度调节转速

{

if(temp<3200)

{

zhuanshu=25;

}

if((temp>3200)&&(temp<3300))

{

zhuanshu=30;

}

if((temp>3400)&&(temp<3600))

{

zhuanshu=50;

}

if((temp>3200)&&(temp<3300))

{

zhuanshu=60;

}

} break;

case 183: //定时时间数字7

{

static dingshi=0;

if(dingshi>=60000) //1分钟一个基准

{

min--;

dingshi=0;

if(min==0)

{

ET0=0;

zhuanshu=0;

}

}

break;

case 184: receiveData=0;min++;break; //增加时间 数字8

case 185: receiveData=0;min--;break; //减少时间 数字9

}

}

}

}

void UsartConfiguration()

{

SCON=0X50; //设置为工作方式1

TMOD=0X21; //设置计数器工作方式2

PCON=0X80; //波特率加倍

TH0 = 0xFC;

TL0 = 0x66;

TH1=0XF9; //计数器初始值设置,注意波特率是9600的

TL1=0XF9;

ET0=1;

ES=1; //打开接收中断

EA=1; //打开总中断

TR1=1;

TR0=1; //打开计数器

}

void Usart() interrupt 4

{

receiveData=SBUF; //出去接收到的数据

RI = 0; //清除接收中断标志位

}

void Time1(void) interrupt 1 //3 为定时器1的中断号 1 定时器0的中断号 0 外部中断1 2 外部中断2 4 串口中断

{

static unsigned char timer1=0;

TH0 = 0xF6; //重新赋初值

TL0 = 0x66;

timer1++;

cnt++;

dingshi++;

if(dingshi>=60050)

{

dingshi=0;

}

if(timer1>100) //PWM周期为100*0.5ms

{

timer1=0;

}

if(timer1

{

PWM=1;

}

else

{

PWM=0;

}

}

void Delay1ms(uint y) //延时程序

{

uint x;

for( ; y>0; y--)

{

for(x=110; x>0; x--);

}

}

uchar Ds18b20Init() //温度的子函数

{

uchar i;

DSPORT = 0; //将总线拉低480us~960us

i = 70;

while(i--);//延时642us

DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低

i = 0;

while(DSPORT) //等待DS18B20拉低总线

{

i++;

if(i>5)//等待>5MS

{

return 0;//初始化失败

}

Delay1ms(1);

}

return 1;//初始化成功

}

void Ds18b20WriteByte(uchar dat)

{

uint i, j;

for(j=0; j<8; j++)

{

DSPORT = 0; //每写入一位数据之前先把总线拉低1us

i++;

DSPORT = dat & 0x01; //然后写入一个数据,从最低位开始

i=6;

while(i--); //延时68us,持续时间最少60us

DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值

dat >>= 1;

}

}

uchar Ds18b20ReadByte()

{

uchar byte, bi;

uint i, j;

for(j=8; j>0; j--)

{

DSPORT = 0;//先将总线拉低1us

i++;

DSPORT = 1;//然后释放总线

i++;

i++;//延时6us等待数据稳定

bi = DSPORT; //读取数据,从最低位开始读取

/*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/

byte = (byte >> 1) | (bi << 7);

i = 4; //读取完之后等待48us再接着读取下一个数

while(i--);

}

return byte;

}

void Ds18b20ChangTemp()

{

Ds18b20Init();

Delay1ms(1);

Ds18b20WriteByte(0xcc);//跳过ROM操作命令

Ds18b20WriteByte(0x44); //温度转换命令

// Delay1ms(100);//等待转换成功,而如果你是一直刷着的话,就不用这个延时了

}

void Ds18b20ReadTempCom()

{

Ds18b20Init();

Delay1ms(1);

Ds18b20WriteByte(0xcc);//跳过ROM操作命令

Ds18b20WriteByte(0xbe);//发送读取温度命令

}

void Ds18b20ReadTemp()

{

float tp;

int temp1 = 0;

uchar tmh, tml;

Ds18b20ChangTemp();//先写入转换命令

Ds18b20ReadTempCom();//然后等待转换完后发送读取温度命令

tml = Ds18b20ReadByte();//读取温度值共16位,先读低字节

tmh = Ds18b20ReadByte();//再读高字节

temp1 = tmh;

temp1 <<= 8;

temp1 |= tml;

if(temp1< 0)

{

temp1=temp1-1;

temp1=~temp1;

tp=temp1;

temp1=tp*0.0625*100+0.5;

}

else

{

tp=temp1;

temp1=tp*0.0625*100+0.5;

}

temp=temp1%10000;

}


 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分