From d8802cddbcbc4b8bef41aedcf0a7ae56df3aecaa Mon Sep 17 00:00:00 2001 From: andrzejboro <35424249+andrzejboro@users.noreply.github.com> Date: Sun, 14 Jan 2018 17:11:45 +0100 Subject: [PATCH 1/2] Update LM75A.cpp --- LM75A/LM75A.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/LM75A/LM75A.cpp b/LM75A/LM75A.cpp index c93f2cb..3477ca7 100644 --- a/LM75A/LM75A.cpp +++ b/LM75A/LM75A.cpp @@ -98,15 +98,16 @@ float LM75A::getTemperatureInDegrees() const // Shift data (left-aligned) refactored_value >>= 5; - - // Relocate negative bit (11th bit to 16th bit) + + float real_value; if (refactored_value & 0x0400) { - refactored_value &= 0x03FF; - refactored_value |= 0x8000; + // When sign bit is set, set upper unused bits, then 2's complement + refactored_value |= 0xf800; + refactored_value = ~refactored_value + 1; + real_value = (float)refactored_value * (-1) * LM75A_DEGREES_RESOLUTION; } + else + real_value = (float)refactored_value * LM75A_DEGREES_RESOLUTION; - // Real value can be calculated with sensor resolution - real_result = (float)refactored_value * LM75A_DEGREES_RESOLUTION; - - return (float)refactored_value * LM75A_DEGREES_RESOLUTION; + return real_value; } From a13ef6cdfe3fa805bdd46c85c67eb48337e63dfb Mon Sep 17 00:00:00 2001 From: andrzejboro <35424249+andrzejboro@users.noreply.github.com> Date: Sun, 14 Jan 2018 21:13:19 +0100 Subject: [PATCH 2/2] Update LM75A.cpp --- LM75A/LM75A.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LM75A/LM75A.cpp b/LM75A/LM75A.cpp index 3477ca7..2591220 100644 --- a/LM75A/LM75A.cpp +++ b/LM75A/LM75A.cpp @@ -89,7 +89,7 @@ float LM75A::getTemperatureInDegrees() const } // Modify the value (only 11 MSB are relevant if swapped) - uint16_t refactored_value; + int16_t refactored_value; uint8_t* ptr = (uint8_t*)&refactored_value; // Swap bytes