Using RT-Thread Studio DIY mini desktop clock (B) | acquired temperature and humidity sensor data (I2C device driver package + SHT3x)

1. Project Schedule

Mini Desktop clock project to demonstrate how to use the RT-Thread Stduio development project, the entire project structure is as follows:

In a previous blog post brief introduction to the RT-Thread Studio one-stop tool based on this chip STM32L431RCT6 create a project, and modify the clock to an external clock.

Using RT-Thread Studio DIY mini desktop clock (a) | STM32 chip is created based on the project

Took over we start adding I2C devices, add SHT3x package, get SHT3x temperature and humidity sensor data.

2. Add I2C devices

2.1 Open the I2C device driver framework

Double-click the left side of the RT-Thread Settingfile, you can open the RT-Thread graphical configuration tool, software simulation I2C this one is gray, that there is no open, click once to open the I2C driver software framework, the icon changes color means open:

the right click this option to open more configuration, such as viewing dependent on the driving device to view the detailed configuration of the drive device to view the API documentation for that device driver to view the online documentation and other operations:

press the Ctrl+Ssave configuration to take effect, the software will automatically add I2C device driver framework to the project:

2.2. Adding I2C source software

After opening the drive frame I2C software, but also the underlying software add I2C driver implemented, depending on the specific source chip I2C driver software, the present embodiment download the software to add the STM32 I2C driver: Gitee Download .

git clone https://gitee.com/tyustli/tyustli.git

After downloading the source code only two files:

the two files added to the project driversfolder:

Back to the RT-Thread Studio IDE, right-click on the project name and select Refresh, to see added to the files in the directory :

2.3. Registration I2C devices

软件 I2C 添加到工程中之后就可以调用软件 I2C 注册函数 rt_hw_i2c_init 来注册软件 I2C 设备了,该函数的原型如下:

int rt_hw_i2c_init(char *name, rt_uint8_t scl, rt_uint8_t sda)
  • name:设备名称
  • scl:软件模拟I2C的SCL引脚
  • sda:软件模拟I2C的SDA引脚

在小熊派IoT开发板上,温湿度传感器SHT30连接在PB6(SCL)和PB7(SDA) ,所以在main.c文件中先添加头文件:

#include <drv_soft_i2c.h>

然后在文件最后添加如下注册软件 I2C到系统中的代码:

int register_i2c(void)
{
    rt_hw_i2c_init("i2c1", GET_PIN(B,6), GET_PIN(B,7));

    return RT_EOK;
}
//注册到系统中,自动初始化设备
INIT_BOARD_EXPORT(register_i2c);

添加完成之后点击编译,下载到开发板中运行,即可在串口终端中看到日志信息(绿色),提示I2C总线设备已注册成功:

因为main线程中循环打印对使用控制台有影响,所以将打印函数注释:

重新编译下载,在串口终端中输入命令list_device查看系统中注册的设备吗,再次确认I2C总线设备注册成功:

3. 添加SHT3x软件包

3.1. 搜索添加软件包

RT-Thread Studio有在线软件包中心,里面有非常丰富的软件包供用户使用,点击立即添加进入:

搜索SHT,使用的传感器型号为SHT30,所以添加SHT3x软件包:

添加之后,在项目设置中即可看到该软件包,右击可以查看软件包在线文档:

软件会自动打开该软件包的在线说明文档,在使用软件包之前,该文档必须要看:

接下来按Ctrl+S保存项目,软件会自动添加软件包代码到工程中,其中README.md是软件包详细使用文档,使用前必须要看:

接下来编译、下载项目到开发板中,查看串口终端输出。

3.2. 使用命令测试软件包

大部分软件包都提供测试命令,SHT3x软件包也是一样,提供了如下命令供测试:

- sht3x probe <i2c_dev_name> <pu/pd>  --挂载SHT3x设备,需要指定i2c设备名称和上下拉方式,默认下拉
- sht3x read --阅读SHT3x温湿度
- sht3x status --读取查看状态寄存器值
- sht3x reset --软件复位SHT3x
- sht3x heater <on/off> --/关heater

首先挂载SHT30温湿度传感器到之前注册的I2C总线设备上:

挂载之后可以进行查看寄存器,读取温湿度,复位,开/关heater操作,比如读取一次温度和湿度:

4. 获取温湿度传感器数据

在使用软件包提供的命令测试设备成功之后,创建一个线程,使用SHT3x软件包提供的API获取SHT3x数据。

在Applicaition分组之下,新建一个文件sht30_ccollect.c用于存放SHT3x获取数据的相关代码,编辑以下内容:

/*
 * Copyright (c) 2006-2019, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-02-20     Mculover666  the first version
 */

#include <rtthread.h>
#include <board.h>
#include <sht3x.h>

#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5

static rt_thread_t tid1 = RT_NULL;

/* 入口函数 */
static void sht30_collect_thread_entry(void *parameter)
{
    sht3x_device_t  sht3x_device;

    sht3x_device = sht3x_init("i2c1", 0x44);

    while (1)
    {
        if(RT_EOK == sht3x_read_singleshot(sht3x_device))
        {
            rt_kprintf("sht30 humidity   : %d.%d  ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
            rt_kprintf("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
        }
        else
        {
            rt_kprintf("read sht3x fail.\r\n");
            break;
        }
        rt_thread_mdelay(2000);
    }
}

/* 创建线程 */
int sht30_collect(void)
{
    /* 创建线程*/
    tid1 = rt_thread_create("sht30_collect_thread",
            sht30_collect_thread_entry, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);

    /* 如果获得线程控制块,启动这个线程 */
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);

    return 0;
}

在main.c中测试该程序:

编译下载程序,在串口终端查看结果:

接收更多精彩文章及资源推送,欢迎订阅我的微信公众号:『mculover666』。

发布了216 篇原创文章 · 获赞 575 · 访问量 25万+

Guess you like

Origin blog.csdn.net/Mculover666/article/details/104153715