话不多说上图:
完成的数据手册百度吧 嘻嘻
这是SHTC31000芯片的时序图:
这个怎么看我就不多解释了哈
SHTC3.h
#ifndef SHTC3_SHTC3_H_
#define SHTC3_SHTC3_H_
#include "main.h"
typedef struct
{
uint8_t HumidityMSB;
uint8_t HumidityLSB;
uint8_t HumidityCRC;
uint8_t temperatureMSB;
uint8_t temperatureLSB;
uint8_t temperatureCRC;
}SHTC3_MeasureData;
typedef struct
{
uint8_t temp;
uint8_t hum;
}SHTC31000;
extern SHTC31000 SHTC3_Data;
void SHTC3_init();
//void SHTC3_GoSleep();//休眠
void SHTC3_Read();
void SHTC3_Refresh();//实时刷新当前温度湿度
#endif /* SHTC3_SHTC3_H_ */
上代码:SHTC3.c
#include "SHTC3.h"
#include "i2c.h"
#include "retarget.h"
#include "systemTimer.h"
#define SHTC3_Address 0x07
#define SHTC3_Aaddress_W 0xE0
#define SHTC3_Aaddress_R 0xE1
#define SHTC3_WakeUp 0X3517
#define SHTC3_Sleep 0xB098
#define SHTC3_MeasuringCmd 0x5C24
SHTC3_MeasureData shtc3Read;
SHTC31000 SHTC3_Data;
void SHTC3_init()
{
uint8_t cmdTemp[2];
cmdTemp[0] = (uint8_t)(SHTC3_WakeUp>>8);
cmdTemp[1] = (uint8_t)(SHTC3_WakeUp);
HAL_I2C_Master_Transmit(&hi2c1, SHTC3_Aaddress_W, (uint8_t *)&cmdTemp, sizeof(cmdTemp), 1000);
HAL_Delay(10);
cmdTemp[0] = (uint8_t)(SHTC3_MeasuringCmd>>8);
cmdTemp[1] = (uint8_t)(SHTC3_MeasuringCmd);
HAL_I2C_Master_Transmit(&hi2c1, SHTC3_Aaddress_W, (uint8_t *)&cmdTemp, sizeof(cmdTemp), 1000);
HAL_Delay(10);
}
//void SHTC3_GoSleep()
//{
// uint8_t cmdTemp[2];
//
// cmdTemp[0] = (uint8_t)(SHTC3_Sleep>>8);
// cmdTemp[1] = (uint8_t)(SHTC3_Sleep);
// HAL_I2C_Master_Transmit(&hi2c1, SHTC3_Aaddress_W, (uint8_t *)&cmdTemp, sizeof(cmdTemp), 1000);
//}
void SHTC3_Read()
{
float hum,temp;
HAL_I2C_Master_Receive(&hi2c1, SHTC3_Aaddress_R, (uint8_t *)&shtc3Read, sizeof(shtc3Read), 1000);
hum = (shtc3Read.HumidityMSB << 8)|(shtc3Read.HumidityLSB);
temp = (shtc3Read.temperatureMSB << 8 )|shtc3Read.temperatureLSB;
SHTC3_Data.hum = (uint8_t)(100*((float)hum/65535));//湿度
SHTC3_Data.temp = (uint8_t)((175*((float)temp/65535)) - 45);//温度
//
// printf(" SHTC3_Data.hum is %d",SHTC3_Data.hum);
// printf(" SHTC3_Data.temp is %d",SHTC3_Data.temp);
}
void SHTC3_Refresh()//实时刷新当前温度湿度
{
SHTC3_init();
SHTC3_Read();
}
主函数中调用void SHTC3_Refresh()//实时刷新当前温度湿度
SHTC3_Data.hum 湿度
SHTC3_Data.temp 温度