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 551f342
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions components/ds18b20/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1

- Fix the issue that sign-bit is not extended properly when doing temperature value conversion.

## 0.1.0

- Initial driver version, based on the [onewire_bus](https://components.espressif.com/components/espressif/onewire_bus) library.
2 changes: 1 addition & 1 deletion components/ds18b20/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.1.0"
version: "0.1.1"
description: DS18B20 device driver
url: https://github.com/espressif/esp-bsp/tree/master/components/ds18b20
dependencies:
Expand Down
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 551f342

Please sign in to comment.