Created
October 29, 2015 20:42
-
-
Save HappyCodingRobot/0bc83992fefc0cb7e633 to your computer and use it in GitHub Desktop.
Reading TI TMP100 sensor via i2c and libmpsse
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
# -*- coding: utf-8 -*- | |
from mpsse import * | |
import time | |
i2c = MPSSE(I2C, FOUR_HUNDRED_KHZ) | |
#i2c = MPSSE(I2C, ONE_HUNDRED_KHZ) | |
# Adress: \x4A (base) -> write: \x94 , read: \x95 | |
# Configuration: \x60 (01100000b : 12bit resolution) | |
WCMD = '\x94' | |
RCMD = '\x95' | |
CADR = '\x01' | |
DADR = '\x00' | |
CONF = '\x60' | |
def init(): | |
i2c.Start() # send i2c start condition | |
i2c.Write(WCMD) # send slave addr with write bit | |
i2c.Write(CADR) # set register pointer to config register | |
if i2c.GetAck() == ACK: # get ACK from slave | |
i2c.Write(CONF) # set configuration value | |
if i2c.GetAck() == ACK: # get ACK from slave | |
print 'Init done' | |
i2c.Start() # send new i2c start condition | |
i2c.Write(WCMD+DADR) # send addr with write bit and set pointer reg | |
if not i2c.GetAck() == ACK: | |
print '#no ack: set data reg' | |
return False | |
else: | |
print '#no ack: set config' | |
return False | |
else: | |
print '#no ack: set pointer reg' | |
return False | |
i2c.Stop() | |
return True | |
def getData(): | |
data = None | |
i2c.Start() # send i2c start condition | |
i2c.Write(RCMD) # send slave addr with read bit | |
if i2c.GetAck() == ACK: | |
#print 'reading data' | |
data = i2c.Read(2) # reading 2 bytes with ack | |
i2c.Stop() | |
#return list(data) | |
return [ord(data[0]),ord(data[1])*1000/256] | |
if init() == True: | |
try: | |
while True: | |
val = getData() | |
print val | |
time.sleep(.400) | |
except KeyboardInterrupt: | |
pass | |
i2c.Close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment