STM32——测量空气的温度和湿度(STM32 + DHT11温湿度 + OLED显示)
DHT11温湿度相关介绍
在4. 51 测量空气的温度和湿度(51单片机 + DHT11温湿度 + LCD1602显示)有介绍
初始化GPIOB11
void DH11_GPIO_Init_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void DH11_GPIO_Init_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
启动DHT11
#define read0 GPIO_ResetBits(GPIOB, GPIO_Pin_11)
#define read1 GPIO_SetBits(GPIOB, GPIO_Pin_11)
void DHT11_Start(void)
{
DH11_GPIO_Init_OUT();
read1;
delay_us(30);
read0;
delay_ms(20);
read1;
delay_us(30);
DH11_GPIO_Init_IN();
}
获取一个字节
#define Read_Data GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)
unsigned char DHT11_Rec_Byte(void)
{
unsigned char i = 0;
unsigned char data;
for(i=0;i<8;i++)
{
while( Read_Data == 0);
delay_us(30);
data <<= 1;
if( Read_Data == 1 )
{
data |= 1;
}
while( Read_Data == 1 );
}
return data;
}
获取数据
void DH11_REC_Data(void)
{
unsigned int R_H,R_L,T_H,T_L;
unsigned char RH,RL,TH,TL,CHECK;
DH11_Start();
read1;
if( Read_Data == 0 )
{
while( Read_Data == 0);
while( Read_Data == 1);
R_H = DHT11_REC_Byte();
R_L = DHT11_REC_Byte();
T_H = DHT11_REC_Byte();
T_L = DHT11_REC_Byte();
CHECK = DHT11_REC_Byte();
read0;
delay_us(55);
read1;
if(R_H + R_L + T_H + T_L == CHECK)
{
RH = R_H;
RL = R_L;
TH = T_H;
TL = T_L;
}
}
rec_data[0] = RH;
rec_data[1] = RL;
rec_data[2] = TH;
rec_data[3] = TL;
}
代码整合
DHT11.h
#ifndef _DHT11_H_
#define _DHT11_H_
#include "stm32f10x.h"
#define read0 GPIO_ResetBits(GPIOB, GPIO_Pin_11)
#define read1 GPIO_SetBits(GPIOB, GPIO_Pin_11)
#define Read_Data GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)
void DHT11_GPIO_Init_IN(void);
void DHT11_GPIO_Init_OUT(void);
void DHT11_Start(void);
unsigned char DHT11_REC_Byte(void);
void DHT11_REC_Data(void);
DHT11.c
#include "DHT11.h"
#include "SysTick.h"
#include "stm32f10x.h"
unsigned int rec_data[4];
void DHT11_GPIO_Init_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void DHT11_GPIO_Init_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void DHT11_Start(void)
{
DHT11_GPIO_Init_OUT();
read1;
delay_us(30);
read0;
delay_ms(20);
read1;
delay_us(30);
DHT11_GPIO_Init_IN();
}
unsigned char DHT11_REC_Byte(void)
{
unsigned char i = 0;
unsigned char data;
for(i=0;i<8;i++)
{
while(Read_Data == 0);
delay_us(30);
data <<= 1;
if(Read_Data == 1)
{
data += 1;
}
while(Read_Data == 1);
}
return data;
}
void DHT11_REC_Data(void)
{
unsigned int R_H,R_L,T_H,T_L;
unsigned char RH,RL,TH,TL,CHECK;
DHT11_Start();
read1;
if(Read_Data == 0)
{
while(Read_Data == 0);
while(Read_Data == 1);
R_H = DHT11_REC_Byte();
R_L = DHT11_REC_Byte();
T_H = DHT11_REC_Byte();
T_L = DHT11_REC_Byte();
CHECK = DHT11_REC_Byte();
read0;
delay_us(55);
read1;
if(R_H + R_L + T_H + T_L == CHECK)
{
RH = R_H;
RL = R_L;
TH = T_H;
TL = T_L;
}
}
rec_data[0] = RH;
rec_data[1] = RL;
rec_data[2] = TH;
rec_data[3] = TL;
}
oled.h
在15. STM32——软件IIC驱动OLED屏幕显示字符、字符串、数字、汉字中有
oled.c
在15. STM32——软件IIC驱动OLED屏幕显示字符、字符串、数字、汉字中有
main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "shake.h"
#include "relay.h"
#include "exti.h"
#include "usart.h"
#include "tim.h"
#include "motor.h"
#include "SysTick.h"
#include "HC_SR04.h"
#include "oled.h"
#include "DHT11.h"
extern unsigned int rec_data[4];
int main()
{
delay_ms(1000);
OLED_Init();
OLED_Clear();
delay_ms(1000);
OLED_ShowChinese(0,2,6);
OLED_ShowChinese(16,2,7);
OLED_ShowChinese(32,2,8);
OLED_ShowChinese(48,2,9);
OLED_ShowChinese(64,2,10);
OLED_ShowChinese(90,2,15);
OLED_ShowChinese(112,2,11);
OLED_ShowChinese(0,5,6);
OLED_ShowChinese(16,5,7);
OLED_ShowChinese(32,5,12);
OLED_ShowChinese(48,5,9);
OLED_ShowChinese(64,5,10);
OLED_ShowChinese(90,5,15);
OLED_ShowChinese(112,5,13);
while(1)
{
delay_ms(1000);
DHT11_REC_Data();
OLED_ShowNum(74,2,rec_data[2],2,16);
OLED_ShowNum(98,2,rec_data[3],1,16);
OLED_ShowNum(74,5,rec_data[0],2,16);
OLED_ShowNum(98,5,rec_data[1],2,16);
}
}
效果
如果觉得这篇文章对你有用。欢迎大家点赞、评论哈哈哈!!! 需要整个工程代码,欢迎大家打赏,评论区留上你的邮箱 or vx or qq。o( ̄︶ ̄)o
|