使用STM32芯片,sht20驱动
SHT20温湿度传感器驱动代码#For CubeMX
单片机源程序如下:
- /*
- *********************************************************
- ** Filename: stop_mode.c
- ** Abstract: 使用STM32L151C8T6MCU,使用硬件IIC读取sht20温湿度传感器数值,切记要初始化IIC
- ** 使用注意事项:
- ** Date : 2018-01-04
- ** Author:王翔武
- *********************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "sht20.h"
- #include "i2c.h"
- /*============================================================
- *
- * Function Name: Read SHT20 Data
- *
- * Parameter: float *Temperature, float *Humidity,这两个参数分别返回温度和湿度数值。
- * 引用格式为SHT20_Data(&TemValue, &RH_Value);
- * &TemValue, &RH_Value是临时定义的两个变量,用于存放读取的数值。
- * Functional Description: Read Temperature and Humidity data.
-
- * Return Value:
- *
- * Common error:
- *
- * Author: John Wang 2017/11/15
- *
- ============================================================*/
- void SHT20_Data(float *Temperature, float *Humidity)
- {
- /*定义局部变量*/
- uint8_t SHT20_DataBuf [4];
- uint16_t T_Data, RH_Data;
- /*读取sht20原始数据*/
- HAL_I2C_Mem_Read(&hi2c2, SHT20_ADDR_RD, Trig_T_Addr, I2C_MEMADD_SIZE_8BIT, SHT20_DataBuf, 2, 0xFFFF);
- HAL_I2C_Mem_Read(&hi2c2, SHT20_ADDR_RD, Trig_RH_Addr, I2C_MEMADD_SIZE_8BIT, &SHT20_DataBuf[2], 2, 0xFFFF);
- T_Data = ((uint16_t)SHT20_DataBuf[0] << 8) + (SHT20_DataBuf[1] & 0xfe);
- RH_Data = ((uint16_t)SHT20_DataBuf[2] << 8) + (SHT20_DataBuf[3] & 0xfc);
- /*sht20原始数据,处理得到正常数值*/
- *Temperature = T_Data*0.00268 - 46.85;
- *Humidity = RH_Data*0.0019 - 6.0000;
- }
- /************************ Johnking *****END OF FILE****/
复制代码
这个程序目前不能运行,求大神帮忙看看 错在哪里:
SHT20温湿度传感器驱动代码#For CubeMX.rar
(4.65 KB, 下载次数: 339)
|