RT-Thread与CPK-RA2L1采集DHT11温湿度数据

时间:2025-11-01  作者:Diven  阅读:0

一、准备
本篇文章主要介绍使用RT-Thread Studio 和瑞萨 CPK-RA2L1评估板,使用大佬的轮子采集温湿度

RT-Thread与CPK-RA2L1采集DHT11温湿度数据

二、硬件准备
CPK-RA2L1评估板, 这个板子的芯片型号是 R7FA2L1AB2DFM,DHT11 温湿度传感器

1.jpg

三、新建工程

1、总线空闲状态为高电平,主机把总线拉低等待DHT11响应,主机把总线拉低必须大于18毫秒,保证DHT11能检测到起始信号。DHT11接收到主机的开始信号后,等待主机开始信号结束,然后发送80us低电平响应信号.主机发送开始信号结束后,延时等待20-40us后, 读取DHT11的响应信号,主机发送开始信号后,可以切换到输入模式,或者输出高电平均可, 总线由上拉电阻拉高。

1.jpg

2、总线为低电平,说明DHT11发送响应信号,DHT11发送响应信号后,再把总线拉高80us,准备发送数据,每一bit数据都以50us低电平时隙开始,高电平的长短定了数据位是0还是1.格式见下面图示.如果读取响应信号为高电平,则DHT11没有响应,请检查线路是否连接正常.当最后一bit数据传送完毕后,DHT11拉低总线50us,随后总线由上拉电阻拉高进入空闲状态

1.jpg

3、数字0信号

1.jpg

4、数字1信号

1.jpg

四、驱动代码


