Here is an alternate reader if using hardware SPI. Untested on the device as I don't have one.
Code: Select all
#!/usr/bin/env python
# MAX318555.py
# 2017-02-11
# Public Domain
import time
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
"""
This script reads the temperature of a thermocouple
connected to a MAX31855 SPI chip.
"""
FAULT_D17=16
FAULT_D3=8
FAULT_SCV=4
FAULT_SCG=2
FAULT_OC=1
simDat=[0x64007f00, # 1600.0 127.0
0x3e806490, # 1000.0 100.5625
0x064c1900, # 100.75 25.0
0x01900000, # 25.0 0.0
0x0000fff0, # 0.0 -0.0625
0xfffcff00, # -0.25 -1.0
0xfff0ec00, # -1.0 -20.0
0xf060c900, # -250.0 -55.0
0xf061c900, # -250.0 -55.0 D17 error
0xf060c908, # -250.0 -55.0 D3 error
0xf060c901, # -250.0 -55.0 OC error
]
sdp=-1
def simulate():
global sdp
sdp += 1
if sdp >= len(simDat):
sdp = 0
return 4, [(simDat[sdp]>>24)&0xff, (simDat[sdp]>>16)&0xff,
(simDat[sdp]>>8)&0xff, simDat[sdp]&0xff]
pi = pigpio.pi()
if not pi.connected:
exit(0)
# pi.spi_open(0, 1000000, 0) # CE0, 1Mbps, main SPI
# pi.spi_open(1, 1000000, 0) # CE1, 1Mbps, main SPI
# pi.spi_open(0, 1000000, 256) # CE0, 1Mbps, auxiliary SPI
# pi.spi_open(1, 1000000, 256) # CE1, 1Mbps, auxiliary SPI
# pi.spi_open(2, 1000000, 256) # CE2, 1Mbps, auxiliary SPI
sensor = pi.spi_open(0, 1000000, 0) # CE0, 1Mbps, main SPI
stop = time.time() + 60
while time.time() < stop:
c, d = pi.spi_read(sensor, 4)
# c, d = simulate()
if c == 4:
# Thermocouple temperature
t = ((d[0]<<8)|d[1])>>2
if d[0] & 0x80:
t -= 16384
t /= 4.0
# Internal temperature
int_t = ((d[2]<<8)|d[3])>>4
if d[2] & 0x80:
int_t -= 4096
int_t /= 16.0
# Fault
fault = d[3] & 0x0f
if d[1] & 1: # D17 error
fault |= FAULT_D17
if fault == 0: # No fault
print("t={:.2f} int_t={:.4f}".format(t, int_t))
else:
print("fault={}".format(fault))
time.sleep(0.25) # read 4 times a second.
pi.spi_close(sensor)
pi.stop()