<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

使用RT-Thread和CPK-RA2L1采集DHT11溫濕度

冬至子 ? 來源:快樂小鳥 ? 作者:快樂小鳥 ? 2023-10-11 11:34 ? 次閱讀

一、準備
本篇文章主要介紹使用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

四、驅動代碼

/*

  • 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
    /
    #include "dht11.h"
    #include
    #include "hal_data.h"
    #include
    #define DBG_TAG "sensor.asair.dhtxx"
    #ifdef PKG_USING_DHTXX_DEBUG
    #define DBG_LVL DBG_LOG
    #else
    #define DBG_LVL DBG_ERROR
    #endif
    #include
    /
    timing /
    #define DHT1x_BEGIN_TIME 20 /
    ms /
    #define DHT2x_BEGIN_TIME 1 /
    ms /
    #define DHTxx_PULL_TIME 30 /
    us /
    #define DHTxx_REPLY_TIME 100 /
    us /
    #define MEASURE_TIME 40 /
    us /
    RT_WEAK void rt_hw_us_delay(rt_uint32_t us)
    {
    rt_uint32_t delta;
    us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
    delta = SysTick->VAL;
    while (delta - SysTick->VAL < us) continue;
    }
    /
    *
  • This function will split a number into two part according to times.
  • @param num the number will be split
  • @param integer the integer part
  • @param decimal the decimal part
  • @param times how many times of the real number (you should use 10 in this case)
  • @return 0 if num is positive, 1 if num is negative
    */
    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;
    }
    /**
  • This function will read and update data array.
  • @param dev the device to be operated
  • @return RT_TRUE if read successfully, otherwise return RT_FALSE.
    /
    rt_bool_t dht_read(dht_device_t dev)
    {
    RT_ASSERT(dev);
    uint8_t i, retry = 0, sum = 0;
    #ifdef PKG_USING_DHTXX_INTERRUPT_DISABLE
    rt_base_t level;
    #endif
    /
    Reset data buffer /
    rt_memset(dev->data, 0, DHT_DATA_SIZE);
    /
    MCU request sampling /
    rt_pin_mode(dev->pin, PIN_MODE_OUTPUT);
    rt_pin_write(dev->pin, PIN_LOW);
    if (dev->type == DHT11) {
    rt_thread_mdelay(DHT1x_BEGIN_TIME); /
    Tbe /
    } else {
    rt_thread_mdelay(DHT2x_BEGIN_TIME);
    }
    #ifdef PKG_USING_DHTXX_INTERRUPT_DISABLE
    level = rt_hw_interrupt_disable();
    #endif
    rt_pin_mode(dev->pin, PIN_MODE_INPUT_PULLUP);
    rt_hw_us_delay(DHTxx_PULL_TIME); /
    Tgo /
    /
    Waiting for sensor reply /
    while (rt_pin_read(dev->pin) && retry < DHTxx_REPLY_TIME)
    {
    retry++;
    rt_hw_us_delay(1); /
    Trel /
    }
    if(retry >= DHTxx_REPLY_TIME) return RT_FALSE;
    retry = 0;
    while (!rt_pin_read(dev->pin) && retry < DHTxx_REPLY_TIME)
    {
    retry++;
    rt_hw_us_delay(1); /
    Treh /
    };
    if(retry >= DHTxx_REPLY_TIME) return RT_FALSE;
    /
    Read data /
    for(i=0; i {
    dev->data[i] = dht_read_byte(dev->pin);
    }
    #ifdef PKG_USING_DHTXX_INTERRUPT_DISABLE
    rt_hw_interrupt_enable(level);
    #endif
    /
    Checksum */
    for(i=0; i {
    sum += dev->data[i];
    }
    if(sum != dev->data[4]) return RT_FALSE;
    return RT_TRUE;
    }
    /**
  • This function will get the humidity from dhtxx sensor.
  • @param dev the device to be operated
  • @return the humidity value
    /
    rt_int32_t dht_get_humidity(dht_device_t const dev)
    {
    RT_ASSERT(dev);
    rt_int32_t humi = 0;
    switch(dev->type)
    {
    case DHT11:
    humi = dev->data[0] * 10 + dev->data[1];
    break;
    default:
    break;
    }
    return humi;
    }
    /
    *
  • This function will get the temperature from dhtxx sensor.
  • @param dev the device to be operated
  • @return the temperature value
    /
    rt_int32_t dht_get_temperature(dht_device_t const dev)
    {
    RT_ASSERT(dev);
    rt_int32_t temp = 0;
    switch(dev->type)
    {
    case DHT11:
    temp = dev->data[2] * 10 + (dev->data[3] & 0x7f);
    if(dev->data[3] & 0x80) {
    temp = -temp;
    }
    break;
    default:
    break;
    }
    return temp;
    }
    /
    *
  • This function will init dhtxx sensor device.
  • @param dev the device to init
  • @param pin the pin of Dout
  • @return the device handler
    */
    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

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 上拉電阻
    +關注

    關注

    5

    文章

    351

    瀏覽量

    30329
  • 溫濕度傳感器

    關注

    5

    文章

    550

    瀏覽量

    35381
  • GPIO
    +關注

    關注

    16

    文章

    1150

    瀏覽量

    50781
  • DHT11
    +關注

    關注

    19

    文章

    265

    瀏覽量

    57284
  • RT-Thread
    +關注

    關注

    31

    文章

    1189

    瀏覽量

    39020
