From a1e02fc6a898475316d18a27f61092eabcefccb0 Mon Sep 17 00:00:00 2001 From: petur Date: Sun, 25 Mar 2018 22:56:01 +0200 Subject: [PATCH] fix for ds18b20 negative decimals ds18b20 decimals do not take into account the sign bit. Since the original calculation was not so readable, rewritten in readable way that also fixes the bug. Same code as PR against master. --- app/modules/ds18b20.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/modules/ds18b20.c b/app/modules/ds18b20.c index b07bc173..a9df0ab1 100644 --- a/app/modules/ds18b20.c +++ b/app/modules/ds18b20.c @@ -192,6 +192,7 @@ static int ds18b20_lua_read(lua_State *L) { static int ds18b20_read_device(uint8_t *ds18b20_device_rom) { lua_State *L = lua_getstate(); + int16_t ds18b20_raw_temp; if (onewire_crc8(ds18b20_device_rom,7) == ds18b20_device_rom[7]) { @@ -216,8 +217,9 @@ static int ds18b20_read_device(uint8_t *ds18b20_device_rom) { lua_pushfstring(L, "%d:%d:%d:%d:%d:%d:%d:%d", ds18b20_device_rom[0], ds18b20_device_rom[1], ds18b20_device_rom[2], ds18b20_device_rom[3], ds18b20_device_rom[4], ds18b20_device_rom[5], ds18b20_device_rom[6], ds18b20_device_rom[7]); ds18b20_device_scratchpad_conf = (ds18b20_device_scratchpad[4] >> 5) + 9; - ds18b20_device_scratchpad_temp = ((int8_t)(ds18b20_device_scratchpad[1] << 4) + (ds18b20_device_scratchpad[0] >> 4) + ((double)(ds18b20_device_scratchpad[0] & 0x0F) / 16)); - ds18b20_device_scratchpad_temp_dec = ((double)(ds18b20_device_scratchpad[0] & 0x0F) / 16 * 1000); + ds18b20_raw_temp = ((ds18b20_device_scratchpad[1] << 8) | ds18b20_device_scratchpad[0]); + ds18b20_device_scratchpad_temp = (double)ds18b20_raw_temp / 16; + ds18b20_device_scratchpad_temp_dec = (ds18b20_raw_temp - (ds18b20_raw_temp / 16 * 16)) * 1000 / 16; if (ds18b20_device_scratchpad_conf >= ds18b20_device_res) { ds18b20_device_res = ds18b20_device_scratchpad_conf;