Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread
Renesa Version Board开发RT-Thread 之I2C驱动应用(SHT20)
发布于 2024-07-28 13:07:41 浏览:180
订阅该版
[tocm] # 概述 本文主要介绍如何使用Renesa Version Board上的I2C接口在RT -Thread框架下功能的应用。笔者基于SHT-Sensor详细介绍了配置的方法。还介绍了RT -Thread框架下I2C驱动的接口函数。 # 1 硬件接口介绍 ## 1.1 Version Board上的I2C硬件接口 Renesa Version Board板卡扩展口提供一个标准的I2C接口,其命名如下: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/e9f61bf093056c1f2820a3aa6ce09def.png.webp) 在如下网址能看见该IO所对应的Pin引脚: ```json https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/hw-board/ra8d1-vision-board/ra8d1-vision-board ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/20bfe797ff2bc21d8273aa7e710dba79.png.webp) ## 1.2 SHT20 ### 1.2.1 SHT20简介 SHT20是由Sensirion公司推出的一款数字温湿度传感器。它采用先进的CMOSens®技术,具有高精度、低功耗和长期稳定性的特点。 SHT20的测量范围涵盖了-40℃至125℃的温度和0%至100%的湿度,精确度分别为±0.3℃和±3%RH。它使用了数字式输出接口,可以通过I2C总线进行数据通信,并支持温度和湿度的实时测量。 SHT20的封装形式小巧,尺寸为3x3x1.1mm,适合各种应用场景。它具有低功耗特性,工作电压范围为2.1V至3.6V,供电电流在典型条件下为1.2mA。此外,SHT20还具有快速响应时间和强大的抗干扰能力。 ### 1.2.2 SHT-20模块电路 1)SHT20硬件接口图 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/a988632b64134039f61657303346fa6f.png) 2) sensor模块电路 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/553b41989c97eba55e059b07a6225f90.png) SHT-20实物图: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/dfc11c8e91705116862c43f034a9f77d.png.webp) # 2 软件实现 ## 2.1 软件版本信息 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/774df2742336f20656b64a5aa8e386b4.png) ## 2.2 RT-Thread Studio创建项目 打开RT-Thread Studio,File->RT-Thread Project,打开之后看见如下页面,输入项目名称,创建项目。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/b1159084b0429f0cac194d86c767f17e.png) ## 2.3 FSP配置I2C接口 1)选择SCI3接口,配置为I2C模式,该接口对应的IO口为P408和P409 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/49ca88b8c48f7454cebe5800c8d973c9.png.webp) 2)在Stacks面板上配置驱动的相关信息 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/726bc359c304d1d40db64865a7b57f05.png.webp) 完成以上两个步骤之后,就可以重新生成项目代码。 3) RT-Thread Studio打开驱动接口,配置完成后,使用Ctrl+S保存文件。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/c3715eee45ed0150458dca7bbc1bed50.png.webp) ## 2.4 使能Sensor驱动 打开RT-Thread Setting,搜索sht2,找到该驱动后使能该驱动。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/56ebafbf326a7504845377cd801b683d.png.webp) 在项目目录中,packages中已经加载该驱动程序。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/5b8df529e7789c5233c8d116377f3062.png) # 3 RT-Thread驱动架构 ## 3.1 接口函数 一般来说,MCU的I2C设备作为主从设备进行通信。在RT-Thread中,I2C主机被虚拟化为I2C总线设备。I2C从站通过I2C设备接口与I2C总线通信。相关接口如下: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/cd4f0645fc03eef4203d9f1ae88a93b5.png) ### 3.1.1 查找设备函数 在使用I2C总线设备之前,需要根据I2C总线设备名称获取设备句柄,以便对I2C总线设备进行操作。设备功能如下: ```c rt_device_t rt_device_find(const char* name); ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/fc3f591764e7f37d7cf1c6383474a2e0.png) **举个例子:** 注册到系统的I2C设备名称为i2co2、i2c1等。使用示例如下: ```c #define AHT10_I2C_BUS_NAME "i2c1" /* Sensor connected I2C bus device name */ struct rt_i2c_bus_device *i2c_bus; /* I2C bus device handle */ /* Find the I2C bus device and get the I2C bus device handle */ i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name); ``` ### 3.1.2 数据传输函数 可以通过获取I2C总线设备句柄来使用' rt_i2c_transfer() '进行数据传输。函数原型如下: ```c rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num); ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/46d3f339d09cb94a14d96db904531db0.png) ** 一个应用实例:** Message结构体定义 ```c struct rt_i2c_msg { rt_uint16_t addr; /* Slave address */ rt_uint16_t flags; /* Reading, writing signs, etc. */ rt_uint16_t len; /* Read and write data bytes */ rt_uint8_t *buf; /* Read and write data buffer pointer */ } ``` 读数据函数实现 ```c /* Read sensor register data */ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf) { struct rt_i2c_msg msgs; msgs.addr = AHT10_ADDR; /* Slave address */ msgs.flags = RT_I2C_RD; /* Read flag */ msgs.buf = buf; /* Read and write data buffer pointer */ msgs.len = len; /* Read and write data bytes */ /* Call the I2C device interface to transfer data */ if (rt_i2c_transfer(bus, &msgs, 1) == 1) { return RT_EOK; } else { return -RT_ERROR; } } ``` ## 3.3 驱动Demo RT-Thread提供了一个Demo,其详细代码如下: ```c /* * Program listing: This is an I2C device usage routine * The routine exports the i2c_aht10_sample command to the control terminal * Command call format: i2c_aht10_sample i2c1 * Command explanation: The second parameter of the command is the name of the I2C bus device to be used. If it is empty, the default I2C bus device is used. * Program function: read the temperature and humidity data of the aht10 sensor and print. */ #include
#include
#define AHT10_I2C_BUS_NAME "i2c1" /* Sensor connected I2C bus device name */ #define AHT10_ADDR 0x38 /* Slave address */ #define AHT10_CALIBRATION_CMD 0xE1 /* Calibration command */ #define AHT10_NORMAL_CMD 0xA8 /* General command */ #define AHT10_GET_DATA 0xAC /* Get data command */ static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C bus device handle */ static rt_bool_t initialized = RT_FALSE; /* Sensor initialization status */ /* Write sensor register */ static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data) { rt_uint8_t buf[3]; struct rt_i2c_msg msgs; buf[0] = reg; //cmd buf[1] = data[0]; buf[2] = data[1]; msgs.addr = AHT10_ADDR; msgs.flags = RT_I2C_WR; msgs.buf = buf; msgs.len = 3; /* Call the I2C device interface to transfer data */ if (rt_i2c_transfer(bus, &msgs, 1) == 1) { return RT_EOK; } else { return -RT_ERROR; } } /* Read sensor register data */ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf) { struct rt_i2c_msg msgs; msgs.addr = AHT10_ADDR; msgs.flags = RT_I2C_RD; msgs.buf = buf; msgs.len = len; /* Call the I2C device interface to transfer data */ if (rt_i2c_transfer(bus, &msgs, 1) == 1) { return RT_EOK; } else { return -RT_ERROR; } } static void read_temp_humi(float *cur_temp, float *cur_humi) { rt_uint8_t temp[6]; write_reg(i2c_bus, AHT10_GET_DATA, 0); /* send command */ rt_thread_mdelay(400); read_regs(i2c_bus, 6, temp); /* obtian sensor data */ /* Humidity data conversion */ *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20); /* Temperature data conversion */ *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50; } static void aht10_init(const char *name) { rt_uint8_t temp[2] = {0, 0}; /* Find the I2C bus device and get the I2C bus device handle */ i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name); if (i2c_bus == RT_NULL) { rt_kprintf("can't find %s device!\n", name); } else { write_reg(i2c_bus, AHT10_NORMAL_CMD, temp); rt_thread_mdelay(400); temp[0] = 0x08; temp[1] = 0x00; write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp); rt_thread_mdelay(400); initialized = RT_TRUE; } } static void i2c_aht10_sample(int argc, char *argv[]) { float humidity, temperature; char name[RT_NAME_MAX]; humidity = 0.0; temperature = 0.0; if (argc == 2) { rt_strncpy(name, argv[1], RT_NAME_MAX); } else { rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX); } if (!initialized) { /* Sensor initialization */ aht10_init(name); } if (initialized) { /* Read temperature and humidity data */ read_temp_humi(&temperature, &humidity); rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10); rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10); } else { rt_kprintf("initialize sensor failed!\n"); } } /* Export to the msh command list */ MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample); ``` # 4 系统测试 ## 4.1 硬件环境 将SHT20的电源和通信接口与Version Board连接起来,其连接图如下: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/1b9a62044e45b3e52ae1a076ba20aa05.png) 硬件实物图如下: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/6723b275d5f582b0475617b0e19993d9.png.webp) ## 4.2 软件测试方法介绍 RT-Thread已经提供了SHT20相关的测试命令,其具体测试方法如下: ### 4.2.1 探测传感器 sht2x 软件包提供了丰富的测试命令,项目只要在 RT-Thread 上开启 Finsh/MSH 功能即可。在做一些基于 sht2x 的应用开发、调试时,这些命令会非常实用,它可以准确的读取指传感器测量的温度与湿度。具体功能可以输入 sht20 ,可以查看完整的命令列表: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/b56c3ba43f4fe9eb76be419030b1edc9.png) 具体方法如下: ```shell msh />sht20 probe i2c1 #探测成功,没有错误日志 msh /> msh />sht20 probe i2c88 #探测失败,提示对应的 I2C 设备找不到 [E/sht20] can't find sht20 device on 'i2c88' msh /> ``` ### 4.2.2 读取数据 探测成功之后,输入 sht20 read 即可获取温度与湿度,包括提示信息,日志如下: ```shell msh />sht20 read read sht20 sensor humidity : 54.7 % read sht20 sensor temperature: 27.3 msh /> ``` ## 4.3 板卡级测试 ### 4.3.1 准备测试环境 1)使用Ctrl+B组合键,编译项目代码,如下编译成功,可以看见如下log: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/78b21330857ebbc2a37c20e1b50c913d.png) 2)下载代码到板卡中 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/b2f5167af4bb08d489c82378ab04b577.png.webp) 3)打开串口终端工具,如果代码能正常工作,可以看见如下log: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/2679fd8e703fa753cca9b14ac042997d.png) ### 4.3.2 测试 1)查询I2C驱动接口信息,使用如下命令: ```shell list device ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/1b083693aa7f44330a004a052e8f93fc.png) 2)检测SHT20在线状态 探测失败状态: ```shell sht20 probe sci3 ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/202ec3a8ab999aaea47e0facd654e4bc.png.webp) 探测成功状态: ```shell sht20 probe sci3i ``` ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/3ff72ca2111b685c1e2bf37977d4c96b.png) 改变环境温度,观察传感器温湿度的变化情况: ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240728/636ea8e48bfb6fcc095c79ecbcff24da.png.webp)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
mfang2023
南亭少翁
文章
2
回答
0
被采纳
0
关注TA
发私信
相关文章
1
RT-THREAD在STM32H747平台上移植lwip
2
正点原子miniSTM32开发板读写sdcard
3
反馈rtt串口驱动对低功耗串口lpuart1不兼容的问题
4
Keil MDK 移植 RT-Thread Nano
5
RT1061/1052 带 RTT + LWIP和LPSPI,有什么坑要注意吗?
6
RT thread HID 如何收发数据
7
求一份基于RTT系统封装好的STM32F1系列的FLASH操作程序
8
RT-Thread修改项目名称之后不能下载
9
rt-studio编译c++
10
有木有移植rt-thread(nano)到riscv 32位MCU上
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
张世争
8
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部