当前位置:网站首页>Stm32+dht11 reading temperature and humidity data display
Stm32+dht11 reading temperature and humidity data display
2022-07-21 05:57:00 【InfoQ】
One 、 Introduction to the environment
Two 、DHT11 Introduce

3、 ... and 、 Core code
3.1 dht11.c
#include "dht11.h"
#include "delay.h"
// Reset DHT11
void DHT11_Rst(void)
{
DHT11_IO_OUT(); //SET OUTPUT
DHT11_DQ_OUT=0; // Pull it down DQ
DelayMs(20); // Pull down at least 18ms
DHT11_DQ_OUT=1; //DQ=1
DelayUs(30); // The main engine is pulled up 20~40us
}
// wait for DHT11 The response of the
// return 1: Not detected DHT11 The existence of
// return 0: There is
u8 DHT11_Check(void)
{
u8 retry=0;
DHT11_IO_IN();//SET INPUT
while (DHT11_DQ_IN&&retry<100)//DHT11 Will pull down 40~80us
{
retry++;
DelayUs(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)//DHT11 After pulling down, it will pull up again 40~80us
{
retry++;
DelayUs(1);
};
if(retry>=100)return 1;
return 0;
}
// from DHT11 Read a bit
// Return value :1/0
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)// Wait until it goes low
{
retry++;
DelayUs(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)// Wait for high level
{
retry++;
DelayUs(1);
}
DelayUs(40);// wait for 40us
if(DHT11_DQ_IN)return 1;
else return 0;
}
// from DHT11 Read a byte
// Return value : Data read
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
// from DHT11 Read data once
//temp: Temperature value ( Range :0~50°)
//humi: Humidity value ( Range :20%~90%)
// Return value :0, normal ;1, Read failed
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)// Read 40 Bit data
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
// initialization DHT11 Of IO mouth DQ Simultaneous detection DHT11 The existence of
// return 1: non-existent
// return 0: There is
u8 DHT11_Init(void)
{
RCC->APB2ENR|=1<<2; // Can make PORTA Port clock
GPIOA->CRL&=0XFF0FFFFF;//PORTA.11 Push pull output
GPIOA->CRL|=0X00300000;
GPIOA->ODR|=1<<5; // Output 1
DHT11_Rst();
return DHT11_Check();
}
3.2 dht11.h
#ifndef __DHT11_H
#define __DHT11_H
#include "sys.h"
//IO Direction setting
#define DHT11_IO_IN() {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0x00800000;}
#define DHT11_IO_OUT() {GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0x00300000;}
////IO Operation function
#define DHT11_DQ_OUT PAout(5) // Data port PA5
#define DHT11_DQ_IN PAin(5) // Data port PA5
u8 DHT11_Init(void); // initialization DHT11
u8 DHT11_Read_Data(u8 *temp,u8 *humi);// Read the temperature and humidity
u8 DHT11_Read_Byte(void); // Read a byte
u8 DHT11_Read_Bit(void); // Read one bit
u8 DHT11_Check(void); // Detect the presence of DHT11
void DHT11_Rst(void); // Reset DHT11
#endif
3.3 main.c
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "sys.h"
#include "usart.h"
#include <string.h>
#include <stdlib.h>
#include "timer.h"
#include "adc.h"
#include "dht11.h"
int main(void)
{
u8 Temperature,Humidity;
LED_Init(); // initialization LED
USARTx_Init(USART1,72,115200);// A serial port 1 The initialization
DHT11_Init();
while(1)
{
LED1=!LED1;
if(DHT11_Read_Data(&Temperature,&Humidity) == 0)
{
printf(" Temperature and humidity : %d,%d\n",Temperature,Humidity);
}
DelayMs(500);
}
}

边栏推荐
- Introduction to open source lightweight workflow workflowcore
- 路由策略干涉选路
- strerror和perror
- Theoretical basis of multithreading
- Leetcode-152- maximum array of products
- ts 使用第三方库,没有类型声明文件报错处理
- 8 Redis 数据库管理
- vite 动态加载静态资源图片,修复打包后图片404问题。
- Informatics Olympiad all in one 2076: [21cspj popularization group] network | Logu p7911 [csp-j 2021] network connection
- php 登记用户需求
猜你喜欢
剑指 Offer 11. 旋转数组的最小数字
[postgraduate entrance examination vocabulary training camp] day 9 - vital, dynamity, previous, pray, transit, virile, invent
DOM系列之创建元素
PicGo配置阿里云oss
Flutter 苹果原生拼音键盘在TextField上输入异常 | 拼音输入过程回调问题
strcat() - 连接字符串
探索者TSSD 2019软件安装包下载及安装教程
[MySQL] current date minus one day field year month day -1 day
C语言中的自定义类型
【考研词汇训练营】Day 9 —— vital,dynasty,previous,praise,transit,virile,invent
随机推荐
旋转框目标检测mmrotate v0.3.1 训练HRSC2016数据集(二)
【考研词汇训练营】Day 8 —— complete,traditional,extra,afford,professional,require
PHP read / write file text
PHP match regular fetch URL
strerror和perror
Try to understand the essence of low code platform design from another angle
Go语言path is relative, but relative import paths are not supported in module mode
Theoretical basis of multithreading
【码蹄集新手村 600 题】如何将一个十进制整数的所有二进制的偶数位置上的二进制数值都变为 0
[postgraduate entrance examination vocabulary training camp] day 8 - complete, traditional, extra, aford, professional, required
【考研词汇训练营】Day 9 —— vital,dynasty,previous,praise,transit,virile,invent
如何确定自己的研究方向 (读后感)
[code hoof set novice village question 600] divide lucky numbers
【码蹄集新手村600题】整除幸运数
C语言中的自定义类型
什麼是棧?
或许是 WebGIS 下一代的数据规范 - OGC API 系列
你是为了什么而努力?不放弃才是我们唯一的选择,加油,每一个有缘人
strcat() - 连接字符串
怎么才能写好技术文档——这是我的全部经验