<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天內不再提示

【RA2L1開發實踐】- 溫濕度檢測平臺

冬至子 ? 來源:xiaodaidaii ? 作者:xiaodaidaii ? 2023-10-10 15:06 ? 次閱讀

主要驅動代碼

dht11.c

/*

  • Copyright (c) 2006-2021, RT-Thread Development Team
  • SPDX-License-Identifier: Apache-2.0
  • Change Logs:
  • Date Author Notes
  • 2023-03-15 XiaoDai the first version
    /
    #ifndef SRC_DHT11_C_
    #define SRC_DHT11_C_
    #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);
    }
    #endif /
    SRC_DHT11_C_ /
    /
  • Copyright (c) 2006-2021, RT-Thread Development Team
  • SPDX-License-Identifier: Apache-2.0
  • Change Logs:
  • Date Author Notes
  • 2023-03-15 XiaoDai the first version
    */
    #ifndef SRC_DHT11_H_
    #define SRC_DHT11_H_
    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_ */
0.96oled代碼
void lcd_thread_handler(void *parameter)
{
LCD_Init();
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
lcd_show();
rt_thread_mdelay(1000);
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
while(1)
{
lcd_show_pic1();
rt_thread_mdelay(500);
}
}
if(key_num ==1)
{
// u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 1, 40, "LED2 is on");
u8g2_SendBuffer(&u8g2);
}
else {
// u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 1, 40, "LED2 is off");
u8g2_SendBuffer(&u8g2);
}
hal_entry.c

/*

Copyright (c) 2006-2021, RT-Thread Development Team

SPDX-License-Identifier: Apache-2.0

Change Logs:
Date Author Notes
2021-10-10 Sherman first version
/
#include
#include "hal_data.h"
#include
#define LED1_PIN "P502" /
Onboard LED1 pins /
#define LED2_PIN "P501" /
Onboard LED2 pins */
#define USER_INPUT "P004"
rt_uint32_t pin ;
int key_num ;
void hal_entry(void)
{
rt_kprintf("nHello RT-Thread!n");
while (1)
{
if( pin == 1)
{
rt_kprintf("nUSER_INPUT push !n");
rt_pin_write(LED2,PIN_HIGH);
}
rt_thread_mdelay(500);
}
}
void irq_callback_test(void args)
{
static int out ,out2 ;
rt_kprintf("n IRQ03 triggered n");
rt_uint32_t led1_pin = rt_pin_get(LED1_PIN);
rt_uint32_t led2_pin = rt_pin_get(LED2_PIN);
out = rt_pin_read(led1_pin)?PIN_LOW:PIN_HIGH;
out2 = rt_pin_read(led2_pin)?PIN_LOW:PIN_HIGH;
rt_pin_write(led1_pin,out);
rt_pin_write(led2_pin,out2);
key_num++;
if( pin == 0 && key_num ==2)
{
rt_kprintf("n LED2 is on !n");
rt_pin_write(LED2,PIN_HIGH);
}
else {
rt_kprintf("n LED2 is off !n");
rt_pin_write(LED2,PIN_LOW);
}
if(key_num ==2 )
key_num =0;
}
void push_btn(void)
{
/
init */
rt_uint32_t pin = rt_pin_get(USER_INPUT);
rt_kprintf("n pin number : 0x%04X n", pin);
rt_err_t err = rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING, irq_callback_test, RT_NULL);
if (RT_EOK != err)
{
rt_kprintf("n attach irq failed. n");
}
err = rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
if (RT_EOK != err)
{
rt_kprintf("n enable irq failed. n");
}
}
MSH_CMD_EXPORT(push_btn, push_btn);

偶然之中在源代碼中發現官方已經幫我們定義好了有些引腳,所以就沒必要重復定義了

1.jpg

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

    關注

    0

    文章

    118

    瀏覽量

    15158
  • SRC
    SRC
    +關注

    關注

    0

    文章

    60

    瀏覽量

    17826
  • DHT11
    +關注

    關注

    19

    文章

    264

    瀏覽量

    57264
  • CMD命令
    +關注

    關注

    0

    文章

    28

    瀏覽量

    8198
  • RT-Thread
    +關注

    關注

    31

    文章

    1184

    瀏覽量

    39003
