ZHCAFX9 August 2025 HDC1010 , HDC1080 , HDC2010 , HDC2021 , HDC2022 , HDC2080 , HDC3020 , HDC3020-Q1 , HDC3021 , HDC3021-Q1 , HDC3022 , HDC3022-Q1 , HDC3120
本節(jié)演示了如何使用超高分辨率設置來配置 HDC302x 器件并從中讀取測量值。所有示例代碼都對 I2C 地址使用全局變量,并可根據(jù)用戶的器件配置輕松更改。
在讀取測量值之前,必須先對器件進行配置??梢酝ㄟ^向 HDC302x 發(fā)送特定的命令序列來完成該操作。圖 2-6 中顯示了此配置的圖示。
Wire.beginTransmission(0x44); // Initiate communication with HDC302x
Wire.write(0x24); // Write Command MSB to device.
Wire.write(0x00); // Write Command LSB to device.
Wire.endTransmission();
delay(25); //wait 25ms before reading
需要延遲一段時間來確保測量轉(zhuǎn)換已完成,然后再開始讀取。在本例中,由于測量配置基于最高分辨率和可重復性,因此使用了 15ms 延遲。盡管應實現(xiàn) 15ms 的最小延遲,但在設置適當?shù)难舆t之前,工程師應參考數(shù)據(jù)表。
void loop() {
float humidity;
float temp;
// send device command for highest repeatability
Wire.beginTransmission(0x44);
Wire.write(0x24); //send MSB of command
Wire.write(0x00); //command LSB
Wire.endTransmission();
delay(15); //wait 15ms before reading
Wire.requestFrom(0x44, 6); //request 6 bytes from HDC device
Wire.readBytes(HDC_DATA_BUFF, 6); //move 6 data bytes into buffer
temp = getTemp(HDC_DATA_BUFF);
Serial.print("Temp (C): "); // print final temp value
Serial.println(temp);
delay(1000); // wait 1 second (optional)
humidity = getHum(HDC_DATA_BUFF);
Serial.print("Humidity (RH): "); // print final humidity value
Serial.print(humidity);
Serial.println("%");
delay(1000); // wait 1 second (optional)
}
數(shù)據(jù)轉(zhuǎn)換功能
使用 HDC302x 數(shù)據(jù)表中提供的公式計算溫度和濕度:
// function processes raw temperature values and returning final value
float getTemp(uint8_t humBuff[]) {
float tempConv;
float celsius;
TEMP_MSB = humBuff[0] << 8 | humBuff[1]; //shift 8 bits of data in
//first array index to get
//MSB then OR with LSB
tempConv = (float)(TEMP_MSB); // convert uint8_t temp value
celsius = ((tempConv / 65535) * 175) - 45; // calculate celcius
return celsius;
}
// function for processing raw humidity values and returning final value
float getHum(uint8_t humBuff[]) {
float humConv;
float humidity;
HUM_MSB = (humBuff[3] << 8) | humBuff[4]; //shift 8 bits of data in
//first array index to get
//MSB then OR with LSB
humConv = (float)(HUM_MSB); // convert uint8_t humidity value
humidity = (humConv / 65535) * 100; // calculate humidity
return humidity;
}
緩沖器結構的注釋
六字節(jié)緩沖區(qū) HDC_DATA_BUFF 包含:
盡管本示例不使用 CRC 字節(jié),但這些字節(jié)包含在緩沖區(qū)中以確保完整,如果需要,可以檢查這些字節(jié)以確保數(shù)據(jù)完整性。
請訪問以下鏈接,查看 HDC302x 在按需觸發(fā)模式下的完整示例代碼。