当前位置:网站首页>Stc89c52/90c516rd/89c516rd DHT11 temperature and humidity sensor drive code
Stc89c52/90c516rd/89c516rd DHT11 temperature and humidity sensor drive code
2022-06-30 03:15:00 【LH_ SMD】
1. Hardware
STC89C52/90C516RD/89C516RD etc.
Crystal oscillator :11.0592M
Memory configuration :
Memory Model by small, Selecting other does not work properly .
2. Code
dht11.h
#ifndef __DHT11_H
#define __DHT11_H
#include "reg52.h"
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
IO Operation function
sbit DHT11_DQ_OUT=P1^4;
extern uchar idata rec_dat[15];
uchar get_dht11_data(uchar *temp, uchar *humi);
#endif
dht11.c
#include <dht11.h>
#include <intrins.h>
#include "delay.h"
uchar idata rec_dat[15] = "";
uchar idata humi_data_H = 0;
uchar idata humi_data_L = 0;
uchar idata temp_data_H = 0;
uchar idata temp_data_L = 0;
uchar idata check_data = 0;
/*************************************************** * Delay subfunction * Time delay 10us ****************************************************/
static void delay_10us() //@11.0592MHz
{
uchar i;
i = 2;
while (--i);
}
static void delay_30us() //@11.0592MHz
{
uchar i;
i = 11;
while (--i);
}
/* Function description : obtain dht11 A byte Parameters :void Return value : Acquired dht11 One byte of data */
static uchar get_dht11_byte(void)
{
uchar i = 0;
uchar time_out = 0;
uchar temp = 0;
uchar dht11_byte_data = 0;
for(i=0;i<8;i++)
{
time_out=0;
while((!DHT11_DQ_OUT)&&time_out++ <= 5); /* each bit Before the data starts , Both last about 50us Low level of */
if(time_out == 6) break; /* Timeout exit */
delay_30us(); /* exceed 30us High level after , Indicates that the bit by 1 */
temp=0;
if(DHT11_DQ_OUT)temp=1;
time_out=0;
while((DHT11_DQ_OUT)&&time_out++ <= 200);
if(time_out==200)break;
dht11_byte_data<<=1;
dht11_byte_data|=temp; //0
}
return dht11_byte_data;
}
/* Function description : obtain DHT11 Temperature and humidity data Parameters :*temp temperature *humi humidity Return value : Return on success 0 Failure to return 1 */
uchar get_dht11_data(uchar *temp, uchar *humi)
{
uchar time_out;
uchar temp1 = 0;
DHT11_DQ_OUT=0;
delay_ms(20); /* At least pull down the host 18ms */
DHT11_DQ_OUT=1;
delay_10us();
delay_10us();
delay_10us();
delay_10us(); /* The main engine is pulled up 20-40us */
time_out = 0;
while(!DHT11_DQ_OUT && time_out++ <= 5); /* wait for DHT11 Respond */
if(time_out == 6) return 1; /* Timeout exit ,sht11 Not responding */
else /* DHT11 Respond to */
{
time_out=0;
while((!DHT11_DQ_OUT)&&time_out++ <= 5); /* wait for DHT11 The response is complete , About time delay is required 80us */
if(time_out == 6) return 1; /* Timeout exception */
time_out=0;
while((DHT11_DQ_OUT)&&time_out++ <= 5);
if(time_out == 6) return 1; /* Timeout exception */
humi_data_H=get_dht11_byte();
humi_data_L=get_dht11_byte();
temp_data_H=get_dht11_byte();
temp_data_L=get_dht11_byte();
check_data =get_dht11_byte();
DHT11_DQ_OUT=1;
// data verification
temp1 = (humi_data_H+humi_data_L+temp_data_H+temp_data_L);
if(temp1 == check_data)
{
*humi = humi_data_H;
*temp = temp_data_H;
rec_dat[0] = temp_data_H/10 + '0';
rec_dat[1] = temp_data_H%10 + '0';
rec_dat[2] = ' ';
rec_dat[3] = humi_data_H/10 + '0';
rec_dat[4] = humi_data_H%10 + '0';
rec_dat[5] = '\0';
}
}
return 0;
}
If you have any problems with the code, please refer to DHT11 Data book or message , The common module or chip driver code will be updated from time to time .
边栏推荐
- Possible problems in MySQL cross database operation with database name
- Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
- Auto. JS learning notes 15:ui interface basics of autojs Chapter 2
- Knowledge points of 2022 system integration project management engineer examination: software quality assurance and quality evaluation
- 通用分页(2)
- Compile a DLL without import table
- Summary of PHP test sites encountered in CTF questions (I)
- JvxeTable子表记录加载完毕事件
- Mysql提取表字段中的字符串
- Use of Arthas
猜你喜欢
随机推荐
Tri rapide, index groupé, recherche de la plus grande valeur K dans les données
HOOK Native API
如果辨别我现在交易的外盘股指期货交易平台是否正规安全?
Note the use of export/import and class inheritance in ES6
HOOK Native API
Data set preparation and arrangement
The MariaDB database was found 12 hours late
自定义MVC的使用
Neo4j--- performance optimization
[QT] QMap使用详解
Utilisation de foreach en Qt
Global and Chinese markets for active transdermal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
How to modify and add fields when MySQL table data is large
Federal learning: dividing non IID samples by Dirichlet distribution
Utf8 error in Oracle migration of Jincang Kingbase database
Global and Chinese market of ULTRACENTRIFUGES 2022-2028: Research Report on technology, participants, trends, market size and share
广播模块代码在autojs4.1.1版本运行正常,但在pro7.0版本上运行报错(未解决)
Summary of interview and Employment Questions
Link garbled escape character
Use compose to realize the effect of selecting movie seats by panning tickets



![[QT] QMap使用详解](/img/ee/6e71a3dc5b90d2d1b7f7d3f6b56221.png)




