目录
本人测试的是STM32L496VGx开发板(ali-developer-kit),其I2C接口如下,
其SHTC1的文档资料:
1)SHTC1地址

2)软重置

3)通讯序列
4)值计算公式

【1】采用的是RT-Thread Studio 2.2.5的工具,按开发板创建好工程后,直接打开.ioc通过STM32CubeMx配置I2C接口,本文采用的默认启用I2C2。



完成配置进行代码输出
【2】在RT-Thread Settings组件页面中打开I2C支持,或者menuconfig配置
或
在rtconfig.h或board.h调整I2C定义,本文在rtconfig.h直接定义,PIN口和在STM32CubeMx配置对应。

i2c_shtc1_sample.c
- /*
- * 程序清单:这是一个 I2C 设备使用例程
- * 例程导出了 i2c_shtc1_sample 命令到控制终端
- * 命令调用格式:i2c_shtc1_sample i2c1
- * 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
- * 程序功能:通过 I2C 设备读取温湿度传感器 shtc1 的温湿度数据并打印
- */
-
- #include
- #include
-
- #define shtc1_I2C_BUS_NAME "i2c2" /* 传感器连接的I2C总线设备名称 */
- #define shtc1_ADDR (0xE0 >> 1) /* 从机地址 7 位地址模式*/
-
- static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
- static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
-
- //字段16进制打印输出
- void out_temp(rt_uint8_t *temp, rt_uint16_t len)
- {
- for(rt_uint8_t i=0; i
- {
- rt_kprintf("%02X ", temp[i]);
- }
- rt_kprintf("\n");
- }
- //软重置
- void reSet()
- {
- rt_uint8_t temp[2] = {0X80, 0X5D};
- rt_i2c_master_send(i2c_bus,shtc1_ADDR,RT_I2C_WR,temp,2);
- }
- /*SHTC1提供了定义测量期间的传感器行为以及测量结果的传输序列的可能性。
- *每 个测量命令都会同时触发温度测量和湿度测量。
- *bool enabled时钟是否启用,bool humi是否在前*/
- void clock_set(rt_bool_t enabled,rt_bool_t humi)
- {
- rt_uint8_t temp[2] = {0, 0};
- if(enabled){
- /*时钟拉伸 已启用*/
- if(humi){
- /*湿度、温度*/
- temp[0] = 0x5C;
- temp[1] = 0x24;
- }else{
- /*温度、湿度*/
- temp[0] = 0x7C;
- temp[1] = 0xA2;
- }
- }else{
- /*时钟拉伸 丧失能力的*/
- if(humi){
- /*湿度、温度*/
- temp[0] = 0x58;
- temp[1] = 0xE0;
- }else{
- /*温度、湿度*/
- temp[0] = 0x78;
- temp[1] = 0x66;
- }
- }
- out_temp(temp,2);
- rt_i2c_master_send(i2c_bus,shtc1_ADDR,RT_I2C_WR,temp,2);
- }
-
-
- void read_temp_humi(float *cur_humi,float *cur_temp)
- {
- rt_uint8_t temp[6];
- rt_i2c_master_recv(i2c_bus,shtc1_ADDR
- ,RT_I2C_RD
- ,temp,6); /* 获取传感器数据 */
- out_temp(temp,6); /* 十六进制输出显示 */
- rt_uint32_t val01 = (temp[0] << 8 | temp[1]);
- rt_uint32_t val02 = (temp[3] << 8 | temp[4]);
- rt_kprintf("val01:%d,val02:%d\n",val01,val02);
- /* 温度数据转换 */
- *cur_temp = val01 * 175.0 / (1 << 16) - 45;
- /* 湿度数据转换 */
- *cur_humi = val02 * 100.0 / (1 << 16);
- }
-
- void shtc1_init(const char *name)
- {
- /* 查找I2C总线设备,获取I2C总线设备句柄 */
- 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
- {
- reSet();
- rt_thread_mdelay(1000);
- /*温度在前*/
- clock_set(1,0);
- rt_thread_mdelay(100);
- initialized = RT_TRUE;
- }
- }
-
- static void i2c_shtc1_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, shtc1_I2C_BUS_NAME, RT_NAME_MAX);
- }
-
- if (!initialized)
- {
- /* 传感器初始化 */
- shtc1_init(name);
- }
- int count = 0;
- while (initialized&&(count++<1))
- {
- /* 读取温湿度数据 */
- read_temp_humi(&humidity,&temperature);
-
- rt_kprintf("\nread shtc1 sensor humidity : %d.%d\n", (int)humidity, (int)(humidity * 100) % 100);
- if( temperature >= 0 )
- {
- rt_kprintf("read shtc1 sensor temperature: %d.%d\n", (int)temperature, (int)(temperature * 100) % 100);
- }
- else
- {
- rt_kprintf("read shtc1 sensor temperature: %d.%d\n", (int)temperature, (int)(-temperature * 100) % 100);
- }
- rt_thread_mdelay(1000);
- }
- }
- /* 导出到 msh 命令列表中 */
- MSH_CMD_EXPORT(i2c_shtc1_sample, i2c shtc1 sample);
编译通过后,烧录进板块,测试:

3.2 方法二,调用现成组件
在RT-Thread Settings配置中添加软件包,搜索SHTC1添加软件包

并按其说明在menuconfig开启配置,注意这种配置会改变rtconfig.h,可能会把上面对于I2C的声明即PIN定义给剔除了,如果烧录是提示设备找不到,可以查看是否出现这种情况。

参考软件包使用说明,本文的main.c代码如下,没有采用软件包建议的INIT_APP_EXPORT方式是怕I2C没初始化到:
- #include
- #include
-
- #include "sensor_sr_shtc1.h"
-
- int shtc1_port(void)
- {
- struct rt_sensor_config cfg;
-
- cfg.intf.dev_name = SHTC1_I2CBUS_NAME;
- cfg.intf.user_data = (void *)SHTC1_ADDR_DEFAULT;
-
- rt_hw_shtc1_init("shtc1", &cfg);
- return 0;
- }
-
- int main(void)
- {
- shtc1_port();
- int count = 1;
- while (count++)
- {
- rt_thread_mdelay(1000);
- }
-
- return RT_EOK;
- }
四、编辑及测试
烧录进板,测试

两种方法得到的数值几乎近似,传感器所处地南方、湿热,哈哈