DHT11驱动代码,STM32F103ZET6
时间: 2023-06-20 21:04:19 浏览: 122
以下是使用STM32CubeMX和HAL库驱动DHT11的示例代码:
```c
#include "main.h"
#include "dht11.h"
/* DHT11数据接口引脚定义 */
#define DHT11_GPIO_PORT GPIOA
#define DHT11_GPIO_PIN GPIO_PIN_1
/* 定义DHT11的状态 */
typedef enum {
DHT11_IDLE = 0U,
DHT11_READY,
DHT11_ERROR
} DHT11_StatusTypeDef;
/* 定义DHT11结构体 */
typedef struct {
DHT11_StatusTypeDef Status;
uint8_t Humidity;
uint8_t Temperature;
} DHT11_HandleTypeDef;
/* 定义DHT11句柄 */
DHT11_HandleTypeDef DHT11;
/* DHT11引脚初始化 */
static void DHT11_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* 打开GPIOA时钟 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* 配置GPIOA.1为输入模式 */
GPIO_InitStruct.Pin = DHT11_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStruct);
}
/* 延时函数 */
static void DHT11_Delay_us(uint32_t us)
{
uint32_t cnt = us * (SystemCoreClock / 1000000) / 5;
while (cnt--);
}
/* 读取DHT11数据 */
static HAL_StatusTypeDef DHT11_ReadData(void)
{
uint8_t i = 0;
uint8_t data[5] = {0};
uint8_t checksum = 0;
/* 发送起始信号 */
HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_RESET);
DHT11_Delay_us(18000);
HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_SET);
DHT11_Delay_us(40);
/* 切换到输入模式 */
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = DHT11_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
/* 等待DHT11响应信号 */
if (HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN) == GPIO_PIN_RESET) {
while (!HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
while (HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
/* 读取40位数据 */
for (i = 0; i < 40; i++) {
while (!HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
DHT11_Delay_us(50);
if (HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN) == GPIO_PIN_RESET) {
data[i / 8] <<= 1;
} else {
data[i / 8] <<= 1;
data[i / 8] |= 0x01;
}
while (HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
}
/* 检查校验和 */
checksum = data[0] + data[1] + data[2] + data[3];
if (data[4] == checksum) {
DHT11.Humidity = data[0];
DHT11.Temperature = data[2];
return HAL_OK;
}
}
return HAL_ERROR;
}
/* DHT11初始化 */
void DHT11_Init(void)
{
/* 初始化DHT11引脚 */
DHT11_GPIO_Init();
/* 初始化DHT11句柄 */
DHT11.Status = DHT11_IDLE;
DHT11.Humidity = 0;
DHT11.Temperature = 0;
}
/* 读取DHT11数据 */
HAL_StatusTypeDef DHT11_Read(DHT11_HandleTypeDef *hdht11)
{
HAL_StatusTypeDef status = HAL_OK;
uint32_t timeout = 0;
/* 检查状态 */
if (hdht11->Status == DHT11_IDLE) {
/* 标记为就绪状态 */
hdht11->Status = DHT11_READY;
/* 发送读取命令 */
status = DHT11_ReadData();
/* 延时等待下一次读取 */
HAL_Delay(2000);
/* 标记为闲置状态 */
hdht11->Status = DHT11_IDLE;
} else {
status = HAL_BUSY;
}
return status;
}
```
使用示例:
```c
#include "main.h"
#include "dht11.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
DHT11_Init();
while (1) {
if (DHT11_Read(&DHT11) == HAL_OK) {
printf("Humidity: %d%%, Temperature: %dC\r\n", DHT11.Humidity, DHT11.Temperature);
} else {
printf("Error reading DHT11 data!\r\n");
}
HAL_Delay(1000);
}
}
```
阅读全文