int split_int(const int num, int *integer, int *decimal, const rt_uint32_t times)
{
int flag = 0;
if (num < 0) flag = 1;
int anum = num<0 ? -num : num;
integer = anum / times;
decimal = anum % times;
return flag;
}
/

  • This function will convert temperature in degree Celsius to Kelvin.
  • @param c the temperature indicated by degree Celsius
  • @return the result
    /
    float convert_c2k(float c)
    {
    return c + 273.15;
    }
    /
    *
  • This function will convert temperature in degree Celsius to Fahrenheit.
  • @param c the temperature indicated by degree Celsius
  • @return the result
    /
    float convert_c2f(float c)
    {
    return c * 1.8 + 32;
    }
    /
    *
  • This function will convert temperature in degree Fahrenheit to Celsius.
  • @param f the temperature indicated by degree Fahrenheit
  • @return the result
    /
    float convert_f2c(float f)
    {
    return (f - 32) * 0.55555;
    }
    /
    *
  • This function will read a bit from sensor.
  • @param pin the pin of Dout
  • @return the bit value
    /
    static uint8_t dht_read_bit(const rt_base_t pin)
    {
    uint8_t retry = 0;
    while(rt_pin_read(pin) && retry < DHTxx_REPLY_TIME)
    {
    retry++;
    rt_hw_us_delay(1);
    }
    retry = 0;
    while(!rt_pin_read(pin) && retry < DHTxx_REPLY_TIME)
    {
    retry++;
    rt_hw_us_delay(1);
    }
    rt_hw_us_delay(MEASURE_TIME);
    return rt_pin_read(pin);
    }
    /
    *
  • This function will read a byte from sensor.
  • @param pin the pin of Dout
  • @return the byte
    */
    static uint8_t dht_read_byte(const rt_base_t pin)
    {
    uint8_t i, byte = 0;
    for(i=0; i<8; i++)
    {
    byte <<= 1;
    byte |= dht_read_bit(pin);
    }
    return byte;
    }

    for(i=0; i {
    sum += dev->data[i];
    }
    if(sum != dev->data[4]) return RT_FALSE;
    return RT_TRUE;
    }

    rt_err_t dht_init(struct dht_device dev, const rt_base_t pin)
    {
    if(dev == NULL)
    return -RT_ERROR;
    dev->type = DHT_TYPE;
    dev->pin = pin;
    rt_memset(dev->data, 0, DHT_DATA_SIZE);
    rt_pin_mode(dev->pin, PIN_MODE_INPUT_PULLUP);
    return RT_EOK;
    }
    // 1、初始化类型
    dht_device_t dht_create(const rt_base_t pin)
    {
    dht_device_t dev;
    dev = rt_calloc(1, sizeof(struct dht_device));
    if (dev == RT_NULL)
    {
    LOG_E("Can't allocate memory for dhtxx device");
    return RT_NULL;
    }
    dev->type = DHT_TYPE;
    dev->pin = pin;
    rt_memset(dev->data, 0, DHT_DATA_SIZE);
    rt_pin_mode(dev->pin, PIN_MODE_INPUT_PULLUP);
    return dev;
    }
    void dht_delete(dht_device_t dev)
    {
    if (dev)
    rt_free(dev);
    }
    /

    Copyright (c) 2006-2021, RT-Thread Development Team
  • SPDX-License-Identifier: Apache-2.0

    Change Logs:
    Date Author Notes
    2023-03-01 DYC the first version
    /
    #ifndef SRC_DHT11_H_
    #define SRC_DHT11_H_
    #include
    #include
    #include
    #include
    #include
    #define DHTLIB_VERSION "0.9.0"
    #define DHT_DATA_SIZE 5
    /
    sensor model type */
    #define DHT11 0
    #define DHT_TYPE DHT11
    struct dht_device
    {
    rt_base_t pin;
    rt_uint8_t type;
    rt_uint8_t data[DHT_DATA_SIZE];
    rt_mutex_t lock;
    };
    typedef struct dht_device *dht_device_t;
    dht_device_t dht_create(const rt_base_t pin);
    void dht_delete(dht_device_t dev);
    rt_err_t dht_init(struct dht_device *dev, const rt_base_t pin);
    rt_bool_t dht_read(dht_device_t dev);
    rt_int32_t dht_get_humidity(dht_device_t dev);
    rt_int32_t dht_get_temperature(dht_device_t dev);
    float convert_c2k(float c);//将摄氏温度转为开氏温度
    float convert_c2f(float c);//将摄氏温度转为华氏温度
    float convert_f2c(float f);//将华氏温度转为摄氏温度
    rt_int32_t split_int(const rt_int32_t num, rt_int32_t *integer,
    rt_int32_t *decimal, const rt_uint32_t times);
    rt_err_t rt_hw_dht_init(const char *name, struct rt_sensor_config cfg);
    #endif /
    SRC_DHT11_H_ */

    这里DHT11 使用的是 GPIO 0208,所以需要把这个引脚配置为输入模式

    1.jpg

    五、烧录验证

    1.jpg

    猜您喜欢

    整流桥二极管是电力电子设备中不可少的元件,应用于电源转换、整流电路以及各种电子设备中。随着科技的发展和电力电子技术的进步,市场上出现了多种型号的整流桥二极管,满...
    2025-04-09 13:00:37

    电流检测电阻作为电子电路中的关键元件,越来越受到工程师和设计师的关注。作为知名的电子元器件制造商,Kyocera(京瓷)在电流检测电阻领域也拥有丰富的产品线和技...
    2016-11-24 06:18:49

    随着科技的不断进步,数据传输和设备连接的需求日益增加。USB接口作为一种广泛使用的连接标准,其发展也在不断演变。USB4-TYPEA_13.1X14MM_TM是...
    2025-03-08 04:47:31

    贴片排阻作为重要的电子元器件,因其体积小、性能稳定、安装方便而被应用。金山(ELITE)作为国内知名的电子元件生产厂家,其贴片排阻产品因优良的品质和多样的类型获...
    2013-12-12 12:02:51

    雪崩二极管(Avalanche Diode)是特殊类型的二极管,应用于过压保护和高频信号处理等领域。由于其独特的工作原理,测量雪崩二极管的性能和参数显得尤为重要...
    2025-03-30 04:01:39

    贴片电阻,作为电子电路中很重要的元件,其阻值会随着温度的变化而发生改变。这种变化程度,就是我们所说的贴片电阻温度系数,通常用ppm/℃(百万分之一每摄氏度)来表...
    2024-11-29 10:25:52

    许多人对以太网反向供电 (PoE) 适配器存在一个疑问:会比普通电源适配器更费电吗?答案并非绝对。PoE适配器的工作原理PoE技术通过一根网线同时传输数据和电力...
    2024-09-12 00:00:00

    DCDC开关芯片是现代电子产品中重要的核心部件,应用于各种消费电子、工业设备以及汽车电子中。的出现与发展,推动了电子设备效率和性能的显著提升,使其在满足一般用户...
    2024-01-04 00:00:00

    贴片电阻上的1002标识并非直接代表阻值,而是表示其尺寸大小。1002是一种封装尺寸代码,指的是电阻的长和宽分别为1.0mm和0.25mm(公制)。要确定100...
    2024-11-29 10:26:35

    贴片电阻上的9090并不是直接代表电阻值,而是表示尺寸。9090指的是0909封装尺寸,即英制表示法为0.09英寸 x 0.09英寸,公制表示法为2.286mm...
    2024-11-29 10:26:21