在4. 51 测量空气的温度和湿度(51单片机 + DHT11温湿度 + LCD1602显示)有介绍
//对于stm32来说,是输入
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);
}
//对于stm32来说,是输出
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);
}
#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; //拉低电平至少18us
delay_ms(20);
read1; //拉高电平20~40us
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++) //1个数据就是1个字节byte,1个字节byte有8位bit
{
while( Read_Data == 0); //从1bit开始,低电平变高电平,等待低电平结束
delay_us(30); //延迟30us是为了区别数据0和数据1,0只有26~28us
data <<= 1; //左移
if( Read_Data == 1 ) //如果过了30us还是高电平的话就是数据1
{
data |= 1; //数据+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 ) //判断DHT11是否响应
{
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(); //接收5个数据
read0; //当最后一bit数据传送完毕后,DHT11拉低总线 50us
delay_us(55); //这里延时55us
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;
}
#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);
#include "DHT11.h"
#include "SysTick.h"
#include "stm32f10x.h"
unsigned int rec_data[4];
//对于stm32来说,是输入
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);
}
//对于stm32来说,是输出
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;
}
在15. STM32——软件IIC驱动OLED屏幕显示字符、字符串、数字、汉字中有
在15. STM32——软件IIC驱动OLED屏幕显示字符、字符串、数字、汉字中有
#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显示下列一些字
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); //℃
//这种都来自于字库文件 oledfont.h
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); //1s更新一次数据
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