首页 > 技术 > 内容

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

时间:2026-01-23  作者:Diven  阅读:0

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

二、硬件准备
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

    猜您喜欢


    应急物品放置柜是专门设计用于存放各种应急物品的柜子,旨在确保在突发事件发生时,能够迅速获取所需的救援设备和物资。通常配备了急救包、灭火器、手电筒、饮用水、食品以...
    2008-04-16 00:00:00
    RF射频同轴连接器是应用于无线通信、广播电视和卫星通信等领域的重要组件。主要用于连接射频电缆,以实现信号的传输和接收。RF同轴连接器的设计考虑了频率特性和阻抗匹...
    2009-05-28 00:00:00
    电阻器作为基础且重要的元件,应用于各种电路设计中。尤其是瓷管电阻,由于其良好的耐高温性能和稳定的电阻值,成为许多高精密设备和工业控制系统的首选。科达嘉(CODA...
    2018-05-04 15:13:35
    黄油规格尺寸是多少?在购买黄油时,了解其规格和尺寸非常重要。一般而言,市面上的黄油通常以块状出售,常见的规格有227克(即半磅)和454克(即一磅)。黄油也可以...
    2018-09-12 00:00:00
    高扇出 net 是时序收敛的一个常见瓶颈。所以,除了传统的降低扇出的方法之外,还可以将该 net 引入 BUFG,但前提是有可用的 BUFG。众所周知,BUFG...
    2018-04-10 18:06:00
    贴片电阻本身并不直接标注耐压值。其承受电压能力主要取决于封装尺寸,而非阻值大小。一般来说,封装尺寸越大,其耐压能力越强。常见的0201封装的贴片电阻,通常耐压为...
    2024-11-29 10:25:41
    以太网接口的电压承受能力并非一个简单的数值,涉及到多种因素,包括接口标准、线缆质量、以及瞬时电压等。理解这些因素有助于保护你的网络设备免受损害。一般来说,标准的...
    2024-08-30 00:00:00
    现代电子元件中,贴片二极管因其体积小、性能优越而被应用于各种电子设备中。了解贴片二极管的型号标注含义,不仅有助于我们在选择和使用时做出更明智的决策,还能提高电路...
    2025-04-06 14:31:39
    贴片电阻0201,以其极小的尺寸在电子世界中是举足轻重的配件。这种表面贴装电阻器(SMD)仅有0.25mm x 0.125mm的大小,比一粒芝麻还小,却能有效控...
    2024-11-29 10:26:32