[RISC-V MCU 应用开发] 国产risc-v微控制器读取SHT41高精度温湿度传感器

[复制链接]
 楼主| dirtwillfly 发表于 2025-6-22 15:20 | 显示全部楼层 |阅读模式
本帖最后由 dirtwillfly 于 2025-6-22 15:22 编辑

1. 使用的软硬件
1.1 开发环境
1)RT-Thread Studio

1.2 硬件
1)SHT41温湿度传感器模块  1块
2)HPM5361EVK        1块
3)杜邦线                 若干


2. SHT41温湿度传感器

2.1 SHT41传感器参数

湿度:
典型相对湿度精度   1.8 %RH相对湿度测量范围    0 ~ 100 %RH
响应时间 (τ63%)     4 s

温度:
典型温度精度        0.2 °C
温度测量范围       -40 ~ 125 °C
响应时间 (τ63%)   2 s

通用:
电源电压                1.08 ~ 3.6 V
平均电源电流          0.4 uA(@1Hz)
接口                       I²C
装尺寸 (长x宽x高)   1.5 x 1.5 x 0.5 mm³


2.2 典型的应用电路

37616857adacec8ef.png

2.3 I2C通讯

支持的I2C模式包括标准模式、快速模式和快速增强模式。数据以16位字的倍数传输,并带有8位校验和(循环冗余校验,即CRC)。
所有的传输都必须以起始条件(S)开始,并以停止条件(P)结束。要完成一次读取传输,应发送非应答信号(NACK)和停止条件(P)。
通过发送特定从设备的7位I2C地址,后跟一个表示通信方向的第八位来实现对该设备的选择:“0”表示向该从设备发送数据,即“写入”,而“1”则表示一次“读取”请求。


135716857aea23e56b.png

I2C总线以8位数据包的方式进行操作。从传感器到主机的数据信息中,每隔一个8位数据包就会包含一个校验和。  
湿度和温度数据的传输始终遵循以下方式:第一个数值是温度信号(2个8位数据包 + 8位CRC校验),第二个数值是湿度信号(2个8位数据包 + 8位CRC校验)。

 楼主| dirtwillfly 发表于 2025-6-22 15:21 | 显示全部楼层
本帖最后由 dirtwillfly 于 2025-6-22 15:29 编辑

3. hpm5361 硬件电路

使用I2C1读取SHT41的数据,该接口的SCL和SDA信号,开发板上设计了10k的上拉电阻。
857796857afc19f5f3.png
该接口对应的GPIO为PB07和PB06。
167276857b02b56dc4.png

363966857af89bca31.png
 楼主| dirtwillfly 发表于 2025-6-22 15:21 | 显示全部楼层
本帖最后由 dirtwillfly 于 2025-6-29 09:59 编辑

4. 软件开发
4.1配置资源

在RT-Thread Setting 界面的硬件标签页,使能I2C,以及I2C1 BUS。 微信截图_20250629094335.png

在组件标签页,打开"使用I2C设备驱动程序"的使能开关。

444096857b11a7ab9f.png

在软件包标签页,打开"sht4x:digital humidity and temperature sensor sht4x driver library"的使能开关。

然后点击”保存“,等待开发环境自动配置完毕。

4.2 配置rtconfig.h

在rtconfig.h文件,添加宏定义:
  1. #define BSP_USING_I2C
  2. #define BSP_USING_I2C1
4.3 代码开发
新建app_sht4x.c文件和头文件app_sht4x.h。
在app_sht4x.c文件添加包含的头文件:
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <hpm_soc.h>
  4. #include "app_sht4x.h"
  5. #include "sht4x.h"
添加宏定义,其中i2c1为要使用的I2C的设备名:
  1. #define SHT4X_DEBUG
  2. #define SHT4X_DEV_NAME       "i2c1"
添加全局变量:
  1. sht4x_device_t sht4x_dev = RT_NULL;
  2. struct sht4x_struct sht4x_str;

  3. static struct rt_thread sht4x_thread;//定义线程控制块
  4. static char sht4x_thread_stack[1024];//定义线程栈
其中结构体struct sht4x_struct需要在app_sht4x.h定义:
  1. struct sht4x_struct{
  2.         rt_int32_t temperature;
  3.         rt_uint32_t humidity;
  4. };
编写sht4x的线程逻辑,整体线程循环为:读取温度和湿度,然后通过串口打印输出,然后延时1000ms。
  1. static void sht4x_thread_entry(void *parameter)
  2. {
  3.         while(1)
  4.         {
  5.                 sht4x_str.temperature = sht4x_read_temperature(sht4x_dev);
  6.                 sht4x_str.humidity = sht4x_read_humidity(sht4x_dev);
  7. #ifdef SHT4X_DEBUG
  8.                 rt_kprintf("humidity   : %d.%d %%\n", (int)sht4x_str.humidity, (int)(sht4x_str.humidity * 10) % 10);
  9.                 rt_kprintf("temperature: %d.%d C\n", (int)sht4x_str.temperature, (int)(sht4x_str.temperature * 10) % 10);
  10. #endif
  11.                 rt_thread_mdelay(1000);
  12.         }
  13. }
编写初始化函数,主要功能为初始化sht4x_dev设备,读取sht4x的芯片串号,初始化sht4x线程,最后启动线程。
  1. int sht4x_sensor_init(void)
  2. {
  3.         rt_err_t ret = 0;

  4.         sht4x_dev = sht4x_init(SHT4X_DEV_NAME);
  5.         if(sht4x_dev == RT_NULL)
  6.         {
  7.                 rt_kprintf("sht4x_init fail\n\r");
  8.                 return RT_ERROR;
  9.         }
  10.         rt_kprintf("sht4x id:0x%x\n",sht4x_read_serial(sht4x_dev));

  11.         rt_kprintf("sht4x_thread init\n\r");
  12.         ret = rt_thread_init(&sht4x_thread, "sht4x_thread", sht4x_thread_entry, RT_NULL, sht4x_thread_stack, sizeof(sht4x_thread_stack), 19, 20);
  13.         if(ret != RT_EOK)
  14.                 rt_kprintf("sht4x_thread init fail\n\r");
  15.         else
  16.                 ret = rt_thread_startup(&sht4x_thread);

  17.         return ret;
  18. }
然后,再把初始化函数添加到app_sht4x.h文件。
最后的app_sht4x.h文件内容为:
  1. #ifndef APPLICATIONS_APP_SHT4X_H_
  2. #define APPLICATIONS_APP_SHT4X_H_

  3. struct sht4x_struct{
  4.         rt_int32_t temperature;
  5.         rt_uint32_t humidity;
  6. };

  7. int sht4x_sensor_init(void);

  8. #endif /* APPLICATIONS_APP_SHT4X_H_ */




 楼主| dirtwillfly 发表于 2025-6-22 15:21 | 显示全部楼层
本帖最后由 dirtwillfly 于 2025-6-29 10:00 编辑

5. 验证效果在main.c文件,添加包含文件:
  1. #include "app_sht4x.h"
在main()函数添加sht4x的初始化函数:
  1. sht4x_sensor_init();
编译下载,使用串口调试工具观察,会有如下信息:
微信截图_20250629095704.png
幻境之眼 发表于 2025-7-8 11:23 | 显示全部楼层
看着确实不错,数据都能读出来,还上了RT-thred。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎进入TI MCU论坛      21ic TI技术交流1群:61549143(已满),  21ic TI技术交流2群:311421422 我的博客:http://blog.timcu.com/

1194

主题

35082

帖子

1122

粉丝
快速回复 在线客服 返回列表 返回顶部