温湿度传感器介绍
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,应用于专用数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性和卓越的长期稳定性。 传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一个高性能八位单片机相连接。采用单线制串口行接口,信号传输距离可达20M以上。
应用于通暖空调,汽车,自动控制设备,气象站家电 湿度调节器,医疗,除湿器等等。
温湿度传感器介绍
供电电压:3.3 - 5.5V直流电 输出为单总线数字信号 温度测量范围0-50度(精度正负2度,分辨率1度) 湿度测量范围为20-90%RH(精度为正负5%,分辨率1%)
模块的+ 接单片机的5V 模块的- 接单片机的GND 模块的OUT 接单片机定义的引脚 vcc和gnd之间可以加一个电容,用于去耦滤波 采用单总线双向串行通信协议,每次采集都要由单片机发起开始信号,然后DHT11会向单片机发送响应并开始传输40位数据帧,高位在前。
数据格式为:
第一二个字节: 8bit湿度整数数据+8bit湿度小数数据 第三四个字节: 8bit温度整数数据+8bit温度小数数据 第五个字节 : 8bit校验位(它是前四个数据相加后八位的数值) 温湿度小数部分默认为0,即单片机采集的数据都是整数,校验位为4个字节的数据相加取结果的低8位数据作为校验和;
示例一: 湿度高八位:0011 0101 湿度低八位:0000 0000 温度高八位:0011 1000 温度低八位:0000 0000 检验位:0110 1101
计算 : 0011 0101 (相加) 0011 1000 结果: 01101101 湿度为0011 0101 = 35H = 53%RH 温度为0011 1000 = 18H = 24°
示例二: 湿度高八位:0011 0101 湿度低八位:0000 0000 温度高八位:0001 1000 温度低八位:0000 0000 检验位:0110 1001
计算 : 0011 0101 (相加) 0001 1000 结果: 0100 1101 不等于 01101001 本次接收数据不正确,重新接收数据
温湿度传感器时序介绍
总线空闲状态为高电平,主机把总线拉低等待DHT11响应,主机把总线拉低必须大于18毫秒,保证DHT11能检测起始信号。DHT11接收到主机的开始信号后,等待主机开始信号结束,然后发送80us低电平响应信号,主机发送开始信号结束后,延时等待20-40us后,读取DHT11的响应信号,主机发送开始信号后,可以切换到输入模式,或者输出高电平均可,总线由上拉电阻拉高。 DHT11输出0时时序 DHT11输出1时时序
代码
main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "DHT11.h"
#include <stdio.h>
extern unsigned int rec_dat[4];
extern unsigned char rec_dat_lcd0[6];
extern unsigned char rec_dat_lcd1[6];
extern unsigned char rec_dat_lcd2[6];
extern unsigned char rec_dat_lcd3[6];
void Delay500ms(void)
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 22;
j = 3;
k = 227;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
int main()
{
LCD_Init();
InitUART();
LCD_ShowString(1,0,"Humi:");
LCD_ShowString(2,1,"Temp:");
while(1)
{
DHT11_delay_ms(150);
DHT11_receive();
sprintf(rec_dat_lcd0,"%d",rec_dat[0]);
sprintf(rec_dat_lcd1,"%d",rec_dat[1]);
sprintf(rec_dat_lcd2,"%d",rec_dat[2]);
sprintf(rec_dat_lcd3,"%d",rec_dat[3]);
DHT11_delay_ms(100);
LCD_ShowString(1,6,rec_dat_lcd0);
LCD_ShowString(1,8,".");
LCD_ShowString(1,9,rec_dat_lcd1);
LCD_ShowString(1,10,"%");
LCD_ShowString(2,6,rec_dat_lcd2);
LCD_ShowString(2,8,".");
LCD_ShowString(2,9,rec_dat_lcd3);
LCD_ShowString(2,10,"C");
}
}
DHT11.h
#ifndef _DHT11_H_
#define _DHT11_H_
void DHT11_START();
unsigned char DHT11_rec_byte();
void DHT11_receive();
void DHT11_delay_us(unsigned char n);
void DHT11_delay_ms(unsigned int z);
void InitUART(void);
#endif
DHT11.c
#include <REG52.H>
#include "DHT11.h"
sbit Temp_data = P2^0;
unsigned int rec_dat[4];
unsigned char rec_dat_lcd0[6];
unsigned char rec_dat_lcd1[6];
unsigned char rec_dat_lcd2[6];
unsigned char rec_dat_lcd3[6];
void DHT11_START()
{
Temp_data = 1;
DHT11_delay_us(2);
Temp_data = 0;
DHT11_delay_ms(20);
Temp_data = 1;
DHT11_delay_us(13);
}
unsigned char DHT11_rec_byte()
{
unsigned char i,dat;
for(i=0;i<8;i++)
{
while(!Temp_data);
DHT11_delay_us(8);
dat<<=1;
if(Temp_data == 1)
{
dat += 1;
}
while(Temp_data);
}
return dat;
}
void DHT11_receive()
{
unsigned int R_H,R_L,T_H,T_L;
unsigned char RH,RL,TH,TL,revise;
DHT11_START();
Temp_data = 1;
if(Temp_data == 0)
{
while(Temp_data == 0);
DHT11_delay_us(40);
R_H = DHT11_rec_byte();
R_L = DHT11_rec_byte();
T_H = DHT11_rec_byte();
T_L = DHT11_rec_byte();
revise = DHT11_rec_byte();
DHT11_delay_us(25);
if((R_H+R_L+T_H+T_L) == revise)
{
RH = R_H;
RL = R_L;
TH = T_H;
TL = T_L;
}
rec_dat[0] = RH;
rec_dat[1] = RL;
rec_dat[2] = TH;
rec_dat[3] = TL;
}
}
void DHT11_delay_us(unsigned char n)
{
while(--n);
}
void DHT11_delay_ms(unsigned int z)
{
unsigned int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void InitUART(void)
{
SCON = 0X40;
REN = 1;
TMOD = 0X20;
TH1 = 0XF3;
TL1 = 0XF3;
TI = 1;
TR1 = 1;
}
LCD1602.h
#ifndef _LCD1602_H_
#define _LCD1602_H_
void LCD_WriteCommand(unsigned char Command);
void LCD_WriteDate(unsigned char Date);
void LCD_Init(void);
void LCD_SetCursor(unsigned char Line,unsigned char Columu);
void LCD_Showchar(unsigned char Line,unsigned char Columu,unsigned char Char);
void LCD_ShowString(unsigned char Line,unsigned char Columu,unsigned char *String);
int LCD_Pow(int x,int y);
int LCD_getNumberSize(int Number);
void LCD_ShowNumber(unsigned char Line,unsigned char Columu,unsigned int Number);
#endif
LCD1602.c
#include <REGX52.h>
#include "LCD1602.h"
#include <INTRINS.H>
sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_E = P2^7;
#define LCD_DataPort P0
void LCD_Delay1ms(void)
{
unsigned char i, j;
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DataPort = Command;
LCD_E = 1;
LCD_Delay1ms();
LCD_E = 0;
LCD_Delay1ms();
}
void LCD_WriteDate(unsigned char Date)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DataPort = Date;
LCD_E = 1;
LCD_Delay1ms();
LCD_E = 0;
LCD_Delay1ms();
}
void LCD_Init(void)
{
LCD_WriteCommand(0X38);
LCD_WriteCommand(0X0c);
LCD_WriteCommand(0X06);
LCD_WriteCommand(0X01);
}
void LCD_SetCursor(unsigned char Line,unsigned char Columu)
{
if(Line == 1){
LCD_WriteCommand(0x80 | (Columu - 1));
}else{
LCD_WriteCommand(0x80 | (Columu - 1) + 0x40);
}
}
void LCD_Showchar(unsigned char Line,unsigned char Columu,unsigned char Char)
{
LCD_SetCursor(Line,Columu);
LCD_WriteDate(Char);
}
void LCD_ShowString(unsigned char Line,unsigned char Columu,unsigned char *String)
{
unsigned char i;
LCD_SetCursor(Line,Columu);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteDate(String[i]);
}
}
int LCD_Pow(int x,int y)
{
unsigned char i;
int Result = 1;
for(i=0;i<y;i++)
{
Result *=x;
}
return Result;
}
int LCD_getNumberSize(int Number)
{
unsigned int Size = 0;
while(Number > 0)
{
Number /= 10;
Size++;
}
return Size;
}
void LCD_ShowNumber(unsigned char Line,unsigned char Columu,unsigned int Number)
{
unsigned int i;
LCD_SetCursor(Line,Columu);
for(i=LCD_getNumberSize(Number);i>0;i--)
{
LCD_WriteDate('0'+Number/LCD_Pow(10,i-1)%10);
}
}
|