Skip to content

Commit

Permalink
fix handling of negative temps
Browse files Browse the repository at this point in the history
highest bit in temperature registers is sign
1 = negative, 0 = positive
  • Loading branch information
Marty committed Nov 17, 2013
1 parent 438f2ea commit 47787f0
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Adafruit_AM2315.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,24 @@ boolean Adafruit_AM2315::readData(void) {
humidity /= 10;
//Serial.print("H"); Serial.println(humidity);

temp = reply[4];
// test negative temp (-10.1C)
//reply[4] = 0x80;
//reply[5] = 0x65;
// test negative temp (-100.1C)
//reply[4] = 0x83;
//reply[5] = 0xE9;
// test positive temp (100.1C)
//reply[4] = 0x03;
//reply[5] = 0xE9;

temp = reply[4] & 0x7F; // high bit is sign
temp *= 256;
temp += reply[5];
temp /= 10;

// change sign
if (reply[4] >> 7) temp = -temp;

//Serial.print("T"); Serial.println(temp);

return true;
Expand Down

0 comments on commit 47787f0

Please sign in to comment.