STM32F051C8 DHT11 串口打印PB8,STM32CubeMX生成详细过程以及代码
时间: 2024-03-14 11:49:13 浏览: 86
STM32使用STM32CubeMX生成文件,并实现串口打印功能
好的,下面是详细的过程和代码:
1. 打开STM32CubeMX软件,新建一个工程。
2. 选择单片机型号为STM32F051C8,然后在Pinout & Configuration选项卡中对引脚进行配置。将DHT11连接到PB8引脚,将USART1的TX和RX引脚分别连接到PA9和PA10引脚。具体的引脚配置如下图所示:
![STM32F051C8 DHT11 串口打印PB8 引脚配置](https://img-blog.csdnimg.cn/20211019112234770/STM32F051C8-DHT11-serial-print-PB8_pin-configuration.png)
3. 在Configuration选项卡中,配置USART1的波特率为9600,数据位为8位,停止位为1位。具体的配置如下图所示:
![STM32F051C8 DHT11 串口打印PB8 USART1 配置](https://img-blog.csdnimg.cn/20211019112453273/STM32F051C8-DHT11-serial-print-PB8_USART1-configuration.png)
4. 在Project Manager选项卡中,选择生成代码。然后在弹出的对话框中选择使用CubeIDE作为开发环境,并选择要生成的文件类型,最后点击Generate代码按钮。
5. 在CubeIDE中打开生成的工程,然后打开main.c文件。在main函数中添加以下代码:
```c
#include "main.h"
#include "dht11.h"
#include <stdio.h>
#include <string.h>
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
char buffer[50];
while (1)
{
DHT11_Data_TypeDef data;
if (DHT11_ReadData(&data) == DHT11_OK)
{
sprintf(buffer, "Temperature: %d.%dC, Humidity: %d.%d%%\r\n",
data.temperature / 10, data.temperature % 10,
data.humidity / 10, data.humidity % 10);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);
}
else
{
HAL_UART_Transmit(&huart1, (uint8_t *)"Failed to read data from DHT11.\r\n", 34, HAL_MAX_DELAY);
}
HAL_Delay(2000);
}
}
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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin : PB8 */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
```
6. 在工程中添加一个名为dht11.h的头文件,并在其中定义以下内容:
```c
#ifndef DHT11_H
#define DHT11_H
#include "main.h"
typedef enum {
DHT11_OK,
DHT11_ERROR
} DHT11_Status_TypeDef;
typedef struct {
uint8_t humidity;
uint8_t temperature;
} DHT11_Data_TypeDef;
DHT11_Status_TypeDef DHT11_ReadData(DHT11_Data_TypeDef *data);
#endif
```
7. 在工程中添加一个名为dht11.c的源文件,并在其中实现以下函数:
```c
#include "dht11.h"
#define DHT11_TIMEOUT 10000
static void DHT11_SetPinMode(uint16_t GPIO_Pin, GPIO_InitTypeDef *GPIO_InitStruct)
{
GPIO_InitStruct->Pin = GPIO_Pin;
GPIO_InitStruct->Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct->Pull = GPIO_NOPULL;
GPIO_InitStruct->Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, GPIO_InitStruct);
}
static void DHT11_SetPinInputMode(uint16_t GPIO_Pin, GPIO_InitTypeDef *GPIO_InitStruct)
{
GPIO_InitStruct->Pin = GPIO_Pin;
GPIO_InitStruct->Mode = GPIO_MODE_INPUT;
GPIO_InitStruct->Pull = GPIO_PULLUP;
GPIO_InitStruct->Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, GPIO_InitStruct);
}
static DHT11_Status_TypeDef DHT11_Start(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
DHT11_SetPinMode(GPIO_PIN_8, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_Delay(18);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
HAL_Delay(40);
DHT11_SetPinInputMode(GPIO_PIN_8, &GPIO_InitStruct);
uint32_t timeout = DHT11_TIMEOUT;
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET)
{
if (timeout-- == 0)
return DHT11_ERROR;
}
timeout = DHT11_TIMEOUT;
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET)
{
if (timeout-- == 0)
return DHT11_ERROR;
}
return DHT11_OK;
}
static uint8_t DHT11_ReadByte(void)
{
uint8_t value = 0;
for (uint8_t i = 0; i < 8; i++)
{
uint32_t timeout = DHT11_TIMEOUT;
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_RESET)
{
if (timeout-- == 0)
return 0;
}
HAL_Delay(30);
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET)
value |= (1 << (7 - i));
timeout = DHT11_TIMEOUT;
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == GPIO_PIN_SET)
{
if (timeout-- == 0)
return 0;
}
}
return value;
}
DHT11_Status_TypeDef DHT11_ReadData(DHT11_Data_TypeDef *data)
{
uint8_t buffer[5] = {0};
if (DHT11_Start() != DHT11_OK)
return DHT11_ERROR;
for (uint8_t i = 0; i < 5; i++)
buffer[i] = DHT11_ReadByte();
if ((buffer[0] + buffer[1] + buffer[2] + buffer[3]) != buffer[4])
return DHT11_ERROR;
data->humidity = buffer[0];
data->temperature = buffer[2];
return DHT11_OK;
}
```
8. 编译并下载程序到STM32F051C8单片机中。
这样,当你连接DHT11到PB8引脚,并将串口连接到PA9和PA10引脚后,你就可以通过串口打印出DHT11传感器读取到的温度和湿度值了。
阅读全文