stm32hal库应用笔记之硬件IIC读写SHTC31000温湿度传感器

本文介绍了SHTC3温湿度传感器的时序图,并展示了STM32平台上的驱动代码实现,包括初始化、读取和实时刷新数据的函数。代码中使用了I2C通信协议,通过HAL库进行数据传输,获取并转换传感器测量的湿度和温度数据。
摘要由CSDN通过智能技术生成

话不多说上图:
完成的数据手册百度吧 嘻嘻
这是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 温度

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值