Created
April 12, 2012 06:26
-
-
Save hamsolodev/2365075 to your computer and use it in GitHub Desktop.
convert two bytes from DS18B20 temperature reading register into usable temperature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ds18b20_convert(lsb, msb): | |
""" | |
Converts the 16-bit sign-extended two’s complement number, stored | |
in the DS18B20's temperature register, into a temperature. | |
""" | |
reading = lsb + (msb << 8) | |
inv = reading & 0x8000 | |
if inv: # two's compliment | |
reading = (reading ^ 0xffff) + 1 | |
val = reading / 16.0 | |
return inv and -val or val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works for default 12 bit resolution, not so sure about 11, 10, and 9 bit resolution settings…