1、将sht3x连接到开发板上,SCL、SDA分别连接PB8,PB9. 2、通过menuconfig使能IIC1接口 配置IIC的SDA、SCL到引脚PB9,PB10上。 #define BSP_I2C1_SCL_PIN 24 #define BSP_I2C1_SDA_PIN 25 3、通过menuconfig使能sht3x 将sht3x挂载到i2c1上。 dev = sht3x_init("i2c1", SHT3X_ADDR_PD); 4、移植sht3x驱动到sensor中 在工程中新建一个sht3xTemp.c文件,在文件里完成sht3x设备的初始化、注册sensor。 static int rt_hw_sht3xTemp_port(void) { struct rt_sensor_config cfg; rt_hw_sht3xTemp_init("sh3x", &cfg); return RT_EOK; } INIT_COMPONENT_EXPORT(rt_hw_sht3xTemp_port); 将rt_hw_sht3xTemp_port作为组件自动调用。 int rt_hw_sht3xTemp_init(const char *name, struct rt_sensor_config *cfg)函数将sht3x设备注册到senseor中。 提供shr3x_temp_fetch_data,shr3x_temp_control供sensor框架调用。 static struct rt_sensor_ops sensor_ops = { shr3x_temp_fetch_data, shr3x_temp_control };
到此为止已经将sht3x注册到了sensor框架中,以后就可以通过sensor框架进行操作。 void readSht3x(void) { rt_device_t dev3 = RT_NULL; struct rt_sensor_data sensor_data; rt_size_t res; dev3 = rt_device_find("temp_sh3x"); if (dev3 == RT_NULL) { rt_kprintf("Can't find device:sh3x\n"); }else { if (rt_device_open(dev3, RT_DEVICE_FLAG_RDWR) != RT_EOK) { rt_kprintf("open device failed!\n"); } rt_device_control(dev3, RT_SENSOR_CTRL_SET_ODR, (void *)100); } if (dev3 != RT_NULL) { res = rt_device_read(dev3, 0, &sensor_data, 2); if (res != 1) { rt_kprintf("read data failed!size is %d\n", res); rt_device_close(dev3); } else { freData.RealTimeData.enviromentParam.ambientTemp = (sensor_data.data.temp>>16)*MULTIPLE/10; freData.RealTimeData.enviromentParam.ambientHumidity = (sensor_data.data.temp&0xFFFF)*MULTIPLE/10; rt_kprintf("sht3x temperature:%d.%dC humidity:%d.%d% timestamp:%5d\n",freData.RealTimeData.enviromentParam.ambientTemp / 100, freData.RealTimeData.enviromentParam.ambientTemp % 100,\ freData.RealTimeData.enviromentParam.ambientHumidity / 100, freData.RealTimeData.enviromentParam.ambientHumidity % 100, sensor_data.timestamp); freData.timeStamp = sensor_data.timestamp; //时间戳 } } }
5、编译工程,测试读取sht温湿度数据获取情况如下图:
|