前言
涵盖之前文章:
- Clion开发STM32之HAL库GPIO宏定义封装(最新版)
- Clion开发stm32之微妙延迟(采用nop指令实现)
- Clion开发STM32之日志模块(参考RT-Thread)
DHT11驱动文件
头文件
#ifndef F1XX_TEMPLATE_MODULE_DHT11_H
#define F1XX_TEMPLATE_MODULE_DHT11_H#include "sys_core.h"
typedef struct {void (*input_mode_set)(void);void (*out_mode_set)(void);void (*send_data)(uint32_t status);uint32_t (*read_data)(void);void (*us_delay)(uint32_t us);
} DHT11_conf_t;typedef struct {uint8_t humi_int; uint8_t humi_deci; uint8_t temp_int; uint8_t temp_deci; uint8_t check_sum;
} DHT11_data_t;void DHT11_conf_set(DHT11_conf_t *cnf);bool DHT11_Driver_Init(void);DHT11_data_t *DHT11_Read_TH(void);#endif
源文件
#include "dht11/module_dht11.h"#define DHT11_RESET 0
#define DHT11_SET 1static DHT11_conf_t *dht11_conf_ptr = NULL;
static DHT11_data_t dht11_data;
#define DHT11_OUT_0 dht11_conf_ptr->send_data(0)
#define DHT11_OUT_1 dht11_conf_ptr->send_data(1)static uint8_t DHT11_ReadByte(void);static inline void us_delay(uint32_t us) {dht11_conf_ptr->us_delay(us);
}static inline void ms_delay(uint32_t ms) {while (ms--) us_delay(1);
}void DHT11_conf_set(DHT11_conf_t *cnf) {dht11_conf_ptr = cnf;
}bool DHT11_Driver_Init(void) {if (dht11_conf_ptr == NULL) return false;dht11_conf_ptr->out_mode_set();DHT11_OUT_1;return true;
}
DHT11_data_t *DHT11_Read_TH(void) {if (dht11_conf_ptr == NULL) return NULL;dht11_conf_ptr->out_mode_set();DHT11_OUT_0;ms_delay(18);DHT11_OUT_1;us_delay(30); dht11_conf_ptr->input_mode_set();if (dht11_conf_ptr->read_data() == DHT11_RESET) {while (dht11_conf_ptr->read_data() == DHT11_RESET);while (dht11_conf_ptr->read_data() == DHT11_SET);dht11_data.humi_int = DHT11_ReadByte();dht11_data.humi_deci = DHT11_ReadByte();dht11_data.temp_int = DHT11_ReadByte();dht11_data.temp_deci = DHT11_ReadByte();dht11_data.check_sum = DHT11_ReadByte();dht11_conf_ptr->out_mode_set();DHT11_OUT_1;uint8_t result = dht11_data.humi_int + dht11_data.humi_deci + dht11_data.temp_int + dht11_data.temp_deci;if (dht11_data.check_sum == result) return &dht11_data;}return NULL;
}
static uint8_t DHT11_ReadByte(void) {uint8_t i, temp = 0;for (i = 0; i < 8; i++) {while (dht11_conf_ptr->read_data() == DHT11_RESET);us_delay(40); if (dht11_conf_ptr->read_data() == DHT11_SET){while (dht11_conf_ptr->read_data() == DHT11_SET);temp |= (uint8_t) (0x01 << (7 - i)); } else {temp &= (uint8_t) ~(0x01 << (7 - i)); }}return temp;
}
测试配置
#include "app_conf.h"#define APP_CONF_ENABLE_DHT11_CNF (1)
#if APP_CONF_ENABLE_DHT11_CNF#include "dht11/module_dht11.h"#define DBG_ENABLE
#define DBG_SECTION_NAME "DHT11_CNF"
#define DBG_LEVEL DBG_LOG #include "sys_dbg.h"
#define DS18B20_LINE PE6
static DHT11_conf_t dh11_conf;
static stm_pin_define_t *dh11_pin_ptr = NULL;static void out_mode_set(void) { stm32_pin_define_mode_set(dh11_pin_ptr, pin_mode_output); }static void input_mode_set(void) { stm32_pin_define_mode_set(dh11_pin_ptr, pin_mode_input); }static void send_data(uint32_t status) { stm32_pin_define_set(dh11_pin_ptr, status); }static uint32_t read_data(void) { return stm32_pin_define_read(dh11_pin_ptr); }
static void DHT11_CNF_pre_init() {dh11_pin_ptr = stm_get_pin(DS18B20_LINE);dh11_conf.us_delay = bsp_us_delay_nop;dh11_conf.out_mode_set = out_mode_set;dh11_conf.input_mode_set = input_mode_set;dh11_conf.send_data = send_data;dh11_conf.read_data = read_data;DHT11_conf_set(&dh11_conf);
}sys_pre_init_export(DHT11_CNF, DHT11_CNF_pre_init);
static void DHT11_CNF_init() {DHT11_Driver_Init();
}sys_init_export(DHT11_CNF, DHT11_CNF_init);
static void DHT11_CNF_after_init() {DHT11_data_t *th = DHT11_Read_TH();if (th == NULL) {LOG_W("DHT11_Read_TH ERROR");} else {LOG_D("humi:%d.%d %RH ,temp: %d.%d℃", th->humi_int, th->humi_deci, th->temp_int, th->temp_deci);}
}sys_after_init_export(DHT11_CNF, DHT11_CNF_after_init);
#endif