收藏 人收藏

    評論

    相關推薦

    基于arduino的dht11溫濕度傳感器的使用

    本文介紹了DHT11溫濕度傳感器電氣特性、DHT11封裝形式及接口說明與典型應用電路,其次介紹了DHT11溫濕度傳感器時序圖與連接圖,最后介
    發表于 01-22 15:50 ?4.4w次閱讀
    基于arduino的<b class='flag-5'>dht11</b><b class='flag-5'>溫濕度</b>傳感器的使用

    CC2541用DHT11傳感器采集溫濕度

    CC2541采集DHT11溫濕度值。使用的協議棧版本:BLE-CC254x-1.4.0編譯軟件:IAR 8.20.2,硬件平臺:Smart RF(主芯片CC2541)。二、基礎知識1
    發表于 04-14 09:41

    DHT11溫濕度數據的采集

    用的控制器是STM32F103C8T6,如果你用STMF103的其他芯片來跑這個代碼也能跑通,基本配置都是一樣的。先介紹DHT11溫濕度數據的采集,有兩個文件,一個DHT11.c,還有
    發表于 07-16 06:24

    DHT11溫濕度傳感器介紹

    DHT11溫濕度傳感器介紹,1.實物原理圖2.模塊說明2.1 DHT11產品概述DHT11數字
    發表于 07-21 09:04

    基于STM32開發板實現傳感數據采集-DHT11溫濕度采集

    STM32F407ZGT6開發板進行項目開發,選用的傳感器為常見通用的DHT11溫濕度傳感器。傳感器將采集到的數據傳輸到STM32(MCU)主控進行數據處理,最后通過串口打印出來。軟硬件環境:硬件:stm32開發板、
    發表于 08-10 07:41

    DHT11數字溫濕度傳感器的相關資料推薦

    DHT溫濕度1602顯示DHT11溫濕度相關介紹DHT11產品概述1.測量范圍
    發表于 11-19 07:33

    DHT11溫濕度傳感器簡介

    DHT11溫濕度傳感器1、DHT11簡介DHT11數字溫濕度傳感器是一款含有已校準數字信號輸出的
    發表于 02-16 06:55

    怎樣去采集DHT11傳感器的溫濕度參數呢

    15x_StdPeriph_Driver:STM8自帶庫文件4.Debug:hex文件存放于EXE文件夾5.Function: 采集DHT11傳感器的溫濕度參數,串口打印低功耗設計時,常用的傳感器參數
    發表于 02-21 07:35

    使用RT-Thread Studio和CPK-RA2L1板點亮0.96寸OLED ssd1306

      一、準備  本篇文章主要介紹使用RT-Thread Studio 和瑞薩 CPK-RA2L1評估板,使用大佬的輪子來點亮0.96寸 OLED ssd1306,  二、硬件準備  首先準備一個
    發表于 04-03 16:14

    使用RT-ThreadCPK-RA2L1采集溫濕度

      一、準備  本篇文章主要介紹使用RT-Thread Studio 和瑞薩 CPK-RA2L1評估板,使用大佬的輪子采集溫濕度  二、硬件準備  
    發表于 04-03 16:20

    DHT11采集溫濕度源程序

    DHT11采集溫濕度并用LCD12864顯示的源程序.可以使用的哈,分享給大家
    發表于 01-07 16:56 ?171次下載

    溫濕度DHT11資料

    溫濕度DHT11資料匯總 DHT11是一款有已校準數字信號輸出的溫濕度傳感器。 其精度濕度+-5%RH, 溫度+-2℃,量程
    發表于 11-29 17:28 ?25次下載

    基于RT-Thread + MicroLab,零基礎做溫濕度監控上位機

    GND board上帶在DHT11溫濕度傳感器,RT-Thread有相應的軟件包,直接利用簡單快捷。在RT-Thread studio添加DHT11
    的頭像 發表于 08-03 15:41 ?3338次閱讀

    【Renesas RA6M4開發板之DHT11溫濕度讀取】

    本篇通過Renesas RA6M4開發板DHT11溫濕度讀取示例程序演示。
    的頭像 發表于 01-18 17:18 ?1389次閱讀
    【Renesas <b class='flag-5'>RA</b>6M4開發板之<b class='flag-5'>DHT11</b><b class='flag-5'>溫濕度</b>讀取】

    CPK-RA2L1評估板PMS1003

    本篇文章主要介紹使用RT-Thread Studio 和瑞薩 CPK-RA2L1評估板,使用攀藤PMS1003 作為pm2.5 ,pm10等粉塵顆粒信息采集傳感器
    的頭像 發表于 10-11 11:40 ?399次閱讀
    <b class='flag-5'>CPK-RA2L1</b>評估板PMS1003
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>