目录
DHT11 温湿度传感器
产品概述
特点
检测模块是否存在
温湿度数据管理系统
uart.c文件
uart.h文件
lcd1602.c文件
lcd1602.H文件
dht11.c文件
dht11.h文件
delay.c文件
delay.h文件
config.h文件
main.c文件
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,应用领域:暖通
空调;汽车;消费品;气象站;湿度调节器;除湿器;家电;医疗;自动控制
![在这里插入图片描述](https://img-blog.csdnimg.cn/47c96a99f371492d8e5e74579d086692.png)
//检测模块是否接在设备上
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;//模块的data插在p3.3
void Delay30ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us() //@11.0592MHz
{
unsigned char i;
i = 25;
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);
}
void check_DHT()
{
//a : dht = 1
dht = 1;
//b :dht = 0
dht = 0;
//延时30ms
Delay30ms();
//c: dht = 1
dht = 1;
//在60us后读d点,如果d点是低电平(被模块拉低),说明模块存在!
Delay60us();
if(dht == 0){
ledOne = 0;//亮灯,说明模块存在
}
}
void main()
{
ledOne = 1;
Delay1000ms();
Delay1000ms();
check_DHT();
while(1);
}
DHT11传输1的时序分析
//接线:LCD1602数据选用P0口, RS接P1.0 RW接P1.1 EN接P1.4
//DHT11:数据线接P3.3口
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;//模块的data插在p3.3
sbit fengshan = P1^6;
char datas[5];
sfr AUXR = 0x8E;
#define databuffer P0 //定义8位数据线,Po端口组
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;
char temp[8];
char huma[8];
void check_busy()
{
char tmp = 0x80;
databuffer = 0x80;
while(tmp & 0x80){//1000 0000
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;
_nop_();
_nop_();
tmp = databuffer;
EN = 0;
_nop_();
}
}
void Write_Cmd_Func(char cmd)
{
check_busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();
databuffer = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char dataShow)
{
check_busy();
RS = 1;
RW = 0;
EN = 0;
_nop_();
databuffer = dataShow;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Delay15ms() //@11.0592MHz
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
void UartInit(void) //[email protected]
{
AUXR = 0x01;
SCON = 0x40; //配置串口工作方式1,REN不使能接收
TMOD &= 0xF0;
TMOD |= 0x20;//定时器1工作方式位8位自动重装
TH1 = 0xFD;
TL1 = 0xFD;//9600波特率的初值
TR1 = 1;//启动定时器
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void Delay30ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us() //@11.0592MHz
{
unsigned char i;
i = 25;
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);
}
void DHT11_Start()
{
dht = 1;
dht = 0;
//延时30ms
Delay30ms();
dht = 1;
//卡d点;while(dht1); 卡e点 while(!dht) 卡f点:while(dht)
while(dht);
while(!dht);
while(dht);
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
void Read_Data_From_DHT()
{
int i;//轮
int j;//每一轮读多少次
char tmp;//8bit01010110的数据
char flag;
DHT11_Start();
for(i= 0;i < 5;i++){//8bit湿度整数数据+8bit湿度小数数据+8bi温度整数数据+8bit温度小数数据+8bit校验和
//卡g点:while(!dht) 有效数据都是高电平,持续时间不一样,50us读,低电平0 高电平
for(j=0;j<8;j++){
while(!dht);//等待卡g点
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;//每左移一次空一位
tmp |= flag;//填补空的位置
}
datas[i] = tmp;
}
}
void LCD1602_INIT()
{
//(1)延时 15ms
Delay15ms();
//(2)写指令 38H(不检测忙信号)
Write_Cmd_Func(0x38);
//(3)延时 5ms
Delay5ms();
//(4)以后每次写指令,读/写数据操作均需要检测忙信号
//(5)写指令 38H:显示模式设置
Write_Cmd_Func(0x38);
//(6)写指令 08H:显示关闭
Write_Cmd_Func(0x08);
//(7)写指令 01H:显示清屏
Write_Cmd_Func(0x01);
//(8)写指令 06H:显示光标移动设置
Write_Cmd_Func(0x06);
//(9)写指令 0CH:显示开及光标设置}
Write_Cmd_Func(0x0c);
}
void LCD1602_showLine(char row, char col, char *string)
{
switch(row){
case 1:
Write_Cmd_Func(0x80+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
case 2:
Write_Cmd_Func(0x80+0x40+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
}
}
void Build_Datas()
{
huma[0] = 'H';
huma[1] = datas[0]/10 + 0x30;
huma[2] = datas[0]%10 + 0x30;
huma[3] = '.';
huma[4] = datas[1]/10 + 0x30;
huma[5] = datas[1]%10 + 0x30;
huma[6] = '%';
huma[7] = '\0';
temp[0] = 'T';
temp[1] = datas[2]/10 + 0x30;
temp[2] = datas[2]%10 + 0x30;
temp[3] = '.';
temp[4] = datas[3]/10 + 0x30;
temp[5] = datas[3]%10 + 0x30;
temp[6] = 'C';
temp[7] = '\0';
}
void main()
{
Delay1000ms();
UartInit();
LCD1602_INIT();
Delay1000ms();
Delay1000ms();
ledOne = 0;
while(1){
Delay1000ms();
Read_Data_From_DHT();
if(datas[2] > 24){
fengshan = 0;
}
Build_Datas();
sendString(huma);
sendString("\r\n");
sendString(temp);
sendString("\r\n");
LCD1602_showLine(1,2,huma);
LCD1602_showLine(2,2,temp);
}
}
代码太乱,为了解决使用了分文件来操作
依次创建delay.c,delay.h,dht11.c,dht11.h,lcd1602.c,1cd1602.h,uart.c,uart.h等文件
#include "reg52.h"
#include "config.h"
#include
sfr AUXR = 0x8E;
sbit D6 = P3^6;
char buffer[12];
void UartInit(void) //[email protected]
{
AUXR = 0x01;
SCON = 0x50; //配置串口工作方式1,REN使能接收
TMOD &= 0xF0;
TMOD |= 0x20;//定时器1工作方式位8位自动重装
TH1 = 0xFD;
TL1 = 0xFD;//9600波特率的初值
TR1 = 1;//启动定时器
EA = 1;//开启总中断
ES = 1;//开启串口中断
}
void sendByte(char data_msg)//传一个字符
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)//传字符串
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void Uart_Handler() interrupt 4
{
char tmp;
char i;
if(RI)//中断处理函数中,对于接收中断的响应
{
RI = 0;//清除接收中断标志位
tmp = SBUF;
if(tmp == ':'){
i = 0;
}
buffer[i++] = tmp;
if(buffer[0] == ':' && buffer[1] =='o' && buffer[2] == 'p'){
fengshan = 0;
memset(buffer,'\0',12);
}
if(buffer[0] == ':' && buffer[1] == 'c' && buffer[2] == 'l'){
fengshan = 1;
D6 = 0;
Delay1000ms();
Delay1000ms();
memset(buffer,'\0',12);
}
}
if(TI);
}
void UartInit(void); //[email protected]
void sendByte(char data_msg);
void sendString(char* str);
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#define databuffer P0 //定义8位数据线,Po端口组
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;
void check_busy()
{
char tmp = 0x80;
databuffer = 0x80;
while(tmp & 0x80){//1000 0000
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;
_nop_();
_nop_();
tmp = databuffer;
EN = 0;
_nop_();
}
}
void Write_Cmd_Func(char cmd)//写指令
{
check_busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();
databuffer = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char dataShow)//写数据
{
check_busy();
RS = 1;
RW = 0;
EN = 0;
_nop_();
databuffer = dataShow;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void LCD1602_INIT()
{
//(1)延时 15ms
Delay15ms();
//(2)写指令 38H(不检测忙信号)
Write_Cmd_Func(0x38);
//(3)延时 5ms
Delay5ms();
//(4)以后每次写指令,读/写数据操作均需要检测忙信号
//(5)写指令 38H:显示模式设置
Write_Cmd_Func(0x38);
//(6)写指令 08H:显示关闭
Write_Cmd_Func(0x08);
//(7)写指令 01H:显示清屏
Write_Cmd_Func(0x01);
//(8)写指令 06H:显示光标移动设置
Write_Cmd_Func(0x06);
//(9)写指令 0CH:显示开及光标设置}
Write_Cmd_Func(0x0c);
}
void LCD1602_showLine(char row, char col, char *string)
{
switch(row){
case 1:
Write_Cmd_Func(0x80+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
case 2:
Write_Cmd_Func(0x80+0x40+col);
while(*string){
Write_Data_Func(*string);
string++;
}
break;
}
}
void LCD1602_INIT();
void LCD1602_showLine(char row, char col, char *string);
#include "reg52.h"
#include "delay.h"
sbit dht = P3^3;//模块的data插在p3.3
char datas[5];
void DHT11_Start()
{
dht = 1;
dht = 0;
//延时30ms
Delay30ms();
dht = 1;
//卡d点;while(dht1); 卡e点 while(!dht) 卡f点:while(dht)
while(dht);
while(!dht);
while(dht);
}
void Read_Data_From_DHT()
{
int i;//轮
int j;//每一轮读多少次
char tmp;
char flag;
DHT11_Start();
for(i= 0;i < 5;i++){
//卡g点:while(!dht) 有效数据都是高电平,持续时间不一样,50us读,低电平0 高电平
for(j=0;j<8;j++){
while(!dht);//等待卡g点
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
void Read_Data_From_DHT();
#include "intrins.h"
void Delay30ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
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);
}
void Delay15ms() //@11.0592MHz
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
#include "intrins.h"
void Delay30ms(); //@11.0592MHz
void Delay40us(); //@11.0592MHz
void Delay1000ms(); //@11.0592MHz
void Delay15ms(); //@11.0592MHz
void Delay5ms(); //@11.0592MHz
sbit ledOne = P3^7;
sbit fengshan = P1^6;
#include "reg52.h"
#include "intrins.h"
#include "delay.h"
#include "lcd1602.h"
#include "uart.h"
#include "dht11.h"
#include "config.h"
char temp[8];
char huma[8];
extern char datas[5];
void Build_Datas()//利用ascall码来转换数字,发现规律每次需加0x30
{
huma[0] = 'H';
huma[1] = datas[0]/10 + 0x30;
huma[2] = datas[0]%10 + 0x30;
huma[3] = '.';
huma[4] = datas[1]/10 + 0x30;
huma[5] = datas[1]%10 + 0x30;
huma[6] = '%';
huma[7] = '\0';
temp[0] = 'T';
temp[1] = datas[2]/10 + 0x30;
temp[2] = datas[2]%10 + 0x30;
temp[3] = '.';
temp[4] = datas[3]/10 + 0x30;
temp[5] = datas[3]%10 + 0x30;
temp[6] = 'C';
temp[7] = '\0';
}
void main()
{
Delay1000ms();
UartInit();//串口初始化
LCD1602_INIT();//LCD1602初始化
Delay1000ms();
Delay1000ms();
ledOne = 0;
while(1){
Delay1000ms();
Read_Data_From_DHT();
if(datas[2] >= 24){
fengshan = 0;
}else{
fengshan = 1;
}
Build_Datas();
sendString(huma);
sendString("\r\n");
sendString(temp);
sendString("\r\n");
LCD1602_showLine(1,2,huma);
LCD1602_showLine(2,2,temp);
}
}