DH11温湿度检测模块、lcd1602、HC-08蓝牙构成温湿度数据管理系统
DHT11 温湿度传感器
产品概述
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,应用领域:暖通。
空调;汽车;消费品;气象站;湿度调节器;除湿器;家电;医疗;自动控制。

特点
相对湿度和温度测量
全部校准,数字输出
长期稳定性
超长的信号传输距离:20米
超低能耗:休眠
引脚安装:可以买封装好的
完全互换 : 直接出结果,不用转化
接线

数据传送逻辑
只有一根数据线DATA,上官一号发送序列指令给DHT11模块,模块一次完整的数据传输为40bit,高位先出。
数据格式
8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据+8bit校验和通讯过程时序图

检测模块是否存在
根据如下时序图,做通信初始化,并检测模块是否存在,功能是否正常.

时序逻辑分析
a:dht = 1
b:dht = 0
延时30ms
c:dht = 1
void DH11_init()
{
DH11 = 1;
DH11 = 0;
Delay20ms(); //延时20ms
DH11 = 1;
while(DH11);
while(!DH11);
while(DH11);
//卡d点;while(dht1); 卡e点 while(!dht) 卡f点:while(dht)
}
DHT11传输0的时序分析

DHT11传输1的时序分析

char DH11_databuf[5];
void get_data()
{
int i = 0;
int j = 0;
char tmp;
char DH11_data;
DH11_init();
for(i = 0;i < 5;i++)//五轮
{
for(j = 0;j < 8;j++)//每轮读8次
{
while(!DH11);
Delay40us();
if(DH11 == 1)
{
DH11_data = 1;
while(DH11);
}
else
{
DH11_data = 0;
}
tmp = tmp << 1; //这一行将 tmp 的值向左移动一位。通过这个操作,tmp 的原来的最低位被丢弃,而最高位补零。
tmp |= DH11_data; //这一行将 DH11_data 的值添加到 tmp 的最低位。如果 DH11_data 是 1,则最低位设置为 1;如果 DH11_data 是 0,则最低位保持不变。
//这两行代码的目的是将 DH11_data 按位添加到 tmp 的低端,并将 tmp 向左移动一位。这个过程可以将 DH11_data 的每一位添加到 tmp 的低端,从而构建一个完整的字节。
}
DH11_databuf[i] = tmp;
}
}
程序源码
main.c
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "uart.h"
#include "lcd.h"
#include "configuration.h"
#include "DH11.h"
extern char DH11_databuf[5];
char humidity[8];
char temperature[8];
void build_data()
{
humidity[0] = 'H';
humidity[1] = DH11_databuf[0]/10 + 0x30;
humidity[2] = DH11_databuf[0]%10 + 0x30;
humidity[3] = '.';
humidity[4] = DH11_databuf[1]/10 + 0x30;
humidity[5] = DH11_databuf[1]%10 + 0x30;
humidity[6] = '%';
humidity[7] = '\0';
temperature[0] = 'T';
temperature[1] = DH11_databuf[2]/10 + 0x30;;
temperature[2] = DH11_databuf[2]%10 + 0x30;;
temperature[3] = '.';
temperature[4] = DH11_databuf[3]/10 + 0x30;;
temperature[5] = DH11_databuf[3]%10 + 0x30;;
temperature[6] = 'C';
temperature[7] = '\0';
}
void main()
{
lcd_init();
my_UartInit();
Delay1000ms();
Delay1000ms();
while(1)
{
Delay1000ms();
get_data();
build_data();
sendString("H:");
sendByte(DH11_databuf[0]/10 + 0x30);
sendByte(DH11_databuf[0]%10 + 0x30);
sendByte('.');
sendByte(DH11_databuf[1]/10 + 0x30);
sendByte(DH11_databuf[1]%10 + 0x30);
sendString("\r\n");
sendString("T:");
sendByte(DH11_databuf[2]/10 + 0x30);
sendByte(DH11_databuf[2]%10 + 0x30);
sendByte('.');
sendByte(DH11_databuf[3]/10 + 0x30);
sendByte(DH11_databuf[3]%10 + 0x30);
sendString("\r\n");
show_string(1,0,humidity);
show_string(2,0,temperature);
}
}
delay.c
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "uart.h"
#include "lcd.h"
#include "configuration.h"
#include "DH11.h"
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
void Delay15ms() //@11.0592MHz
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void Delay20ms() //@11.0592MHz
{
unsigned char i, j;
i = 36;
j = 217;
do
{
while (--j);
} while (--i);
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
DH11.c
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "uart.h"
#include "lcd.h"
#include "configuration.h"
#include "DH11.h"
char DH11_databuf[5];
void DH11_init()
{
DH11 = 1;
DH11 = 0;
Delay20ms();
DH11 = 1;
while(DH11);
while(!DH11);
while(DH11);
}
void get_data()
{
int i = 0;
int j = 0;
char tmp;
char DH11_data;
DH11_init();
for(i = 0;i < 5;i++) //五轮
{
for(j = 0;j < 8;j++) //每轮8bit,读八次数据
{
while(!DH11);
Delay40us();
if(DH11 == 1)
{
DH11_data = 1;
while(DH11);
}
else
{
DH11_data = 0;
}
tmp = tmp << 1; //这一行将 tmp 的值向左移动一位。通过这个操作,tmp 的原来的最低位被丢弃,而最高位补零。
tmp |= DH11_data; //这一行将 DH11_data 的值添加到 tmp 的最低位。如果 DH11_data 是 1,则最低位设置为 1;如果 DH11_data 是 0,则最低位保持不变。
//这两行代码的目的是将 DH11_data 按位添加到 tmp 的低端,并将 tmp 向左移动一位。这个过程可以将 DH11_data 的每一位添加到 tmp 的低端,从而构建一个完整的字节。
}
DH11_databuf[i] = tmp;
}
}
lcd.c
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "uart.h"
#include "lcd.h"
#include "configuration.h"
#include "DH11.h"
#define databuf P0
void check_busy()
{
char tmp = 0x80;
databuf = 0x80;
while(tmp & 0x80)
{
RS = 0;
RW = 1;
E = 0;
_nop_();
E = 1;
_nop_();
_nop_();
tmp = databuf;
E = 0;
_nop_();
}
}
void write_cmd(char cmd)
{
check_busy();
RS = 0;
RW = 0;
E = 0;
_nop_();
databuf = cmd;
_nop_();
E = 1;
_nop_();
_nop_();
E = 0;
_nop_();
}
void write_data(char data1)
{
check_busy();
RS = 1;
RW = 0;
E = 0;
_nop_();
databuf = data1;
_nop_();
E = 1;
_nop_();
_nop_();
E = 0;
_nop_();
}
void lcd_init()
{
Delay15ms();
write_cmd(0x38);
Delay5ms();
check_busy();
write_cmd(0x38);
write_cmd(0x08);
write_cmd(0x01);
write_cmd(0x06);
write_cmd(0x0c);
}
void show_string(char row, char column, char *string)
{
switch(row)
{
case 1:
write_cmd(0x80 + column);
while(*string)
{
write_data(*string++);
// string++;
}
break;
case 2:
write_cmd(0x80 + 0x40 + column);
while(*string)
{
write_data(*string++);
// string++;
}
break;
}
}
uart.c
将蓝牙模块RXD与单片机的TXD相连,TXD与单片机的RXD相连。
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "uart.h"
#include "lcd.h"
#include "configuration.h"
#include "DH11.h"
sfr AUXR = 0x8E;
void my_UartInit()
{
SCON = 0x50;//0100 0000 0111 1111 01xx xxxx
PCON &= 0x7F;// 0111 1111
AUXR |= 0x01;// 0000 0001
TMOD &= 0x0F;// 1100 1111 xx10 xxxx
TMOD |= 0x20;// 0010 0000
TH1 = 0xFD; // smod = 0 9600 = (2^smod) / 32 x 11059200 / 12/(256 - THL)
TH1 = 0xFD; // THL = 253 = FD(HEX)
TR1 = 1;
}
void sendByte(char msg_data)
{
SBUF = msg_data;
while(TI == 0);//结束TI=1,TI==0为假
TI = 0;
}
void sendString(char *str)
{
while(*str != '\0')
{
sendByte(*str);
str++;
}
}
configuration.h
#define databuf P0
sbit DH11 = P3^3;
sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^4;
本文详细介绍了DHT11温湿度传感器的工作原理、特点、数据格式以及与LCD1602和HC-08蓝牙模块的配合,包括数据传送逻辑、检测模块存在判断和时序分析,展示了完整的程序源码实现。
1385

被折叠的 条评论
为什么被折叠?



