以下为SHT10的参考资料:STM32 SHT10温湿度传感器的信号采集 SHT20技术手册
首先讲讲SHT10这款温室度传感器。SHT1x(包括SHT10,SHT11和SHT15)属于Sersirion温湿度传感器家族中的贴片封装系列。更之前我讲过的DHT11这款温湿度传感器相比,体积小了许多,特别适合用于产品中。SHT10温湿度传感器包括一个电容性聚合体测湿敏感元件、一个用能隙材料制成的测温元件(文绉绉的),传感器内部有一个精度高达14为位的A/D转换器,适应串行接口电路实现无缝连接。该产品具有品质卓越、响应速度速度快,抗干扰能力强、性价比高等优点。
1、接口定义:
SHT10的接口定义如下图所示:
如上图所示,1脚为GND,4脚为VDD。它的供电电压范围为2.4~5.5V,建议的电压为3.3V,在电源引脚(VDD、GND)之间必须加上一个0.1uf的电容,应于去耦滤波用。它的2脚DATA为数据引脚,3脚SCK为时钟控制引脚,没有发现这两个引脚很像IIC所使用的引脚功能?没错,这个传感器确实可以认为是IIC接口,但是又有却别。该传感器不能按照IIC的协议编址,但是,如果IIC总线上没有挂接别的元件,传感器可以直接连到IIC总线上,但是单片机必须按照传感器的协议工作。传感器与单片机的接线如下图所示:
说明: 应用STM32驱动SHT20
I2C 部分驱动参考:http://www.openedv.com/forum.php?mod=viewthread&tid=100058
测试结果:
sht2x.c
/*
This source code and any compilation or derivative thereof is the proprietary
information of mingfei.tang Lighting Holding B.V. and is confidential in nature.
Under no circumstances is this software to be combined with any
Open Source Software in any way or placed under an Open Source License
of any type without the express written permission of mingfei.tang Holding B.V.
*/
/*******************************************************************************
* EXPORT INCLUDE FILES
*******************************************************************************/
#include "I2C.h"
#include "Stm32hal.h"
/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/
#include "sht2x.h"
/******************************************************************************
* LOCAL MACROS AND DEFINITIONS
******************************************************************************/
#define I2C_ADDRESS (0x40 << 1)
#define POLY 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001
/******************************************************************************
* LOCAL FUNCTION DECLARATIONS
******************************************************************************/
static void i2c_Delay(uint16_t value );
static uint8_t sht2xdrv_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum);
static uint8_t sht2xdrv_CheckOk(void);
static int32_t sht2xdrv_CalcTemperatureC(uint16_t u16sT);
static int32_t sht2xdrv_CalcRH(uint16_t u16sRH);
static void i2c_Delay(uint16_t value )
{
uint16_t i;
for (i = 0; i < value; i++);
}
static uint8_t sht2xdrv_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
{
uint8_t res = 0;
uint8_t crc = 0;
uint8_t byteCtr;
//calculates 8-Bit checksum with given polynomial
for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
{
crc ^= (data[byteCtr]);
for (uint8_t bit = 8; bit > 0; --bit)
{
if (crc & 0x80)
{
crc = (crc << 1) ^ POLY;
}
else
{
crc = (crc << 1);
}
}
}
if (crc != checksum)
{
res = 1;
}
return res;
}
static uint8_t sht2xdrv_CheckOk(void)
{
if (i2c_CheckDevice( I2C_ADDRESS ) == 0)
{
/* sensor is online*/
return 1;
}
else
{
/* fail: send stop */
i2c_Stop();
return 0;
}
}
static uint8_t sht2xdrv_ReadUserRegister( uint8_t *pRegisterValue )
{
uint8_t ret = SHT2x_STATUS_OK;
uint8_t cmd = USER_REG_R;
i2c_Start();
i2c_SendByte( I2C_ADDRESS | I2C_WR ); // Device address
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
i2c_SendByte( cmd);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
//Read
i2c_Start();
i2c_SendByte(I2C_ADDRESS | I2C_RD);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
*pRegisterValue = i2c_ReadByte();
i2c_NAck();
i2c_Stop();
return ret;
cmd_fail:
i2c_Stop();
ret = SHT2x_STATUS_ERR_BAD_DATA;
return ret;
}
static int32_t sht2xdrv_CalcTemperatureC(uint16_t u16sT)
{
int32_t temperatureC; // variable for result
u16sT &= ~0x0003; // clear bits [1..0] (status bits)
/*
* Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
* optimized for integer fixed point (3 digits) arithmetic
*/
temperatureC = (((uint32_t)17572 * u16sT) >> 16) - 4685;
return (int32_t)temperatureC;
}
static int32_t sht2xdrv_CalcRH(uint16_t u16sRH)
{
uint32_t humidityRH; // variable for result
u16sRH &= ~0x0003; // clear bits [1..0] (status bits)
/*
* Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
* optimized for integer fixed point (3 digits) arithmetic
*/
humidityRH = (((uint32_t)12500 * u16sRH) >> 16) - 600;
return (int32_t)humidityRH;
}
uint8_t sht2xdrv_GetTemp( int32_t *pMeasurand )
{
uint8_t ret = SHT2x_STATUS_OK;
uint8_t checksum; //checksum
uint16_t rawdata;
uint8_t cmd = TRIG_T_MEASUREMENT_HM;
uint8_t data[3] = {0, 0, 0}; //data array for checksum v
i2c_Start();
i2c_SendByte( I2C_ADDRESS | I2C_WR ); // Device address
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
i2c_Delay( 100 );
i2c_SendByte( cmd);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
//Read
i2c_Start();
i2c_SendByte(I2C_ADDRESS | I2C_RD);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
HAL_Delay( 70 );
data[0] = i2c_ReadByte();
i2c_Ack();
data[1] = i2c_ReadByte();
i2c_Ack();
data[2] = i2c_ReadByte();
i2c_NAck();
i2c_Stop();
rawdata = ((uint16_t)data[0] << 8) | data[1];
checksum = data[2];
ret = sht2xdrv_CheckCrc(data, 2, checksum);
if (ret != SHT2x_STATUS_OK)
{
return ret;
}
*pMeasurand = sht2xdrv_CalcTemperatureC(rawdata);
cmd_fail:
i2c_Stop();
return ret;
}
/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
uint8_t sht2xdrv_GetRH( int32_t *pMeasurand )
{
uint8_t ret = SHT2x_STATUS_OK;
uint8_t checksum; //checksum
uint16_t rawdata;
uint8_t cmd = TRIG_RH_MEASUREMENT_POLL;
uint8_t data[3] = {0, 0, 0}; //data array for checksum v
i2c_Start();
i2c_SendByte( I2C_ADDRESS | I2C_WR ); // Device address
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
i2c_SendByte( cmd);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
//Read
i2c_Start();
HAL_Delay( 100 );
i2c_SendByte(I2C_ADDRESS | I2C_RD);
if (i2c_WaitAck() != 0)
{
goto cmd_fail;
}
HAL_Delay( 100 );
data[0] = i2c_ReadByte();
i2c_Ack();
data[1] = i2c_ReadByte();
i2c_Ack();
data[2] = i2c_ReadByte();
i2c_NAck();
i2c_Stop();
rawdata = ((uint16_t)data[0] << 8) | data[1];
checksum = data[2];
ret = sht2xdrv_CheckCrc(data, 2, checksum);
if (ret != SHT2x_STATUS_OK)
{
return ret;
}
*pMeasurand = sht2xdrv_CalcRH(rawdata);
cmd_fail:
i2c_Stop();
return ret;
}
uint8_t sht2xdrv_ResetSht2x( void )
上一篇:STM32驱动FLASH(W25Q128)
下一篇:FSMC(STM32)
设计资源 培训 开发板 精华推荐
- 【立创课堂】基于51单片机的温湿度报警器
- NCP300HSN30T1 3V 电压检测器的典型应用,用于具有附加迟滞的微处理器复位电路
- AD9235-65PCBZ,用于评估 AD9235BRU-65、65Msps、12 位、单 ADC 的评估套件
- wemos d1 OLED拓展板
- DER-843 - 使用 HiperPFS-4 和 LYTSwitch-6 的 42 W 2 级升压和隔离反激 PWM 可调光 LED 镇流器
- AM6TW-4805DZ ±5V 6 瓦单路输出 DC/DC 转换器的典型应用
- 具有高调光比和 LED 开路报告的 LT3756EUD-2 降压模式 1A LED 驱动器的典型应用电路
- 使用 Analog Devices 的 LT3066EDE-3.3 的参考设计
- AD8604ARQZ 作为 DAC 输出缓冲器驱动重负载的典型应用
- 使用 ROHM Semiconductor 的 BU4932 的参考设计