HTS221和其它几款数字温湿度传感器数据对比
[复制链接]
HTS221是ST公司的一款数字温湿度传感器,手册上标称的湿度精度是±3.5%RH(在20%~80%范围内)温度精度是±0.5℃(在15℃~40℃范围内),今天我拿HTS221分别和TI的HDC1080、SILABS的SI7051、SI7021、sensirion的SHT21这几款数字温湿度传感器做了对比 传感器合照 因为条件有限,测试的方法是将HTS221分别和其它传感器两两装进一个纸盒,传感器尽量接近,高度尽量相同 HTS221的Outputdata设置为1Hz, 温度AVGT设置成256 每8秒钟采集一次HTS221和其它传感器数据,然后通过串口发送给PC 下面是HTS221和其它传感器的温湿度数据对比 1.HTS221和SI7051温度对比 2.HTS221和SI7021的温度数据 3.HTS221和SHT21温度对比 4.HTS221和SHT21湿度对比 5.HTS221和SHT21湿度对比 (另一片SHT21) 6.HTS221和HDC1080温度对比 7.HTS221和HDC1080湿度对比 8.HTS221和SI7021湿度对比 上图中蓝色为其它传感器的温湿度数据,橙色为HTS221的温湿度数据 从数据中可以看出HTS221数据的波动比较大,和SI7051对比时温度最大时相差0.184摄氏度 SI7051的标称精度是±0.1摄氏度,单从这个数据来看这颗HTS221的温度精度还是不错的 湿度方面HTS221比2片SHT21湿度最大SHT21高出8.5%rH 比HDC1080最大高出3%rH,比SI7021低2.53%Rh 因为相对湿度和温度的精度有关 湿度方面从这些数据也不好判断谁准谁不准
附上测试代码: - /*
- MSP430FR5969
- -----------------
- | P2.0/UCA0TXD|----> PC
- | |
- | |
- | P2.1/UCA0RXD|<---- PC
- | |
- SCL --o--|P3.5 |
- | |
- SDA --o--|P3.6 |
- */
- /*******************************************************************************
- * * INCLUDES
- * */
- #include "msp430.h"
- #include <stdio.h>
- #include <math.h>
- #include <stdint.h>
- #include "uart.h"
- #include "soft_i2c.h"
- #include "timer.h"
- #include "si7021.h"
- /*******************************************************************************
- * * MACROS
- * */
- /*******************************************************************************
- * * CONSTANTS
- * */
- #define HTS221_WHO_AM_I_REG (uint8_t)0x0F
- #define HTS221_CTRL_REG1 (uint8_t)0x20
- #define HTS221_CTRL_REG2 (uint8_t)0x21
- #define HTS221_CTRL_REG3 (uint8_t)0x22
- #define HTS221_AV_CONF_REG (uint8_t)0x10
- #define HTS221_STATUS_REG (uint8_t)0x27
- #define HTS221_HR_OUT_L_REG (uint8_t)0x28
- #define HTS221_TEMP_OUT_L_REG (uint8_t)0x2A
- #define HTS221_H0_RH_X2 (uint8_t)0x30
- #define HTS221_H1_RH_X2 (uint8_t)0x31
- #define HTS221_T0_DEGC_X8 (uint8_t)0x32
- #define HTS221_T1_DEGC_X8 (uint8_t)0x33
- #define HTS221_T0_T1_DEGC_H2 (uint8_t)0x35
- #define HTS221_H0_T0_OUT_L (uint8_t)0x36
- #define HTS221_H0_T0_OUT_H (uint8_t)0x37
- #define HTS221_H1_T0_OUT_L (uint8_t)0x3A
- #define HTS221_H1_T0_OUT_H (uint8_t)0x3B
- #define HTS221_T0_OUT_L (uint8_t)0x3C
- #define HTS221_T0_OUT_H (uint8_t)0x3D
- #define HTS221_T1_OUT_L (uint8_t)0x3E
- #define HTS221_T1_OUT_H (uint8_t)0x3F
-
-
- #define HTS221_SLV_ADDRESS (0xbe)
- /*******************************************************************************
- * * TYPEDEFS
- * */
- /*******************************************************************************
- * * GLOBAL VARIABLES
- * */
- /*******************************************************************************
- * * EXTERNAL VARIABLES
- * */
- /*******************************************************************************
- * * EXTERNAL FUNCTIONS
- * */
- /*******************************************************************************
- * * LOCAL VARIABLES
- * */
-
- uint8_t buf[12];
- float t[2],h[2];
- /*******************************************************************************
- * * PROFILE CALLBACKS
- * */
- /*******************************************************************************
- * * LOCAL FUNCTIONS
- * */
- void dco_init(void);
- /*******************************************************************************
- * * PUBLIC FUNCTIONS
- * */
- void delay(int32_t d)
- {
- while(d--)
- {
- __delay_cycles(1000);
- }
- }
- float HTS221_Get_Humidity(void)
- {
- int16_t H0_T0_out, H1_T0_out, H_T_out;
- int16_t H0_rh, H1_rh;
- uint8_t buffer[2];
- float tmp_f;
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_H0_RH_X2 | BIT7, buffer , 2);
- H0_rh = buffer[0] >> 1;
- H1_rh = buffer[1] >> 1;
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_H0_T0_OUT_L | BIT7, buffer , 2);
- H0_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_H1_T0_OUT_L | BIT7, buffer , 2);
- H1_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_HR_OUT_L_REG | BIT7, buffer , 2);
- H_T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
- tmp_f = (float)(H_T_out - H0_T0_out) * (float)(H1_rh - H0_rh) / (float)(H1_T0_out - H0_T0_out) + H0_rh;
- return tmp_f;
- }
- float HTS221_Get_Temperature(void)
- {
- int16_t T0_out, T1_out, T_out, T0_degC_x8_u16, T1_degC_x8_u16;
- int16_t T0_degC, T1_degC;
- uint8_t buffer[4], tmp;
- float tmp_f;
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_T0_DEGC_X8 | BIT7,buffer, 2);
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_T0_T1_DEGC_H2 | BIT7, &tmp,1);
- T0_degC_x8_u16 = (((uint16_t)(tmp & 0x03)) << 8) | ((uint16_t)buffer[0]);
- T1_degC_x8_u16 = (((uint16_t)(tmp & 0x0C)) << 6) | ((uint16_t)buffer[1]);
- T0_degC = T0_degC_x8_u16 >> 3;
- T1_degC = T1_degC_x8_u16 >> 3;
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_T0_OUT_L | BIT7, buffer,4);
- T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
- T1_out = (((uint16_t)buffer[3]) << 8) | (uint16_t)buffer[2];
- i2c_read_n_byte(HTS221_SLV_ADDRESS, HTS221_TEMP_OUT_L_REG | BIT7, buffer ,2);
- T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0];
- tmp_f = (float)(T_out - T0_out) * (float)(T1_degC - T0_degC) / (float)(T1_out - T0_out) + T0_degC;
- return tmp_f;
- }
- void HTS221_Init(void)
- {
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_WHO_AM_I_REG,buf,1);
- buf[0] = BIT7 | BIT0;
- i2c_write_n_byte(HTS221_SLV_ADDRESS,HTS221_CTRL_REG1, buf,1);
- buf[0] = 0x3f; //0b00111111 tAvg=256 hAvg=512
- i2c_write_n_byte(HTS221_SLV_ADDRESS,HTS221_AV_CONF_REG, buf,1);
- }
- void test() {
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_WHO_AM_I_REG,buf,1);
- buf[0] = BIT7 | BIT0;
- i2c_write_n_byte(HTS221_SLV_ADDRESS,HTS221_CTRL_REG1, buf,1);
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_STATUS_REG,buf,1);
- if(buf[0] == 0x03)
- {
- t[0] = HTS221_Get_Temperature();
- h[0] = HTS221_Get_Humidity();
- }
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_CTRL_REG2,buf,1);
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_STATUS_REG,buf,1);
- if(buf[0] == 0x03)
- {
- t[0] = HTS221_Get_Temperature();
- h[0] = HTS221_Get_Humidity();
- }
- buf[0] = BIT0 ;
- i2c_write_n_byte(HTS221_SLV_ADDRESS,HTS221_CTRL_REG2, buf,1);
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_CTRL_REG2,buf,1);
- i2c_read_n_byte(HTS221_SLV_ADDRESS,HTS221_STATUS_REG,buf,1);
- if(buf[0] == 0x03)
- {
- t[0] = HTS221_Get_Temperature();
- h[0] = HTS221_Get_Humidity();
- }
-
- _NOP();
- // i2c_write_n_byte(address,VL53L0X_REG_SYSRANGE_START, buf,1);
- }
- void HDC1080_Init(void)
- {
- uint8_t data[4];
- data[0] = BIT7;//RESET
- data[1] = 0x00;
- i2c_write_n_byte((0x40 << 1),0x02,data,2);
- }
- float HDC1080_GetHumidity()
- {
- uint8_t data[4];
- uint16_t temp;
- float humidity;
- hdc1080_i2c_read_n_byte((0x40 << 1),0x01,data,2);
- temp = ((((uint16_t)data[0]) << 8) & 0xff00) + (((uint16_t)data[1]) & 0x00ff);
-
- humidity = ((float)temp)*100/65536;
- return humidity;
- }
- float HDC1080_GetTemperature()
- {
- uint8_t data[4];
- uint16_t temp;
- float temperature;
- hdc1080_i2c_read_n_byte((0x40 << 1),0x00,data,2);
- temp = ((((uint16_t)data[0]) << 8) & 0xff00) + (((uint16_t)data[1]) & 0x00ff);
-
- temperature = (((float)(((uint32_t)temp) * 165))/65536) - 40;
- return temperature;
- }
- int main( void )
- {
- char outBuf[64];//base64缓存
- WDTCTL = WDTPW + WDTHOLD;//停止看门狗
- PM5CTL0 &= ~LOCKLPM5;//这个是一定要加的,不然GPIO不能使用
- dco_init();
- i2c_init();//初始化I2C
- SI7021_Init();
- HTS221_Init();
- HDC1080_Init();
- uart_init();
- //对比HTS221和HDC1080的湿度
- while(0)
- {
- //test();
- h[0] = HDC1080_GetHumidity();
- delay(10000);
- h[1] = HTS221_Get_Humidity();
- sprintf(outBuf,"%f\t%f",h[0],h[1]);
- uart_tx_string(outBuf);//输出到PC
- uart_tx_string("\r\n");//换行
- delay(100000);
- }
- //对比HTS221和HDC1080的温度
- while(0)
- {
- //test();
- t[0] = HDC1080_GetTemperature();
- delay(10000);
- t[1] = HTS221_Get_Temperature();
- sprintf(outBuf,"%f\t%f",t[0],t[1]);
- uart_tx_string(outBuf);//输出到PC
- uart_tx_string("\r\n");//换行
- delay(100000);
- }
- //对比HTS221和SHT21、SI7021的湿度
- while(0)
- {
- h[0] = SI7021_GetHumidityRH();
- delay(10000);
- h[1] = HTS221_Get_Humidity();
- sprintf(outBuf,"%f\t%f",h[0],h[1]);
- uart_tx_string(outBuf);//输出到PC
- uart_tx_string("\r\n");//换行
- delay(100000);
-
- }
- //对比HTS221和SI7051、SHT21的温度
- while(1)
- {
- //test();
- t[0] = SI7021_GetTemperature();
- delay(10000);
- t[1] = HTS221_Get_Temperature();
- sprintf(outBuf,"%f\t%f",t[0],t[1]);
- uart_tx_string(outBuf);//输出到PC
- uart_tx_string("\r\n");//换行
- delay(100000);
- }
- }
- void dco_init(void)
- {
- // Configure one FRAM waitstate as required by the device datasheet for MCLK
- // operation beyond 8MHz _before_ configuring the clock system.
- FRCTL0 = FRCTLPW | NWAITS_1;
- // Clock System Setup
- CSCTL0_H = CSKEY >> 8; // Unlock CS registers
- CSCTL1 = DCORSEL | DCOFSEL_4; // Set DCO to 16MHz
- CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK; // Set SMCLK = MCLK = DCO,
- // ACLK = VLOCLK
- CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers
- CSCTL0_H = 0; // Lock CS registers
- }
复制代码代码包含HTS221、SI7051、SI7021、SHT21、HDC1080驱动
|