一、时序分析
1.检测模块是否存在
a : dht = 1
b :dht = 0延时30ms
c: dht = 1在60us后读d点,如果d点是低电平(被模块拉低),说明模块存在!
#include "reg52.h"
#include <intrins.h>
sbit ledOne = P3^7;
sbit dht = 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()//DHT11时序图分析
{
//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()
{
Delay1000ms();
Delay1000ms();
ledOne = 1;
check_DHT();
while(1);
}
2.读取DHT11数据时序分析
a : dht = 1
b :dht = 0延时30ms
c: dht = 1卡d点;while(dht1); 卡e点 while(!dht) 卡f点:while(dht) 卡g点:while(!dht) 有效数据都是高电平,持续时间不一样,50us读,低电平0 高电平1
二、编程示例
1.温湿度通过串口传到PC显示
#include "reg52.h"
#include <intrins.h>
sbit ledOne = P3^7;
sbit dht = P3^3;
sfr AUXR = 0x8E;
char datas[5];
void UartInit(void) //9600bps@11.0592MHz
{
AUXR = 0x01;
SCON = 0x40;//配置串口工作方式1,REN不使能接收
TMOD &= 0x0F;
TMOD |= 0x20;//配置定时器1工作方式为8位自动重装
TH1 = 0xFD;
TL1 = 0xFD;//9600波特率初值
TR1 = 1; //启动定时器1
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char *str)
{
while(*str != '\0'){
sendByte(*str);
str++;
}
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
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()//DHT11时序图分析
{
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++){
for(j=0;j<8;j++){
while(!dht);//等待卡g点
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp <<= 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
void main()
{
ledOne = 1;
UartInit();
Delay1000ms();
Delay1000ms();
while(1){
Delay1000ms();
Read_Data_From_DHT();
sendString("H:");
sendByte(datas[0]/10 +0x30);
sendByte(datas[0]%10 +0x30);
sendByte('.');
sendByte(datas[1]/10 +0x30);
sendByte(datas[1]%10 +0x30);
sendString("\r\n");
sendString("T:");
sendByte(datas[2]/10 +0x30);
sendByte(datas[2]%10 +0x30);
sendByte('.');
sendByte(datas[3]/10 +0x30);
sendByte(datas[3]%10 +0x30);
sendString("\r\n");
}
}
2.温湿度检测小系统:温湿度数据在LCD1602显示
#include "reg52.h"
#include <intrins.h>
#define databuffer P0//定义8位数据线,P0端口组
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;
sbit D5 = P3^7;
sbit D6 = P3^6;
sbit dht = P3^3;
sfr AUXR = 0x8E;
char datas[5];
char temp[8];
char huma[8];
void UartInit(void) //9600bps@11.0592MHz
{
AUXR = 0x01;
SCON = 0x40;//配置串口工作方式1,REN不使能接收
TMOD &= 0x0F;
TMOD |= 0x20;//配置定时器1工作方式为8位自动重装
TH1 = 0xFD;
TL1 = 0xFD;//9600波特率初值
TR1 = 1; //启动定时器1
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char *str)
{
while(*str != '\0'){
sendByte(*str);
str++;
}
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
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_Busy()
{
char tmp = 0x80;
databuffer = 0x80;
while(tmp & 0x80){//比较最高位
RS = 0;
RW = 1;
EN = 0;
_nop_();
EN = 1;
_nop_();
_nop_();
tmp = databuffer;
EN = 0;
_nop_();
}
}
void Write_Cmd_Func(char cmd)//写指令,RS低电平
{
Check_Busy();
RS = 0;
RW = 0;
EN = 0;
_nop_();//空函数,延时1us
databuffer = cmd;
_nop_();
EN = 1;
_nop_();
_nop_();
EN = 0;
_nop_();
}
void Write_Data_Func(char cmd)//写数据,RS高电平
{
Check_Busy();
RS = 1;
RW = 0;
EN = 0;
_nop_();//空函数,延时1us
databuffer = cmd;
_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 DHT11_Start()//DHT11时序图分析
{
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++){
for(j=0;j<8;j++){
while(!dht);//等待卡g点
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp <<= 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
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()
{
//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 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();
D5 = 0;
while(1){
Delay1000ms();
Read_Data_From_DHT();
if(datas[2] > 31){
D6 = 0;
}
Build_Datas();
sendString(huma);
sendString("\r\n");
sendString(temp);
sendString("\r\n");
LCD1602_showLine(1,2,huma);
LCD1602_showLine(2,2,temp);
}
}