当前位置:网站首页>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 .
边栏推荐
- Principle of device driver
- golang bilibili直播彈幕姬
- How to modify and add fields when MySQL table data is large
- Federal learning: dividing non IID samples by Dirichlet distribution
- Personal PC installation software
- Summary of PHP test sites encountered in CTF questions (I)
- Neo4j--- performance optimization
- How do I enable assembly binding logging- How can I enable Assembly binding logging?
- 数据库的下一个变革方向——云原生数据库
- X Book 6.97 shield unidbg calling method
猜你喜欢

O & M (21) make winpe startup USB flash disk

Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing

X书6.97版本shield-unidbg调用方式
![[QT] QMap使用详解](/img/ee/6e71a3dc5b90d2d1b7f7d3f6b56221.png)
[QT] QMap使用详解

OP-diode-限制摆幅

【十分钟】manim安装 2022

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load

golang bilibili直播彈幕姬

通用分页(2)

F1C100S自制开发板调试过程
随机推荐
How to use redis to realize the like function
问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
Principle of device driver
OP-diode-限制摆幅
On the role of database tables
炒现货黄金的交易平台如何保障资金安全?
X书6.89版本shield-unidbg调用方式
General paging (2)
发现mariadb数据库时间晚了12个小时
Azure developer news flash list of events of developers in June
HOOK Native API
Mysql 带上库名称跨库操作可能出现的问题
Formal and actual parameters, value passing and address passing
Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
JS conversion of letters and numbers
How to use vant to realize data paging and drop-down loading
2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
MySQL extracts strings from table fields
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
[oiclass] chess piece