51单片机——DHT11模块
1.检测模块是否存在
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;
void Delay30ms()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us()
{
unsigned char i;
i = 25;
while (--i);
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void check_DHT()
{
dht = 1;
dht = 0;
Delay30ms();
dht = 1;
Delay60us();
if(dht == 0){
ledOne = 0;
}
}
void main()
{
ledOne = 1;
Delay1000ms();
Delay1000ms();
check_DHT();
while(1);
}
2.读取温湿度数据
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;
char datas[5];
void Delay30ms()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us()
{
unsigned char i;
i = 25;
while (--i);
}
void Delay1000ms()
{
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;
Delay30ms();
dht = 1;
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);
Delay60us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
void main()
{
ledOne = 1;
Delay1000ms();
Delay1000ms();
while(1){
Delay1000ms();
Read_Data_From_DHT();
}
}
3.读取温湿度数据并通过串口上传
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;
char datas[5];
sfr AUXR = 0x8E;
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x40;
TMOD &= 0xF0;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
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()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us()
{
unsigned char i;
i = 25;
while (--i);
}
void Delay1000ms()
{
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;
Delay30ms();
dht = 1;
while(dht);
while(!dht);
while(dht);
}
void Delay40us()
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
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);
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = 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");
}
}
4.在LCD显示并通过蓝牙发送温湿度
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;
sbit fengshan = P1^6;
char datas[5];
sfr AUXR = 0x8E;
#define databuffer P0
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){
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()
{
unsigned char i, j;
i = 27;
j = 226;
do
{
while (--j);
} while (--i);
}
void Delay5ms()
{
unsigned char i, j;
i = 9;
j = 244;
do
{
while (--j);
} while (--i);
}
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x40;
TMOD &= 0xF0;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
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()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay60us()
{
unsigned char i;
i = 25;
while (--i);
}
void Delay1000ms()
{
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;
Delay30ms();
dht = 1;
while(dht);
while(!dht);
while(dht);
}
void Delay40us()
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
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);
Delay40us();
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
void LCD1602_INIT()
{
Delay15ms();
Write_Cmd_Func(0x38);
Delay5ms();
Write_Cmd_Func(0x38);
Write_Cmd_Func(0x08);
Write_Cmd_Func(0x01);
Write_Cmd_Func(0x06);
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);
}
}