SHTC3简单说明
SHTC3是一个检测温度和湿度的传感器,可以检测-40℃~125℃的温度范围和0%~100%的湿度范围,典型精度为±2 %RH和±0.2°C。
SHTC3的工作电压范围为:1.62V~3.6V。
SHTC3使用的通讯方式是I2C,通讯速率可以达到400KHz。
SHTC3只有4个引脚,电源和地、2个I2C通讯引脚。
SHTC3的I2C设备地址为0x70。
下面就是SHTC3的驱动程序。要注意SHTC3的寄存器是16位的2个字节。
#ifndef __DRV_SHTC3_H
#define __DRV_SHTC3_H
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define SHTC3_ADDRESS (0x70)
#define SHTC3_CRC_POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
/*=========================================================================*/
/*=========================================================================
REGISTERS
-----------------------------------------------------------------------*/
//ID16BIT:B15 to 12 & 10 to 6: unspecified info.bits 11 & 5 to 0: SHTC3 identifier
#define SHTC3_REG_ID (0xEFC8) //xxxx 1xxx xx00 0111
#define SHTC3_REG_SLEEP (0xB098) //
#define SHTC3_REG_WAKEUP (0x3517) //
#define SHTC3_REG_RESET (0x805D) //
//Mode1
#define SHTC3_REG_CENM_TEMP_RH (0x7CA2) //��ͨģʽ����ʱ�䳤��>10ms
#define SHTC3_REG_CENM_RH_TEMP (0x5C24) //
#define SHTC3_REG_CELPM_TEMP_RH (0x6458) //����ģʽ����ʱ���: <1ms
#define SHTC3_REG_CELPM_RH_TEMP (0x44DE) //
//Mode2
#define SHTC3_REG_CDNM_TEMP_RH (0x7866) //
#define SHTC3_REG_CDNM_RH_TEMP (0x58E0) //
#define SHTC3_REG_CDLPM_TEMP_RH (0x609C) //
#define SHTC3_REG_CDLPM_RH_TEMP (0x401A) //
//==============================================================================
class SHTC3
{
public:
SHTC3(void);
int Init(void) ;
byte GetMode1_TempRH(int *temp, int *humi);
byte write(unsigned int memory_address, unsigned char *data, int size);
byte read (unsigned int memory_address, unsigned char *data, int size);
private:
uint16_t shtc3_id;
};
#endif
下面是操作SHTC3的寄存器接口。要注意SHTC3的寄存器是16位的2个字节。
#include <Arduino.h>
#include <Wire.h>
#include "shtc3.h"
byte SHTC3::write(unsigned int memory_address, unsigned char *data, int size)
{
byte rc;
Wire.beginTransmission(SHTC3_ADDRESS);
Wire.write((memory_address>>8) & 0xFF);
Wire.write(memory_address & 0xFF);
if((data != NULL) && (size != 0))
Wire.write(data, size);
rc = Wire.endTransmission();
return (rc);
}
byte SHTC3::read(unsigned int memory_address, unsigned char *data, int size)
{
byte rc;
unsigned int cnt;
Wire.beginTransmission(SHTC3_ADDRESS);
Wire.write((memory_address>>8) & 0xFF);
Wire.write(memory_address & 0xFF);
rc = Wire.endTransmission(false);
if (rc != 0) {
return (rc);
}
Wire.requestFrom(SHTC3_ADDRESS, size, true);
if((data != NULL) && (size != 0))
{
cnt = 0;
while(Wire.available()) {
data[cnt] = Wire.read();
cnt++;
}
}
return (0);
}
将驱动程序添加到Arduino的库文件夹下。下面是测试读取代码。
#include <Wire.h>
#include <RPR-0521RS.h>
#include <shtc3.h>
RPR0521RS rpr0521rs;
SHTC3 d_shtc3;
#define myLED 15 //设置引脚15为LED引脚
void setup() {
byte rc;
pinMode(myLED, OUTPUT);
digitalWrite(myLED, HIGH);
Serial.begin(115200);
Serial.println("******************************** senser test *******************************************");
Wire.begin();
rc = rpr0521rs.init();
Serial.print(F("RPR-0521RS Init = "));
Serial.println(rc);
rc = d_shtc3.Init();
Serial.print(F("SHTC3 Init = "));
Serial.println(rc);
}
void loop() {
byte rc;
byte near_far;
unsigned short ps_val;
float als_val;
int temp;
int rh;
digitalWrite(myLED, HIGH);
delay(100);
rc = rpr0521rs.get_psalsval(&ps_val, &als_val);
if (rc == 0) {
Serial.print(F("RPR-0521RS (Proximity) = "));
Serial.print(ps_val);
near_far = rpr0521rs.check_near_far(ps_val);
if (near_far == RPR0521RS_NEAR_VAL) {
Serial.print(F(" Near,"));
} else {
Serial.print(F(" Far,"));
}
if (als_val != RPR0521RS_ERROR) {
Serial.print(F("\t (Ambient Light) = "));
Serial.print(als_val);
Serial.println(F("[lx]."));
}
}
delay(100);
rc = d_shtc3.GetMode1_TempRH(&temp, &rh);
if (rc == 0) {
Serial.print(F("SHTC3 Temp = "));
Serial.print(temp);
Serial.print("^C,");
Serial.print(F(" \t RH = "));
Serial.print(rh);
Serial.println("%");
Serial.println();
}
digitalWrite(myLED, LOW);
delay(800);
}
通过串口输出信息,可以看到温湿度读取成功。
资料: