Skip to content

Instantly share code, notes, and snippets.

@hamsolodev
Created April 12, 2012 06:26
Show Gist options
  • Save hamsolodev/2365075 to your computer and use it in GitHub Desktop.
Save hamsolodev/2365075 to your computer and use it in GitHub Desktop.
convert two bytes from DS18B20 temperature reading register into usable temperature
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
@hamsolodev
Copy link
Author

works for default 12 bit resolution, not so sure about 11, 10, and 9 bit resolution settings…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment