Merge pull request #14173 from barbudor/dht_negative_2comp

DHT support both type of negative temperature
This commit is contained in:
Theo Arends 2021-12-27 16:56:50 +01:00 committed by GitHub
commit 096687d3fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 5 deletions

View File

@ -152,11 +152,10 @@ bool DhtRead(uint32_t sensor) {
case GPIO_SI7021: // iTead SI7021
humidity = ((dht_data[0] << 8) | dht_data[1]) * 0.1;
// DHT21/22 (Adafruit):
temperature = ((int16_t)(dht_data[2] & 0x7F) << 8 ) | dht_data[3];
temperature *= 0.1f;
if (dht_data[2] & 0x80) {
temperature *= -1;
}
int16_t temp16 = dht_data[2] << 8 | dht_data[3]; // case 1 : signed 16 bits
if ((dht_data[2] & 0xF0) == 0x80) // case 2 : negative when high nibble = 0x80
temp16 = -(0xFFF & temp16);
temperature = 0.1f * temp16;
break;
}
if (isnan(temperature) || isnan(humidity)) {