硬汉嵌入式论坛

 找回密码
 立即注册
查看: 877|回复: 4
收起左侧

[技术讨论] 本人写了一份dht11的函数接口,求建议

[复制链接]

5

主题

15

回帖

30

积分

新手上路

积分
30
发表于 2024-7-24 18:25:43 | 显示全部楼层 |阅读模式
/*
* bsp_dht11.c
*
*  Created on: Jul 24, 2024
*      Author: vuxtvg@163.com
*/

#include "bsp_dht11.h"

static void (*g_delay_ms_func)(uint32_t) = NULL;
static void (*g_delay_us_func)(uint32_t) = NULL;
static void (*g_set_out_func)(void) = NULL;
static void (*g_set_in_func)(void) = NULL;
static void (*g_set_out_pin)(uint8_t state) = NULL;
static uint8_t (*g_read_in_pin)(void) = NULL;

static void dht11_delay_ms(uint32_t ms) {
        if (g_delay_ms_func != NULL) {
                g_delay_ms_func(ms);
        }
}

static void dht11_delay_us(uint32_t us) {
        if (g_delay_us_func != NULL) {
                g_delay_us_func(us);
        }
}

static void dht11_set_out_func(void) {
        if (g_set_out_func != NULL) {
                g_set_out_func();
        }
}

static void dht11_set_in_func(void) {
        if (g_set_in_func != NULL) {
                g_set_in_func();
        }
}

static void dht11_set_out_pin(uint8_t state) {
        if (g_set_out_pin != NULL) {
                g_set_out_pin(state);
        }
}

static uint8_t dht11_read_in_pin(void) {
        if (g_read_in_pin != NULL) {
                return g_read_in_pin();
        } else {
                return 0;
        }
}

//复位DHT11  \\起始
static void dhtll_rescb() {
        dht11_set_out_func();
        dht11_set_out_pin(0); //拉低DQ
        dht11_delay_ms(20);
        dht11_set_out_pin(1);        //DQ=1
        dht11_delay_us(30);             //主机拉高20~40us
}

//等待DHT11的回应
//返回1:未检测到DHT11的存在
//返回0:存在
static uint8_t dht11_checkc() {
        uint8_t retry = 0;
        dht11_set_in_func();             //SET INPUT
        while (dht11_read_in_pin() && retry < 100)             //DHT11会拉低40~80us
        {
                retry++;
                dht11_delay_us(1);
        };
        if (retry >= 100)
                return 1;
        else
                retry = 0;
        while (!dht11_read_in_pin() && retry < 100)             //DHT11拉低后会再次拉高40~80us
        {
                retry++;
                dht11_delay_us(1);
        };
        if (retry >= 100)
                return 1;
        return 0;
}

//从DHT11读取一个位
//返回值:1/0
static uint8_t dth11_read_bitc() {
        uint8_t retry = 0;
        while (dht11_read_in_pin() && retry < 100)             //等待变为低电平
        {
                retry++;
                dht11_delay_us(1);
        }             //延时100
        retry = 0;
        while (!dht11_read_in_pin() && retry < 100)             //等待变高电平
        {
                retry++;
                dht11_delay_us(1);
        }
        dht11_delay_us(40);             //等待40us
        if (dht11_read_in_pin())
                return 1;
        else
                return 0;
}

//从DHT11读取一个字节
//返回值:读到的数据DHT11_Read_ByteC
static uint8_t dht11_read_bytec() {
        uint8_t i, dat;
        dat = 0;
        for (i = 0; i < 8; i++) {
                dat <<= 1;
                dat |= dth11_read_bitc();
        }
        return dat;
}

//初始化DHT11的IO口  同时检测DHT11的存在
//返回1:不存在
//返回0:存在
static uint8_t dht11_initC() {
        dht11_set_out_func();
        dht11_set_out_pin(1);        // 输出高
        dhtll_rescb();  //复位DHT11
        return dht11_checkc();  //等待DHT11的回应
}

void dht11_module_initC(dht11_module_cfg_t *cfg) {
        g_delay_us_func = cfg->delay_us;
        g_delay_ms_func = cfg->delay_ms;
        g_read_in_pin = cfg->read_in_pin;
        g_set_out_pin = cfg->set_out_pin;
        g_set_in_func = cfg->set_in;
        g_set_out_func = cfg->set_out;
        dht11_initC();

}

