0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

RTT RA2L1 HS3003采集温湿度之一

冬至子 来源:Lu_盼盼 作者:Lu_盼盼 2023-10-12 11:02 次阅读

新建项目

1、文件->新建->RT-Thread项目

1.jpg

2、选择基于开发板->CPK-RA2L1 填入项目名称,点击完成,就创建好了工程。

1.jpg

3、给RA Smart Configurator设置FSP3.5.0的路径:

1.jpg

4、打开RT-Thread settings添加hs3003软包:

1.jpg

5、打开I2c,配置P407、P408为I2C的SDA、SCL:

1.jpg

6、修改sensor_renesas_hs300x.c为如下内容:

/*

  • Copyright (c) 2006-2018, RT-Thread Development Team
  • SPDX-License-Identifier: Apache-2.0
  • Change Logs:
  • Date Author Notes
  • 2020-11-03 flybreak the first version
    /
    #include
    #include
    #include "sensor_renesas_hs300x.h"
    #define DBG_TAG "sensor.renesas.hs300x"
    #define DBG_LVL DBG_INFO
    #include
    #define SENSOR_TEMP_RANGE_MAX (80)
    #define SENSOR_TEMP_RANGE_MIN (-10)
    #define SENSOR_HUMI_RANGE_MAX (100)
    #define SENSOR_HUMI_RANGE_MIN (0)
    /
    Definitions of Mask Data for A/D data /
    #define RM_HS300X_MASK_HUMIDITY_UPPER_0X3F (0x3F)
    #define RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC (0xFC)
    #define RM_HS300X_MASK_STATUS_0XC0 (0xC0)
    /
    Definitions for Status Bits of A/D Data /
    #define RM_HS300X_DATA_STATUS_VALID (0x00) // Status-bit: Valid data
    /
    Definitions for Calculation */
    #define RM_HS300X_CALC_STATIC_VALUE (16383.0F)
    #define RM_HS300X_CALC_HUMD_VALUE_100 (100.0F)
    #define RM_HS300X_CALC_TEMP_C_VALUE_165 (165.0F)
    #define RM_HS300X_CALC_TEMP_C_VALUE_40 (40.0F)
    #define RM_HS300X_CALC_DECIMAL_VALUE_100 (100.0F)
    struct hs300x_device
    {
    struct rt_i2c_bus_device *i2c;
    uint8_t addr;
    };
    struct hs300x_device temp_humi_dev;
    static rt_err_t _hs300x_init(struct rt_sensor_intf intf)
    {
    temp_humi_dev.i2c = rt_i2c_bus_device_find(intf->dev_name);
    temp_humi_dev.addr = (uint8_t)intf->user_data;
    if (temp_humi_dev.i2c == RT_NULL)
    {
    return -RT_ERROR;
    }
    return RT_EOK;
    }
    int hs300x_read_data(struct hs300x_device
    dev, float *humi_f, float *temp_f)
    {
    uint8_t r_buf[4] = {0};
    uint16_t humi, temp;
    float tmp_f = 0.0;
    struct rt_i2c_msg msgs;
    msgs.addr = dev->addr;
    msgs.flags = RT_I2C_WR;
    msgs.buf = 0;
    msgs.len = 0;
    if (rt_i2c_transfer(dev->i2c, &msgs, 1) != 1)
    {
    LOG_E("mr error!n");
    return -1;
    }
    msgs.addr = dev->addr;
    msgs.flags = RT_I2C_RD;
    msgs.buf = r_buf;
    msgs.len = 4;
    __retry:
    if (rt_i2c_transfer(dev->i2c, &msgs, 1) == 1)
    {
    LOG_D("%x,%x,%x,%xn", r_buf[0], r_buf[1], r_buf[2], r_buf[3]);
    LOG_D("state:%xn", r_buf[0] & RM_HS300X_MASK_STATUS_0XC0);
    if ((r_buf[0] & RM_HS300X_MASK_STATUS_0XC0) != RM_HS300X_DATA_STATUS_VALID)
    {
    rt_thread_mdelay(100);
    goto __retry;
    }
    humi = (r_buf[0] & RM_HS300X_MASK_HUMIDITY_UPPER_0X3F) << 8 | r_buf[1];
    temp = (r_buf[2] << 8 | (r_buf[3] & RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC)) >> 2;
    tmp_f = (float)humi;
    tmp_f = (tmp_f * RM_HS300X_CALC_HUMD_VALUE_100) / RM_HS300X_CALC_STATIC_VALUE;
    if (humi_f)
    {
    humi_f = tmp_f;
    }
    tmp_f = (float)temp;
    tmp_f = ((tmp_f * RM_HS300X_CALC_TEMP_C_VALUE_165) / RM_HS300X_CALC_STATIC_VALUE) - RM_HS300X_CALC_TEMP_C_VALUE_40;
    if (temp_f)
    {
    temp_f = tmp_f;
    }
    return 0;
    }
    else {
    LOG_E("read error!n");
    return -1;
    }
    }
    float hs300x_read_temperature(struct hs300x_device
    dev)
    {
    float temp;
    if (hs300x_read_data(dev, NULL, &temp) == 0)
    {
    return temp;
    }
    return 0;
    }
    float hs300x_read_humidity(struct hs300x_device
    dev)
    {
    float humi;
    if (hs300x_read_data(dev, &humi, NULL) == 0)
    {
    return humi;
    }
    return 0;
    }
    static rt_size_t _hs300x_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data)
    {
    float temperature_x10, humidity_x10;
    if (sensor->info.type == RT_SENSOR_CLASS_TEMP)
    {
    temperature_x10 = 10 * hs300x_read_temperature(&temp_humi_dev);
    data->data.temp = (rt_int32_t)temperature_x10;
    data->timestamp = rt_sensor_get_ts();
    }
    else if (sensor->info.type == RT_SENSOR_CLASS_HUMI)
    {
    humidity_x10 = 10 * hs300x_read_humidity(&temp_humi_dev);
    data->data.humi = (rt_int32_t)humidity_x10;
    data->timestamp = rt_sensor_get_ts();
    }
    return 1;
    }
    static rt_size_t hs300x_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
    {
    RT_ASSERT(buf);
    if (sensor->config.mode == RT_SENSOR_MODE_POLLING)
    {
    return _hs300x_polling_get_data(sensor, buf);
    }
    else
    return 0;
    }
    static rt_err_t hs300x_control(struct rt_sensor_device *sensor, int cmd, void *args)
    {
    rt_err_t result = RT_EOK;
    return result;
    }
    static struct rt_sensor_ops sensor_ops =
    {
    hs300x_fetch_data,
    hs300x_control
    };
    int rt_hw_hs300x_init(const char *name, struct rt_sensor_config cfg)
    {
    rt_int8_t result;
    rt_sensor_t sensor_temp = RT_NULL, sensor_humi = RT_NULL;
    /
    temperature sensor register /
    sensor_temp = rt_calloc(1, sizeof(struct rt_sensor_device));
    if (sensor_temp == RT_NULL)
    return -1;
    sensor_temp->info.type = RT_SENSOR_CLASS_TEMP;
    sensor_temp->info.vendor = RT_SENSOR_VENDOR_UNKNOWN;
    sensor_temp->info.model = "hs300x";
    sensor_temp->info.unit = RT_SENSOR_UNIT_DCELSIUS;
    sensor_temp->info.intf_type = RT_SENSOR_INTF_I2C;
    sensor_temp->info.range_max = SENSOR_TEMP_RANGE_MAX;
    sensor_temp->info.range_min = SENSOR_TEMP_RANGE_MIN;
    sensor_temp->info.period_min = 5;
    rt_memcpy(&sensor_temp->config, cfg, sizeof(struct rt_sensor_config));
    sensor_temp->ops = &sensor_ops;
    result = rt_hw_sensor_register(sensor_temp, name, RT_DEVICE_FLAG_RDONLY, RT_NULL);
    if (result != RT_EOK)
    {
    LOG_E("device register err code: %d", result);
    goto __exit;
    }
    /
    humidity sensor register */
    sensor_humi = rt_calloc(1, sizeof(struct rt_sensor_device));
    if (sensor_humi == RT_NULL)
    return -1;
    sensor_humi->info.type = RT_SENSOR_CLASS_HUMI;
    sensor_humi->info.vendor = RT_SENSOR_VENDOR_UNKNOWN;
    sensor_humi->info.model = "hs300x";
    sensor_humi->info.unit = RT_SENSOR_UNIT_PERMILLAGE;
    sensor_humi->info.intf_type = RT_SENSOR_INTF_I2C;
    sensor_humi->info.range_max = SENSOR_HUMI_RANGE_MAX;
    sensor_humi->info.range_min = SENSOR_HUMI_RANGE_MIN;
    sensor_humi->info.period_min = 5;
    rt_memcpy(&sensor_humi->config, cfg, sizeof(struct rt_sensor_config));
    sensor_humi->ops = &sensor_ops;
    result = rt_hw_sensor_register(sensor_humi, name, RT_DEVICE_FLAG_RDONLY, RT_NULL);
    if (result != RT_EOK)
    {
    LOG_E("device register err code: %d", result);
    goto __exit;
    }
    _hs300x_init(&cfg->intf);
    return RT_EOK;
    __exit:
    if (sensor_temp)
    rt_free(sensor_temp);
    if (sensor_humi)
    rt_free(sensor_humi);
    return -RT_ERROR;
    }
    #define HS300X_I2C_BUS "i2c1"
    int rt_hw_hs300x_port(void)
    {
    struct rt_sensor_config cfg;
    cfg.intf.dev_name = HS300X_I2C_BUS;
    cfg.intf.user_data = (void *)HS300X_I2C_ADDR;
    rt_hw_hs300x_init("hs300x", &cfg);
    return RT_EOK;
    }
    INIT_APP_EXPORT(rt_hw_hs300x_port);
    6、修改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
