Skip to content

Instantly share code, notes, and snippets.

@dpslwk
Last active December 29, 2015 08:19
Show Gist options
  • Save dpslwk/7642348 to your computer and use it in GitHub Desktop.
Save dpslwk/7642348 to your computer and use it in GitHub Desktop.
B025 - RTC/EEPROM/TMP Python example for the TMP100NA Temp sensorhttp://docs.ciseco.co.uk/RET
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" B025 - RTC/EEPROM/TMP Example for the TMP100NA Temp sensor
Copyright (c) 2013 Ciseco Ltd.
Example test code by: Matt Lloyd
Orignal Temp100 Class by Adam Boardman
Which can be found at
https://github.com/adamboardman/eve-alpha-tmp100
Requires python-smbus for i2c interface
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""
import wiringpi2
class Temp100:
def __init__(self):
self.defaultAddress = 0b1001000 # 0x48
self.i2c = wiringpi2.I2C()
self.dev = self.i2c.setup(self.defaultAddress)
self.tmp100temp = 0b00
self.tmp100config = 0b01
self.tmp100tempL = 0b10
self.tmp100tempH = 0b11
self.tmp100configres = 0b10011111
self.tmp100config9bit = 0b00000000
self.tmp100config10bit = 0b00100000
self.tmp100config11bit = 0b01000000
self.tmp100config12bit = 0b01100000
self.tmp100configShutclear = 0b11111110
self.tmp100configShutdown = 0b00000001
def readByteData(self, register):
return self.i2c.readReg8(self.dev, register)
def readWordData(self, register):
return self.i2c.readReg16(self.dev, register)
def writeByteData(self, register, value):
self.i2c.writeReg8(self.dev, register, value)
def getConfiguration(self):
return self.readByteData(self.tmp100config)
def setShutdown(self):
config = self.getConfiguration()
config = config | self.tmp100configShutdown
self.writeByteData(self.tmp100config,config)
def setShutclear(self):
config = self.getConfiguration()
config = config & self.tmp100configShutclear
self.writeByteData(self.tmp100config,config)
def setResolution9bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config9bit
self.writeByteData(self.tmp100config,config)
def setResolution10bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config10bit
self.writeByteData(self.tmp100config,config)
def setResolution11bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config11bit
self.writeByteData(self.tmp100config,config)
def setResolution12bit(self):
config = self.getConfiguration()
config = config & self.tmp100configres
config = config | self.tmp100config12bit
self.writeByteData(self.tmp100config,config)
def getTemperature(self):
tempW = self.readWordData(self.tmp100temp)
#print "word: "+bin(tempW)
temp = (tempW&0xFF) + ((tempW>>12)/16.0)
#print "temp: "+str(temp)
return temp
if __name__ == "__main__":
temp = Temp100()
print("Setting resolution to 12Bits")
temp.setResolution12bit()
print("Reading Temperature")
print("Temp: {}C".format(temp.getTemperature()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment