Skip to content

Commit

Permalink
Fix calculation of negative temperatures with DS18B20 sensor.
Browse files Browse the repository at this point in the history
  • Loading branch information
breaker27 committed Feb 7, 2015
1 parent 9df91ff commit f71d696
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions firmware/shc_envsensor/onewire.c
Expand Up @@ -248,13 +248,13 @@ int16_t onewire_get_temperature(uint8_t * id_array)
case ONEWIRE_FAMILY_CODE_DS18B20:
// get temperature raw value
res = ((uint16_t)(tmp[1] & 0b00000111) << 8) + tmp[0];

// clear undefined bits if lower resolution is configured in DS18B20 e2p
uint8_t undefined_bits = 3 - ((tmp[4] >> 5) & 3);
res = res & (0b1111111111111111 << undefined_bits);

if (tmp[1] >> 7 == 1)
res = -4096 + res;
res = -2048 + res;

res = (int16_t)((int32_t)res * 625 / 100);
break;
Expand Down

0 comments on commit f71d696

Please sign in to comment.