[riot-notifications] [RIOT-OS/RIOT] drivers/ds18: fix `ds18_read` for negative temperatures (#11536)

Parks Projets notifications at github.com
Thu May 16 20:49:59 CEST 2019


### Contribution description

This pull request fixes `ds18` module when the temperature is negative. The function `ds18_read` was always returning a positive value. Also it makes DS18 test displaying negative temperatures well.

Currently, when the real temperature is `-25.0625 °C` the test displays
> `Temperature [ºC]: 138.77`

After fixing `ds18_read` function, the test was displaying
> `Temperature [ºC]: -25.-6`

And now the temperature is well displayed
> `Temperature [ºC]: -25.06`


### Testing procedure

Find a board with a DS18 module and take a trip to Antarctica (or wait for winter, [because it's coming][winter-coming]). 
Alternatively put your module in a fridge to have a negative temperature.

Also, you can test values from *Table 1*, page 6 from the [specification][spec], with both the old code and new one.  
Here with a temperature of `-25.0625 °C`, the old code returns a positive value.

```c
uint8_t b1 = 0x6F, b2 = 0xFE;  // -25.0625 °C
int32_t measurement = ((int32_t)(b2 << 8 | b1) * 625);
int16_t temperature = (int16_t)(measurement / 100);
// Here temperature is 13877
```
Whereas the new code is working as expected.

```c
uint8_t b1 = 0x6F, b2 = 0xFE;  // -25.0625 °C
int32_t measurement = ((int16_t)(b2 << 8 | b1) * 625);
int16_t temperature = (int16_t)(measurement / 100);
// Here temperature is -2506
```


### Issues/PRs references

None

[spec]: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
[winter-coming]: https://en.wikipedia.org/wiki/Winter_Is_Coming
You can view, comment on, or merge this pull request online at:

  https://github.com/RIOT-OS/RIOT/pull/11536

-- Commit Summary --

  * drivers/ds18: fix `ds18_read` with negative temperatures
  * tests/drivers_ds18: display negative numbers

-- File Changes --

    M drivers/ds18/ds18.c (2)
    M tests/driver_ds18/main.c (11)

-- Patch Links --

https://github.com/RIOT-OS/RIOT/pull/11536.patch
https://github.com/RIOT-OS/RIOT/pull/11536.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/11536
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190516/8a81b08d/attachment.html>


More information about the notifications mailing list