Skip to content

Commit

Permalink
fix(ds18b20): fix the wrong value of a sub-zero temperature
Browse files Browse the repository at this point in the history
because of the drop of the sign when doing the temperature conversion

Closes espressif#258
  • Loading branch information
suda-morris committed Jan 16, 2024
1 parent c62e19f commit e0ffc19
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion components/ds18b20/src/ds18b20.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ esp_err_t ds18b20_get_temperature(ds18b20_device_handle_t ds18b20, float *ret_te

const uint8_t lsb_mask[4] = {0x07, 0x03, 0x01, 0x00}; // mask bits not used in low resolution
uint8_t lsb_masked = scratchpad.temp_lsb & (~lsb_mask[scratchpad.configuration >> 5]);
*ret_temperature = (((int16_t)scratchpad.temp_msb << 8) | lsb_masked) / 16.0f;
// Combine the MSB and masked LSB into a signed 16-bit integer
int16_t temperature_raw = (((int16_t)scratchpad.temp_msb << 8) | lsb_masked);
// Convert the raw temperature to a float,
*ret_temperature = temperature_raw / 16.0f;

return ESP_OK;
}

0 comments on commit e0ffc19

Please sign in to comment.