在我的另一篇里文章里已经介绍过DHT11的时序理论了,这里介绍下,我写DHT11的数据获取的思路和调用。程序验证后,发现下述问题,暂时解决不了,但是还是会把个人的代码流程,函数的编写思路和工程写下,也欢迎大家能发现问题,或者能起到参考作用吧。
不过我现在又发现了个很奇怪的问题,STM32复位后的第一次DHT11数据的获取一定是正确的,但是在以后的读取时就数据大概率出问题,问题也找出来是,DHT11一返回的 40 位数据被分为了 5 组,每组 8 位(一字节),但是每一组的相等于高位的第一位,就容易将’0’判定为‘1’,代码检测时序我检查比较过很多次了,找不出问题源,都怀疑是不是DHT11外部连接或者物理性质的问题了。上述问题,在未开启校验的情况下进行了检测,但在每次数据捕获之间间隔 2 秒的情况下进行了测试。
在写DHT11的数据获取代码时,函数编写基于下述思路。
DHT11要用到MS级别的延时,直接等效调用HAL库的延时函数。
//DHT11毫秒延时
//参数1: Xms延时
void DHT11_DelayMs(int ms)
{
HAL_Delay(ms);
}
DHT11要用到US级别的延时,需要用到空指令,和函数调用和循环调用的时间等诸多考虑因数,最后还得有逻辑分析仪或示波器,来检验US的延时是否符合要求。
#define DHT11_5NOP(); __NOP;__NOP;__NOP;__NOP;__NOP;
//DHT11的10微秒延时
void DHT11_Delay10Us()
{
for(char i=100;i>0;i--)
{
DHT11_5NOP();
}
}
下述代码是检测高电平的持续时间,还判断DHT11的数据位是’0’还是’1’。
//判断数据位
//返回值:
char DHT11_IfBit()
{
char Start;
unsigned char StartNum=0;
//拉高判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(!Start);
//高电平存在时长判断
do{
DHT11_Delay10Us();
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
StartNum++;
//if(StartNum>6) break;
}while(Start);
//电平判断
if(StartNum<4)
{
Start=0;
}
else
{
Start=1;
}
return Start;
}
用下述两种方法来,确保度过高低电平的持续时间,进入下一个边沿信号。
//拉高判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(!Start);
//拉低判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(Start);
实践环境:代码是基于STM32CubeMX生成的,主频是72MHz。DHT11的DATA引脚接到PA1。OLED的SCL接PB8,OLED的SDA接PB9。
工程中我都加入了注释,文章后面,会分享我的工程的。下面,提供部分代码的预览。如果工程中的的DHT11库代码,跟我另一片DHT11理论文章中粘贴出来的不一样,以工程中的代码为准,因为我可能有时候有些逻辑优化之类的小改。
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "DHT11.h"
#include "oled.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern unsigned char DHT11_DATA[5];
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
//DHT11的10微秒延时
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
DHT11_Init();
OLED_Init();
OLED_Clear();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//OLED_ShowNum(1,1,DHT11_Start,3,16);
HAL_Delay(500);
while (1)
{
DHT11_ReadData();
//OLED_Clear();DHT11_DATA
//检测输出湿度的数据
OLED_ShowNum(1,0,DHT11_DATA[0],3,16);
OLED_ShowNum(32,0,DHT11_DATA[1],3,16);
//检测输出温度的数据
OLED_ShowNum(1,2,DHT11_DATA[2],3,16);
OLED_ShowNum(32,2,DHT11_DATA[3],3,16);
//检测校验的数据
OLED_ShowNum(1,4,DHT11_DATA[4],3,16);
// OLED_ShowNum(1,6,DHT11_Init(),3,16);
//校验后,输出
OLED_ShowNum(1,6,DHT11_GetData(0,1),3,16);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
//OLED_ShowNum(48,6,HAL_GetTick(),3,16);
//OLED_ShowNum(1,7,DHT11_DATA[0],8,16);
HAL_Delay(2000);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
// HAL_Delay(1000);
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
// DHT11_Init();
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
DHT11.c
#include "main.h"
#include "DHT11.h"
//控制接口
#define DHT11_DataPinx GPIO_PIN_1
#define DHT11_DataGPIOx GPIOA
#define DHT11_DataH() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET)
#define DHT11_DataL() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET)
#define DHT11_DataR() HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)
#define DHT11_5NOP(); __NOP;__NOP;__NOP;__NOP;__NOP;
//数据存放数组
unsigned char DHT11_DATA[5];
//DHT11毫秒延时
//参数1: Xms延时
void DHT11_DelayMs(int ms)
{
HAL_Delay(ms);
}
//DHT11的10微秒延时
void DHT11_Delay10Us()
{
for(char i=100;i>0;i--)
{
DHT11_5NOP();
}
}
//DHT11数据引脚设置为输入模式
void DHT11_DataSetIn()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = DHT11_DataPinx;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DHT11_DataGPIOx, &GPIO_InitStruct);
}
//DHT11数据引脚设置为输出模式
void DHT11_DataSetOut()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(DHT11_DataGPIOx, &GPIO_InitStruct);
}
//判断数据位
//返回值:
char DHT11_IfBit()
{
char Start;
unsigned char StartNum=0;
//拉高判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(!Start);
//高电平存在时长判断
do{
DHT11_Delay10Us();
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
StartNum++;
//if(StartNum>6) break;
}while(Start);
//电平判断
if(StartNum<4)
{
Start=0;
}
else
{
Start=1;
}
return Start;
}
char i=0;
signed char a=0;
//等待收集数据
void DHT11_ReadData()
{
char Start;
DHT11_Init();
DHT11_DataSetIn();
//拉高判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(!Start);
//拉低判断
do{
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
}while(Start);
//第一个数据开始传输
//数组索引0~4
for(i=0;i<5;i++)
{
for(a=0;a<8;a++)
{
Start=DHT11_IfBit();
DHT11_DATA[i]|=Start;
if(a>=7) break;
DHT11_DATA[i]<<=1;
}
}
}
//DHT11初始化
//返回值: 0.有响应 1.无响应
char DHT11_Init(void)
{
char Start=1;
//设置为输出模式
DHT11_DataSetOut();
//主机拉低至少20ms
DHT11_DataL();
DHT11_DelayMs(20);
//主机拉高至少20us-40us
DHT11_DataH();
DHT11_Delay10Us();
DHT11_Delay10Us();
DHT11_Delay10Us();
//设置为输入模式
DHT11_DataSetIn();
Start=HAL_GPIO_ReadPin(DHT11_DataGPIOx,DHT11_DataPinx);
return Start;
}
//DHT11获取数据
//参数Mode: 0:返回湿度 1:返回温度
//参数Check: 0:关闭校验 1:开启校验
//说明:如果开启校验,但是校验失败,就会返回值为0
float DHT11_GetData(char Mode,char Check)
{
float ReData=0;
switch(Mode)
{
case 0:
ReData=DHT11_DATA[0]+DHT11_DATA[1]/255.0f;
break;
case 1:
ReData=DHT11_DATA[2]+DHT11_DATA[3]/255.0f;
break;
}
if(Check==1)
{
if(DHT11_DATA[0]+DHT11_DATA[1]+DHT11_DATA[2]+DHT11_DATA[3]!=DHT11_DATA[4])
{
ReData=0;
}
}
return ReData;
}
我写的DHT11时序理论文章:【模块系列】STM32&&DHT11时序理论&&关键代码
链接:https://pan.baidu.com/s/1yJQj6P5SbZvCbXatUoTB3w 提取码:fvkk