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

时间:2025-09-17  作者: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

    猜您喜欢

    电阻作为电子元件中的基础元件,其性能和规格直接影响着产品的稳定性和精度。SUPEROHM(美隆)作为知名的电阻制造商,其四端子电阻因高精度和低温漂特性,应用于高...
    2023-11-28 01:41:29

    网格带作为新型的传输和储存材料,近年来在各个行业中逐渐受到关注。其主要优势体现在以下几个方面。网格带具有优异的透气性。其独特的网状结构使得空气流通更加顺畅,能够...
    2020-06-17 00:00:00

    20世纪80年代初,Intel公司推出了MCS-51单片机,随后Intel以专利转让的形式把8051内核发布给许多半导体厂家,从而出现了许多与MCS-51系统兼...
    2020-09-28 23:35:00

    在选购螺丝批套装时,了解其规格和尺寸是非常重要的。市面上的螺丝批套装通常包括多种类型和尺寸的螺丝批,以满足不同的使用需求。常见的螺丝批规格包括一字螺丝批和十字螺...
    2013-10-15 00:00:00

    NTC热敏电阻(Negative Temperature Coefficient Thermistor)是一种对温度变化敏感的电子元件,应用于温度测量、温控系统...
    2025-03-18 01:01:39

    金属管件接头因其独特的性能和优越的耐用性,成为工业和建筑领域中应用的重要组件。金属管件接头具有极高的强度和耐压性,能够承受高温和高压环境,确保系统的安全和稳定。...
    2010-06-01 00:00:00

    CAN控制器是用于控制器局域网络(CAN)通信的关键组件。在汽车、工业自动化及其嵌入式系统中有着着重要作用。CAN控制器负责将数据转换为适合在网络上传输的格式,...
    2010-08-14 00:00:00

    气动螺丝批作为现代工业中不可少的工具,其规格尺寸直接影响到工作效率和使用便捷性。气动螺丝批的规格主要包括转速、扭矩、和尺寸等方面。常见的转速范围在2000至40...
    2015-02-05 00:00:00

    保险丝作为重要的保护元件,有着着不可替代的作用。创琪保险丝作为国内知名的保险丝品牌,优良的品质和多样的参数规格,受到了关注和认可。本文将围绕“创琪保险丝有什么参...
    2022-09-22 17:16:30

    智能手机已经成为我们生活中重要的一部分,而电池续航能力则是影响手机使用体验的关键因素。为了延长电池寿命,很多手机厂商都推出了优化电池充电功能。那么,这个功能究竟...
    2024-05-19 00:00:00