#include "sensor_renesas_hs300x.h"
#define LED1_PIN "P502" /
Onboard LED pins /
#define USER_INPUT "P004"
extern struct hs300x_device temp_humi_dev;
void hal_entry(void)
{
float temp_f,humi_f;
rt_kprintf("nHello RT-Thread!n");
rt_uint32_t led1_pin = rt_pin_get(LED1_PIN);
while (1)
{
rt_pin_write(led1_pin, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(led1_pin, PIN_LOW);
rt_thread_mdelay(500);
hs300x_read_data(&temp_humi_dev, &humi_f, &temp_f);
rt_kprintf("HUMI: %d.%2dn", (int)humi_f, (int)(humi_f
100)%100);
rt_kprintf("HUMI: %d.%2dn", (int)temp_f, (int)(temp_f*100)%100);
}
}
void irq_callback_test(void args)
{
rt_kprintf("n IRQ03 triggered n");
}
void icu_sample(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(icu_sample, icu sample);

实现效果为:

1.jpg

【小结】这节主要是新建基础工程,并且添加hs3003的软件包。实现温度、湿度的采集。下一集创建DA16200 AT_MQTT。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • SDA
    SDA
    +关注

    关注

    0

    文章

    124

    浏览量

    28127
  • SCL
    SCL
    +关注

    关注

    1

    文章

    239

    浏览量

    17057
  • FSP
    FSP
    +关注

    关注

    0

    文章

    34

    浏览量

    7131
  • RTThread
    +关注

    关注

    8

    文章

    132

    浏览量

    40860
  • HAL库
    +关注

    关注

    1

    文章

    121

    浏览量

    6217
收藏 人收藏

    评论

    相关推荐

    英飞凌PSC62低功耗温湿度数据采集

    驱动hs3003温湿度传感器,采集完数据后进入深度睡眠模式,以达到节能的目的。
    的头像 发表于 05-30 09:05 41.5w次阅读
    英飞凌PSC62低功耗<b class='flag-5'>温湿度数据采集</b>

    使用RT-Thread和CPK-RA2L1采集DHT11温湿度

    本篇文章主要介绍使用RT-Thread Studio 和瑞萨 CPK-RA2L1评估板,使用大佬的轮子采集温湿度
    的头像 发表于 10-11 11:34 1607次阅读
    使用RT-Thread和CPK-<b class='flag-5'>RA2L1</b><b class='flag-5'>采集</b>DHT11<b class='flag-5'>温湿度</b>

    RA4M2设计挑战赛】HS3003读取数据

    HS3003温湿度计,I2C接口,这里给大家分享下基本的驱动:时钟配置这里要说明下,原来的RA4M
    发表于 01-17 07:57

    RA4M2设计挑战赛】RTT sensor模块驱动hs3003

    上层提供统的操作接口,提高上层代码的可重用性经查看sensor支持包,rtt支持该传感器的:在【RA4M2设计挑战赛】RTT 移植 这篇帖子基础上加入
    发表于 02-06 14:37

    RA4M2设计挑战赛】2. 硬件IIC读取HS3003温湿度数据

    ),基于FreeRTOS作为OS,实现硬件IIC采集HS3003温湿度数据。HS300X原理图通过对比了RA4M2开发板的PMOD接口以及
    发表于 02-11 20:21

    RA4M2设计挑战赛】读取HS3003数据,并在OLED上显示,串口打印

    概述HS3003种数字式温湿度传感器,可以测量环境中的温度和湿度。读取HS3003的数据需要连接传感器到
    发表于 02-22 15:11

    RA4M2设计挑战赛】低功耗远距离无线温度监控项目

    次数据,长达1年左右的工作时间。项目用到的硬件1、瑞萨RA4M2开发板 12、瑞萨
    发表于 02-23 15:03

    RA4M2设计挑战赛】+温湿度采集通过WiFi模块上传PC端

    通过HS3003温湿度模块采集温度,采集温度时3个LED灯闪亮。通过DA16200WiFi模块上传到PC网页端,PC端使用python Flask建立本地服务器,展示温度数据。通过U盘
    发表于 02-24 23:02

    RA4M2设计挑战赛】2、H3003温湿度传感器使用

    RA4M2设计挑战赛】2、H3003温湿度传感器使用H3003简介HS3003
    发表于 02-26 22:29

    基于RA4M2HS3003温湿度测量

    、前言HS3003种数字式温湿度传感器,可以测量环境中的温度和湿度。读取HS3003的数据
    发表于 03-05 23:38

    【精品合集】瑞萨RA4M2物联网网关设计挑战赛作品合集

    FreeRTOS【RA4M2设计挑战赛】2. 硬件IIC读取HS3003温湿度数据【RA4M2设计挑战赛】3. 硬件IIC读取ISL290
    发表于 03-07 14:33

    RTT RA2L1 HS3003采集温湿度项目设计

    , icu sample);  实现效果为:  【小结】  这节主要是新建基础工程,并且添加hs3003的软件包。实现温度、湿度采集。原作者:Lu_盼盼
    发表于 03-07 17:43

    【CW32饭盒派开发板试用体验】6.硬件I2C之连接HS300X读取温湿度数据

    器 【CW32饭盒派开发板试用体验】4. ADC采样摇杆模块X轴和Y轴输出 【CW32饭盒派开发板试用体验】5. 极简方式移植FreeRTOS成功 1 HS3003介绍 HS3003是瑞萨的
    发表于 06-07 21:45

    国民技术N32WB031驱动HS3003采集温湿度

    国民技术n32wb031驱动hs3003
    的头像 发表于 05-02 17:28 25.8w次阅读
    国民技术N32WB031驱动<b class='flag-5'>HS3003</b><b class='flag-5'>采集</b><b class='flag-5'>温湿度</b>

    RA4M2开发(4)----读取HS3003数据,并在OLED上显示,串口打印

    HS3003种数字式温湿度传感器,可以测量环境中的温度和湿度。读取HS3003的数据需要连接传感器到
    的头像 发表于 07-27 10:14 1388次阅读
    <b class='flag-5'>RA4M2</b>开发(4)----读取<b class='flag-5'>HS3003</b>数据,并在OLED上显示,串口打印