uint8_t dht11_module_read_dataC(float *temperature, float *humidity) {

        uint8_t buf[5];
        uint8_t i;
        dhtll_rescb();
        if (dht11_checkc() == 0) {
                for (i = 0; i < 5; i++) {
                        buf = dht11_read_bytec(); // 读取40位数据
                }
                if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) {
                        *humidity = buf[0] + buf[1] / 10.0;
                        *temperature = buf[2] + buf[3] / 10.0;
                        return 0;
                }
        }
        return 1;

}




/*
* bsp_dht11.h
*
*  Created on: Jul 24, 2024
*      Author: vuxtvg@163.com
*/

#ifndef BSP_DHT11_H_
#define BSP_DHT11_H_

#include <stdlib.h>
#include <stdint.h>

// Configuration structure
typedef struct {
        void (*delay_us)(uint32_t us); //Us延时函数
        void (*delay_ms)(uint32_t ms); //Ms延时函数
        void (*set_out)(void); //设置引脚输出
        void (*set_in)(void); //设置引脚输入
        void (*set_out_pin)(uint8_t state);  //输出引脚控制
        uint8_t (*read_in_pin)(void); //输入引脚读取
} dht11_module_cfg_t;

void dht11_initc(void);
void dht11_module_initC(dht11_module_cfg_t *cfg);
uint8_t dht11_module_read_dataC(float *temperature, float *humidity);
#endif /* BSP_DHT11_H_ */


调用接口示例

#include "main.h"
#include "bsp_dht11.h"
#include "dwt_delay.h"

#define DHT11_GPIO_PORT                 GPIOB
#define DHT11_GPIO_PIN                         GPIO_PIN_1
#define DHT11_OUT_PORT                        PBout(1)
#define DHT11_IN_PORT                                PBin(1)
static void DHT11_SET_IN(void) {
        __HAL_RCC_GPIOB_CLK_ENABLE();
        GPIO_InitTypeDef GPIO_InitStruct = { 0 };
        GPIO_InitStruct.Pin = DHT11_GPIO_PIN;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        HAL_GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStruct);
}

static void DHT11_SET_OUT(void) {
        __HAL_RCC_GPIOB_CLK_ENABLE();
        GPIO_InitTypeDef GPIO_InitStruct = { 0 };
        GPIO_InitStruct.Pin = DHT11_GPIO_PIN;
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(DHT11_GPIO_PORT, &GPIO_InitStruct);
}
void set_out_pin(uint8_t state) {
        DHT11_OUT_PORT = state;
}

// Example function to read the IN pin state
uint8_t read_in_pin(void) {
        return DHT11_IN_PORT;
}

void dht11_initc(void) {
        dht11_module_cfg_t dht11_config;
        dht11_config.delay_ms = delay_ms_dwt;
        dht11_config.delay_us = delay_us_dwt;
        dht11_config.read_in_pin = read_in_pin;
        dht11_config.set_in = DHT11_SET_IN;
        dht11_config.set_out = DHT11_SET_OUT;
        dht11_config.set_out_pin = set_out_pin;
        dht11_module_initC(&dht11_config);
}
void get_temp_humi(void)
{
        float temperature, humidity;
        dht11_module_read_dataC(&temperature, &humidity);
        printf("temperature:%.1f humidity:%.1f\r\n",temperature,humidity);
}


还请给一些建议



回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117530
QQ
发表于 2024-7-25 11:46:35 | 显示全部楼层
做的挺好的,很规范。
回复

使用道具 举报

1

主题

2

回帖

5

积分

新手上路

积分
5
发表于 2024-7-26 11:40:25 来自手机 | 显示全部楼层
突然发现群友了,振南老师的RTOS我还没看呢
回复

使用道具 举报

5

主题

15

回帖

30

积分

新手上路

积分
30
 楼主| 发表于 2024-7-28 00:43:00 | 显示全部楼层
myz 发表于 2024-7-26 11:40
突然发现群友了,振南老师的RTOS我还没看呢

哈哈哈,可以,我看了部分,他没更新完
回复

使用道具 举报

5

主题

15

回帖

30

积分

新手上路

积分
30
 楼主| 发表于 2024-7-28 01:24:14 | 显示全部楼层
eric2013 发表于 2024-7-25 11:46
做的挺好的,很规范。

谢谢大佬回复
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-12 18:22 , Processed in 0.037714 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表