收藏 人收藏

    評論

    相關推薦

    基于DragonBoard 410c的溫濕度檢測(一)

    本博客給大家介紹如何使用DragonBoard 410c 開發板實現對環境溫濕度檢測,要實現這一功能,還需要一個溫濕度傳感器,本次設計中,我選用的是DHT11
    發表于 09-26 18:11

    無人升空平臺溫濕度遠程監控系統設計

      摘要:為了解決無法對無人升空平臺設備艙溫濕度進行實時監控的問題,采用STC89C52單片機為控制核心,以Lab-VIEW為開發平臺,提出了無人升空
    發表于 11-13 16:12

    Renesas RA2L1開發板之UART模塊測評

    1、Renesas RA2L1開發板之UART  評測任務  首先非常感謝RT-Thread和Renesas給予測評CPK-RA2L1開發
    發表于 10-18 10:38

    Renesas RA2L1開發板之I2C測評

    1、Renesas RA2L1開發板之I2C  開發板介紹  CPK-RA2L1評估板是一款專門
    發表于 10-24 16:29

    Renesas RA2L1開發板之CAN介紹

    1、Renesas RA2L1開發板之CAN介紹  功能模塊的硬件介紹  CPK-RA2L1評估板是一款專門針對中國本地的開發板,主MCU是
    發表于 11-01 11:46

    Renesas RA2L1開發板之PWM方波配置相關資料推薦

    1、Renesas RA2L1開發板之PWM方波配置  工程的配置  時鐘的配置  本次直接基于官方的例程進行開發,使用瑞薩的 RA Con
    發表于 11-02 15:31

    Renesas RA2L1開發板之I2C接口評測

    1、Renesas RA2L1 開發板之 I2C  開發板介紹  CPK-RA2L1評估板是一款
    發表于 11-04 14:26

    基于RA2L1開發板的初識點燈

    ?! ?.開發板框圖  4.學習記錄(點燈+串口打?。 ?.1 硬件連線  4.2 原理圖  4.3 環境安裝  這里是根據《瑞薩RA2L1開發實踐指南》-零、
    發表于 04-03 16:55

    RA2L1開發實踐溫濕度檢測平臺

    */  #define DHT1x_BEGIN_TIME 20 /* ms */  #define DHT2x_BEGIN_TIME 1 /* ms */  #define DHTxx_PULL_TIME
    發表于 04-03 17:24

    無線溫濕度檢測裝置的設計

    設計了一種基于溫濕度數字式傳感器的無線溫濕度檢測裝置,以單片機為控制核心,采用數字式溫濕度傳感器來檢測目標的溫度和
    發表于 06-13 17:09 ?128次下載
    無線<b class='flag-5'>溫濕度</b><b class='flag-5'>檢測</b>裝置的設計

    溫濕度監測

    進行溫濕度的仿真,采用PROTEUS進行糧倉溫濕度檢測與控制。
    發表于 05-11 14:33 ?26次下載

    溫濕度自記儀是什么,該如何選購溫濕度自記儀

    今天要給大家說的是溫濕度自記儀,在傳統的種植環境中對溫濕度進行檢測時,多半是采用長度法或者干濕法。時至今日,這些檢測方法已經不滿足現在農業生產,而
    的頭像 發表于 10-28 10:43 ?2351次閱讀

    溫濕度遠程監控系統概述

    中易云溫濕度監控系統專為溫濕度監控設計,使用各種物聯網溫濕度監控硬件及云平臺,在檢測環境溫濕度
    的頭像 發表于 12-16 15:36 ?2544次閱讀

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

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

    瑞薩e2studio----RA2L1通過傳感器檢測溫濕度

    ?本篇文章主要介紹如何使用芯片型號R7FA2L1AB2DFL的開發板外接溫濕度傳感器進行溫濕度檢測,并通過串口顯示溫濕度。
    的頭像 發表于 01-04 14:38 ?1147次閱讀
    瑞薩e2studio----<b class='flag-5'>RA</b>2L1通過傳感器<b class='flag-5'>檢測溫濕度</b>
    亚洲欧美日韩精品久久_久久精品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>