编写dht11温湿度传感器的stm32程序
时间: 2023-08-29 22:11:14 浏览: 116
以下是使用STM32的HAL库编写DHT11温湿度传感器程序的示例代码:
```c
#include "stm32f4xx_hal.h"
#define DHT11_PORT GPIOA
#define DHT11_PIN GPIO_PIN_0
TIM_HandleTypeDef htim2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
while (1)
{
// 等待 1s
HAL_Delay(1000);
// 发送开始信号
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_RESET);
HAL_Delay(20);
HAL_GPIO_WritePin(DHT11_PORT, DHT11_PIN, GPIO_PIN_SET);
HAL_Delay(30);
// 接收数据
uint8_t buffer[5] = {0};
uint8_t bit_count = 0;
uint8_t byte_count = 0;
uint8_t data = 0;
for (int i = 0; i < 40; i++)
{
// 等待下一位数据的到来
while (!HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN));
// 计时,以确定数据位是 0 还是 1
TIM2->CNT = 0;
while (HAL_GPIO_ReadPin(DHT11_PORT, DHT11_PIN));
uint16_t time = TIM2->CNT;
if (time > 40)
{
// 数据位为 1
data |= (uint8_t)(1 << (7 - bit_count));
}
bit_count++;
if (bit_count == 8)
{
buffer[byte_count] = data;
byte_count++;
data = 0;
bit_count = 0;
}
}
// 验证校验和
if (buffer[4] == (buffer[0] + buffer[1] + buffer[2] + buffer[3]))
{
// 计算温度和湿度
uint16_t humidity = ((uint16_t)buffer[0] << 8) | buffer[1];
uint16_t temperature = ((uint16_t)buffer[2] << 8) | buffer[3];
// 输出温度和湿度
float h = (float)humidity / 10.0f;
float t = (float)temperature / 10.0f;
printf("Humidity: %.1f%% Temperature: %.1fC\r\n", h, t);
}
else
{
printf("Checksum error!\r\n");
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
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();
}
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_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = DHT11_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DHT11_PORT, &GPIO_InitStruct);
}
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
__HAL_RCC_TIM2_CLK_ENABLE();
htim2.Instance = TIM2;
htim2.Init.Prescaler = 100 - 1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 65535;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
```
请注意,此代码仅供参考,您应该根据您的具体硬件和需求进行修改。在编写自己的程序之前,请确保您已经正确地连接了DHT11传感器。
阅读全文