前言
工程的配置可以参考我的上一篇文章,两个不同地址的传感器可以挂载在一条总线上也可以和我一样分别在两个总线上,配置方法是一样的,这里提供代码进行参考。【STM32】I2C练习,HAL库读取MPU6050角度陀螺仪
sht3x.c
#include "sht3x.h" static int16_t Sht3xAddr = 0x44 << 1; // 读写位0x88 SHT3XDATATYPE Sht3x_Data; int8_t Sensor_I2C1_Read(uint16_t DevAddr, uint8_t *oData, uint16_t DataLen) { return HAL_I2C_Master_Receive(&hi2c1,DevAddr,oData,DataLen,1000); } int8_t Sensor_I2C1_Write(uint16_t DevAddr, uint8_t *iData, uint16_t DataLen) { return HAL_I2C_Master_Transmit(&hi2c1,DevAddr,iData,DataLen,1000); } void Sht3x_Read_Data(void) { uint8_t Read_Buf[6]; Read_Buf[0] = 0x24; Read_Buf[1] = 0x0b; Sensor_I2C1_Write(Sht3xAddr, Read_Buf, 2); Sensor_I2C1_Read(Sht3xAddr,Read_Buf, 6); Sht3x_Data.Temperature = (uint16_t)((Read_Buf[0]<<8) | Read_Buf[1]); //温度拼接 Sht3x_Data.Humidity = (uint16_t)((Read_Buf[3]<<8) | Read_Buf[4]); //湿度拼接 Sht3x_Data.Temperature = (175.0f * (Sht3x_Data.Temperature / 65535.0f) - 45.0f); // T = -45 + 175 * tem / (2^16-1) Sht3x_Data.Humidity = (100.0f * Sht3x_Data.Humidity / 65535.0f); // RH = hum*100 / (2^16-1) }
sht3x.h
#ifndef __SHT3X_H #define __SHT3X_H #include "main.h" typedef struct{ float Temperature; float Humidity; }SHT3XDATATYPE; extern SHT3XDATATYPE Sht3x_Data; extern I2C_HandleTypeDef hi2c1; int8_t Sensor_I2C1_Read(uint16_t DevAddr, uint8_t *oData, uint16_t DataLen); int8_t Sensor_I2C1_Write(uint16_t DevAddr, uint8_t *iData, uint16_t DataLen); void Sht3x_Read_Data(void); #endif