1思路
1打开时钟
1.1使用定时器实现us级的计时
1.2在打开串口
1,3在DHT11驱动中修改引脚
stm32cudeMX 配置
1打开时钟
2打开串口
3打开tim1(定时器)
4生成代码
代码设置
1导入DHT11库(tim.h是定时器的文件系统自动生成的)
DHT11.c
#include "dht11.h"
#include "tim.h"void DHT11_IO_IN(void)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.Pin = GPIO_PIN_1;GPIO_InitStructure.Mode = GPIO_MODE_INPUT;HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
}void DHT11_IO_OUT(void)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.Pin = GPIO_PIN_1;GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
}//复位DHT11
void DHT11_Rst(void)
{ DHT11_IO_OUT(); //设置为输出DHT11_DQ_OUT_LOW; //拉低DQHAL_Delay(20); //拉低至少18msDHT11_DQ_OUT_HIGH; //DQ=1 delay_us(30); //主机拉高20~40us
}//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
uint8_t DHT11_Check(void)
{ uint8_t retry=0;DHT11_IO_IN(); //设置为输出 while (DHT11_DQ_IN&&retry<100)//DHT11会拉低40~80us{retry++;delay_us(1);}; if(retry>=100)return 1;else retry=0;while (!DHT11_DQ_IN&&retry<100)//DHT11拉低后会再次拉高40~80us{retry++;delay_us(1);};if(retry>=100)return 1; return 0;
}//从DHT11读取一个位
//返回值:1/0
uint8_t DHT11_Read_Bit(void)
{uint8_t retry=0;while(DHT11_DQ_IN&&retry<100)//等待变为低电平{retry++;delay_us(1);}retry=0;while(!DHT11_DQ_IN&&retry<100)//等待变高电平{retry++;delay_us(1);}delay_us(40);//等待40usif(DHT11_DQ_IN)return 1;else return 0;
}//从DHT11读取一个字节
//返回值:读到的数据
uint8_t DHT11_Read_Byte(void)
{ uint8_t i,dat;dat=0;for (i=0;i<8;i++) {dat<<=1; dat|=DHT11_Read_Bit();} return dat;
}//从DHT11读取一次数据
//temp:温度值(范围:0~50°)
//humi:湿度值(范围:20%~90%)
//返回值:0,正常;1,读取失败
uint8_t DHT11_Read_Data(uint16_t *temp,uint16_t *humi)
{ uint8_t buf[5];uint8_t i;DHT11_Rst();if(DHT11_Check()==0){for(i=0;i<5;i++)//读取40位数据{buf[i]=DHT11_Read_Byte();}if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]){*humi=(buf[0]<<8) + buf[1];*temp=(buf[2]<<8) + buf[3];}}else return 1;return 0;
}//初始化DHT11的IO口 DQ 同时检测DHT11的存在
//返回1:不存在
//返回0:存在
uint8_t DHT11_Init(void)
{ DHT11_Rst();return DHT11_Check();
}
DHT11.h
#ifndef __DHT11_H__
#define __DHT11_H__
#include "main.h"
#define DHT11_DQ_OUT_HIGH HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET)
#define DHT11_DQ_OUT_LOW HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET)
#define DHT11_DQ_IN HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)//IO方向设置
void DS18B20_IO_IN(void);
void DS18B20_IO_OUT(void);uint8_t DHT11_Init(void);//初始化DHT11
uint8_t DHT11_Read_Data(uint16_t *temp,uint16_t *humi);//读取温湿度
uint8_t DHT11_Read_Byte(void);//读出一个字节
uint8_t DHT11_Read_Bit(void);//读出一个位
uint8_t DHT11_Check(void);//检测是否存在DHT11
void DHT11_Rst(void);//复位DHT11 #endif
2在定时文件中添加计时
利用定时实现数
在tim.c中末尾添加如下函数利用定时其2实现的
/* USER CODE BEGIN 1 */
void delay_us(uint16_t us)
{uint16_t differ = 0xffff-us-5; __HAL_TIM_SET_COUNTER(&htim2,differ); HAL_TIM_Base_Start(&htim2); while(differ < 0xffff-5){ differ = __HAL_TIM_GET_COUNTER(&htim2); }HAL_TIM_Base_Stop(&htim2);
}void delay_ms(uint16_t ms){for(int i=0;i<ms;i++){delay_us(1000);}
}void delay_s(uint16_t s){for(int i=0;i<s;i++){delay_ms(1000);}
}
/* USER CODE END 1 */
tim.h
/* USER CODE BEGIN Includes */
void delay_us(uint16_t us);
void delay_ms(uint16_t ms);
void delay_s(uint16_t s);
/* USER CODE END Includes */
3调用DHT11函数
添加库文件
/* USER CODE BEGIN Includes */
#include "DHT11.h"
#include "stdio.h"
/* USER CODE END Includes */
main函数
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();MX_TIM2_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */uint16_t wen_du;//温度uint16_t shi_du;//湿度/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){if(DHT11_Init()){//判断是否有DHT11uint8_t data[]={"没有检测到DHT11"};HAL_UART_Transmit(&huart1,data,sizeof(data),20);}else{//采集数据DHT11_Init();DHT11_Read_Data(&wen_du,&shi_du);//存储数据char DHT11_data1[2];char DHT11_data2[2];char DHT11_data3[2];char DHT11_data4[2];//数据转换//转换成字符sprintf(DHT11_data1,"%d",(wen_du>>8));//温度高8位sprintf(DHT11_data2,"%d",(wen_du&0xff));//温度低8位sprintf(DHT11_data3,"%d",(shi_du>>8));//湿度高8位sprintf(DHT11_data4,"%d",(shi_du&0xff));//湿度低8位//符号uint8_t wen_du_zi[]={"温度为:"};uint8_t shi_du_zi[]={"湿度为:"};uint8_t dian[]={"."};uint8_t bai_fen_hao[]={"%"};uint8_t wen_du_fu[]={"°C"};//显示数据HAL_UART_Transmit(&huart1,wen_du_zi,sizeof(wen_du_zi),20);//温度为HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data1,sizeof(DHT11_data1),20);//整数HAL_UART_Transmit(&huart1,dian,1,20);HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data2,sizeof(DHT11_data2),20);//小数HAL_UART_Transmit(&huart1,wen_du_fu,4,20);//符号delay_s(1);HAL_UART_Transmit(&huart1,shi_du_zi,sizeof(shi_du_zi),20);//湿度为HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data3,sizeof(DHT11_data3),20);//整数HAL_UART_Transmit(&huart1,dian,1,20);HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data4,sizeof(DHT11_data4),20);//小数HAL_UART_Transmit(&huart1,bai_fen_hao,1,20);//符号}delay_s(1);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
main.h文件
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 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 "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "DHT11.h"
#include "stdio.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* 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 *//* 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();MX_TIM2_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */uint16_t wen_du;//温度uint16_t shi_du;//湿度/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){if(DHT11_Init()){//判断是否有DHT11uint8_t data[]={"没有检测到DHT11"};HAL_UART_Transmit(&huart1,data,sizeof(data),20);}else{//采集数据DHT11_Init();DHT11_Read_Data(&wen_du,&shi_du);//存储数据char DHT11_data1[2];char DHT11_data2[2];char DHT11_data3[2];char DHT11_data4[2];//数据转换//转换成字符sprintf(DHT11_data1,"%d",(wen_du>>8));//温度高8位sprintf(DHT11_data2,"%d",(wen_du&0xff));//温度低8位sprintf(DHT11_data3,"%d",(shi_du>>8));//湿度高8位sprintf(DHT11_data4,"%d",(shi_du&0xff));//湿度低8位//符号uint8_t wen_du_zi[]={"温度为:"};uint8_t shi_du_zi[]={"湿度为:"};uint8_t dian[]={"."};uint8_t bai_fen_hao[]={"%"};uint8_t wen_du_fu[]={"°C"};//显示数据HAL_UART_Transmit(&huart1,wen_du_zi,sizeof(wen_du_zi),20);//温度为HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data1,sizeof(DHT11_data1),20);//整数HAL_UART_Transmit(&huart1,dian,1,20);HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data2,sizeof(DHT11_data2),20);//小数HAL_UART_Transmit(&huart1,wen_du_fu,4,20);//符号delay_s(1);HAL_UART_Transmit(&huart1,shi_du_zi,sizeof(shi_du_zi),20);//湿度为HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data3,sizeof(DHT11_data3),20);//整数HAL_UART_Transmit(&huart1,dian,1,20);HAL_UART_Transmit(&huart1,(uint8_t*)DHT11_data4,sizeof(DHT11_data4),20);//小数HAL_UART_Transmit(&huart1,bai_fen_hao,1,20);//符号}delay_s(1);/* 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 */
本人的下载资源中有完整的项目(免费下载)