ZHCU873D June 2021 – February 2025 HDC3020 , HDC3020-Q1 , HDC3021 , HDC3021-Q1 , HDC3022 , HDC3022-Q1 , HDC3120 , HDC3120-Q1
示例 C 代碼可以在 SysConfg 中通過 ASCStudio 找到:HDC302x 示例代碼。
以下代碼提供了一個示例,說明如何使用 Arduino 對 HDC302x 執(zhí)行溫度和濕度讀取操作:
//HDC302x Sample Code v0.2
//Texas Instruments
//HG
#include <Wire.h>
// Data buffer to hold data from HDC Sensor
uint8_t HDC_DATA_BUFF[4];
//Humidity Variables
uint16_t HUM_MSB;
uint16_t HUM_DEC;
uint16_t HUM_OUTPUT;
//Temperature Variables
uint16_t TEMP_MSB;
uint16_t TEMP_DEC;
uint16_t TEMP_OUTPUT;
//Device and Address Configurations
#define DEVICE_ADDR 0x45 //
//Lowest noise, highest repeatability
#define DEVICE_CONFIG_MSB 0x24 //
#define DEVICE_CONFIG_LSB 0x00 //
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(115200);
Serial.println("HDC302x Sample Code");
}
void loop() {
float humidity;
float temp;
// send device command for highest repeatability
Wire.beginTransmission(DEVICE_ADDR);
Wire.write(0x24); //send MSB of command
Wire.write(0x00); //command LSB
Wire.endTransmission();
delay(25); //wait 25 ms before reading
Wire.requestFrom(DEVICE_ADDR, 6); //request 6 bytes from HDC device
Wire.readBytes(HDC_DATA_BUFF, 6); //move 6 bytes from HDC into a temporary buffer
temp = getTemp(HDC_DATA_BUFF);
Serial.print("Temp (C): ");
Serial.println(temp);
delay(1000);
humidity = getHum(HDC_DATA_BUFF);
Serial.print("Humidity (RH): ");
Serial.print(humidity);
Serial.println("%");
delay(1000);
}
float getTemp(uint8_t humBuff[]) {
float tempConv;
float celsius;
TEMP_MSB = humBuff[0] << 8 | humBuff[1]; //shift 8 bits off data in first array index to get MSB then OR with LSB
tempConv = (float)(TEMP_MSB);
celsius = ((tempConv / 65535) * 175) - 45; //calculate celcius using formula in data sheet
return celsius;
}
float getHum(uint8_t humBuff[]){
float humConv;
float humidity;
HUM_MSB = (humBuff[3] << 8) | humBuff[4]; //shift 8 bits off data in first array index to get MSB then OR with LSB
humConv = (float)(HUM_MSB);
humidity = (humConv / 65535) * 100; //calculate celcius using formula in datasheet
return humidity;
}