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

    浏览量

    15119
  • SRC
    SRC
    +关注

    关注

    0

    文章

    60

    浏览量

    17801
  • DHT11
    +关注

    关注

    19

    文章

    264

    浏览量

    57179
  • CMD命令
    +关注

    关注

    0

    文章

    28

    浏览量

    8173
  • RT-Thread
    +关注

    关注

    31

    文章

    1150

    浏览量

    38909
收藏 人收藏

    评论

    相关推荐

    基于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开发板的初识点灯

    。  3.开发板框图  4.学习记录(点灯+串口打印)  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 25次下载

    温湿度自记仪是什么,该如何选购温湿度自记仪

    今天要给大家说的是温湿度自记仪,在传统的种植环境中对温湿度进行检测时,多半是采用长度法或者干湿法。时至今日,这些检测方法已经不满足现在农业生产,而
    的头像 发表于 10-28 10:43 2301次阅读

    温湿度远程监控系统概述

    中易云温湿度监控系统专为温湿度监控设计,使用各种物联网温湿度监控硬件及云平台,在检测环境温湿度
    的头像 发表于 12-16 15:36 2387次阅读

    【Renesas RA6M4开发板之DHT11温湿度读取】

    本篇通过Renesas RA6M4开发板DHT11温湿度读取示例程序演示。
    的头像 发表于 01-18 17:18 1320次阅读
    【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 1069次阅读
    瑞萨e2studio----<b class='flag-5'>RA</b>2L1通过传感器<b class='flag-5'>检测温湿度</b>