【STM32】使用DHT11温湿度传感器
作者:互联网
文章目录
前言
本项目采用ARM结构中最为代表的Cortex-M4系列的芯片,选用STM32F407ZGT6开发板进行项目开发,选用的传感器为常见通用的DHT11温湿度传感器。传感器将采集到的数据传输到STM32(MCU)主控进行数据处理,最后通过串口打印出来。
一、软硬件准备
1、软件
Keil5
官方下载链接如下:http://www.keil.com/demo/eval/arm.htm
STM32f407固件库
官方下载链接如下:http://www.keil.com/dd2/pack
STM32CudeMx
官方下载链接如下:http://www.st.com/web/en/catalog/tools/PF259242
STM32CudeMx的f407软件包
官方下载链接如下:http://www.st.com/web/en/catalog/tools/PF259243
2、硬件
STM32F407ZGT6
DHT11温湿度传感器
3、接线
DHT11 | STM32 |
---|---|
data | PG9 |
vcc | 5v |
GND | GND |
二、使用步骤
1、硬件平台安装
keil5安装、 STM32CudeMx安装
请按照本人共享的链接里面的开发环境文件夹里面word文档进行操作,提取文件密码为:f22d
BSP工程项目创建
①打开STM32CudeMX
②点击创建工程
③搜索STM32F407ZGT6,双击黄色区域
④点击Categories——》System Core ——》GPIO,选择PF9和PF10,各自点击为GPIO_OutPut
⑤对GPIO进行具体配置
⑥配置RCC时钟
⑦配置系统时钟
⑧这里以串口1为例 我们可以选择串口的模式(异步,同步,半双工) 串口接收中断
a)点击USATR1
b)设置MODE为异步通信(Asynchronous)
c)基础参数:波特率为115200 Bits/s。传输数据长度为8 Bit。奇偶检验无,停止位1 接收和发送都使能
d)GPIO引脚设置 USART1_RX/USART_TX
e) NVIC Settings 一栏使能接收中断
⑨配置STM32F407ZGT6的时钟树,由于是外部8M的晶振,所以得出一下的时钟树
a)选择外部时钟HSE 8MHz
b)PLL锁相环倍频168倍
c)系统时钟来源选择为PLL
d)设置APB1分频器为 /4
32的时钟树框图 如果不懂的话请看《【STM32】系统时钟RCC详解(超详细,超全面)》
⑩建立工程
2.BSP工程项目开发之MD5操作
(1)用keil5打开此工程,uvprojx文件双击
(2)点击option(魔法棒图形),然后进行主频配置,修改为8.0或者12.0,然后重新打开该工程进行检查,最后进行编译。
(3)在keil5上面创建SYSTEM和HARDWAVE两个文件夹
(4)回到本人创建的test工程目录,添加这两个文件夹,本人已经整理好了链接(STM32课程资料\库文件),复制库文件里面SYSTEM和HARDWAVE两个文件夹到test工程目录下。
(5)回到keil5里面,继续点击那个文件管理,然后根据对应的文件夹添加文件,一个都不要漏。
注:HARDWAVE也是这样添加工程文件进去。
(6)配置头文件路径,选择为第4步已经复制的两个文件夹(SYSTEM和HARDWAVE)
3、代码
main.c
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "dht11.h"
void SystemClock_Config(void);
int main(void)
{
u8 t=0;
u8 temperature;
u8 humidity;
int times;
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
delay_init(168);
SystemClock_Config();
DHT11_Init();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE);
while (1)
{
if(t%10==0)//?100ms????
{
DHT11_Read_Data(&temperature,&humidity);
printf("2018A14122 WuXiaoXian\r\n");
printf("Tem:%d\r\n",temperature);
printf("Hum:%d\r\n",humidity);
printf("\r\n\n");
}
delay_ms(100);
t++;
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
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 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uart.c
#include "usart.h"
#include "stdio.h"
/* USER CODE BEGIN 0 */
uint8_t USART_RX_BUF[USART_REC_LEN];
uint16_t USART_RX_STA=0; //??????
uint8_t aRxBuffer[RXBUFFERSIZE];//HAL??????????
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
/* USART1 init function */
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch, 1, 0XFFFF);
return ch;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART1)//?????1
{
if((USART_RX_STA&0x8000)==0)//?????
{
if(USART_RX_STA&0x4000) //????0x0d
{
if(aRxBuffer[0]!= 0x0a)
{
USART_RX_STA=0; //????,????
}
else
{
USART_RX_STA|=0x8000; //?????
}
}
else //????0x0D
{
if(aRxBuffer[0] == 0x0d)
{
USART_RX_STA|=0x4000;
}
else
{
USART_RX_BUF[USART_RX_STA&0x3FFF]=aRxBuffer[0];
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))
{
USART_RX_STA=0; //??????,??????
}
}
}
}
}
}
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uart.h
/**
******************************************************************************
* @file usart.h
* @brief This file contains all the function prototypes for
* the usart.c file
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
#define USART_REC_LEN 500
#define RXBUFFERSIZE 1
extern uint8_t USART_RX_BUF[USART_REC_LEN];
extern uint16_t USART_RX_STA;
extern UART_HandleTypeDef UART1_Handler; //UART??
extern uint8_t aRxBuffer[RXBUFFERSIZE];//HAL??????????
/* USER CODE END Includes */
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_USART1_UART_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
4、配置debug
5、测试结果
一定要先1编译后2烧录,每次更改了都要先编译!!
三、连接阿里云…
思路
由于这款开发板并没有WiFi模块,我们可以外接一个WiFi模块,使用串口传输通信来实现,后续学会了再更新这一步吧
总结
STM32开发相对于Arduino来说更加复杂,建议还是自己去学习一下,推荐正点原子的资料:
https://note.youdao.com/s/Lw3g8Bq9
标签:CODE,HAL,温湿度,STM32,USER,GPIO,DHT11,USART1,RCC 来源: https://blog.csdn.net/silver_lf/article/details/117919328