handling with the negative temperature in the sensor tmp102 (#6316)

This commit is contained in:
星野SKY 2024-03-04 09:02:40 +08:00 committed by GitHub
parent 0298adb1d8
commit bc74dd4980
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,7 +28,7 @@ void TMP102Component::dump_config() {
} }
void TMP102Component::update() { void TMP102Component::update() {
uint16_t raw_temperature; int16_t raw_temperature;
if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) { if (this->write(&TMP102_REGISTER_TEMPERATURE, 1) != i2c::ERROR_OK) {
this->status_set_warning(); this->status_set_warning();
return; return;
@ -39,7 +39,9 @@ void TMP102Component::update() {
return; return;
} }
raw_temperature = i2c::i2ctohs(raw_temperature); raw_temperature = i2c::i2ctohs(raw_temperature);
if (raw_temperature & 0x8000) {
raw_temperature |= 0xF000;
}
raw_temperature = raw_temperature >> 4; raw_temperature = raw_temperature >> 4;
float temperature = raw_temperature * TMP102_CONVERSION_FACTOR; float temperature = raw_temperature * TMP102_CONVERSION_FACTOR;
ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature); ESP_LOGD(TAG, "Got Temperature=%.1f°C", temperature);