-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax31856.py
More file actions
208 lines (171 loc) · 6.51 KB
/
max31856.py
File metadata and controls
208 lines (171 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
#The MIT License (MIT)
#
#Copyright (c) 2015 Stephen P. Smith
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
import time, math
import RPi.GPIO as GPIO
class max31856(object):
"""Read Temperature on the Raspberry PI from the MAX31856 chip using GPIO
Any pins can be used for CS (chip select), MISO, MOSI and CLK
"""
def __init__(self, csPin = 8, misoPin = 9, mosiPin = 10, clkPin = 11):
self.csPin = csPin
self.misoPin = misoPin
self.mosiPin = mosiPin
self.clkPin = clkPin
self.setupGPIO()
#
# Config Register 2
# ------------------
# bit 7: Reserved -> 0
# bit 6: Averaging Mode 1 Sample -> 0 (default)
# bit 5: Averaging Mode 1 Sample -> 0 (default)
# bit 4: Averaging Mode 1 Sample -> 0 (default)
# bit 3: Thermocouple Type -> K Type (default) -> 0 (default)
# bit 2: Thermocouple Type -> K Type (default) -> 0 (default)
# bit 1: Thermocouple Type -> K Type (default) -> 1 (default)
# bit 0: Thermocouple Type -> K Type (default) -> 1 (default)
#
self.writeRegister(1, 0x03)
def setupGPIO(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.csPin, GPIO.OUT)
GPIO.setup(self.misoPin, GPIO.IN)
GPIO.setup(self.mosiPin, GPIO.OUT)
GPIO.setup(self.clkPin, GPIO.OUT)
GPIO.output(self.csPin, GPIO.HIGH)
GPIO.output(self.clkPin, GPIO.LOW)
GPIO.output(self.mosiPin, GPIO.LOW)
def readThermocoupleTemp(self):
self.requestTempConv()
# read 4 registers starting with register 12
out = self.readRegisters(0x0c, 4)
[tc_highByte, tc_middleByte, tc_lowByte] = [out[0], out[1], out[2]]
temp = ((tc_highByte << 16) | (tc_middleByte << 8) | tc_lowByte) >> 5
if (tc_highByte & 0x80):
temp -= 0x80000
temp_C = temp * 0.0078125
fault = out[3]
if ((fault & 0x80) == 1):
raise FaultError("Cold Junction Out-of-Range")
if ((fault & 0x40) == 1):
raise FaultError("Thermocouple Out-of-Range")
if ((fault & 0x20) == 1):
raise FaultError("Cold-Junction High Fault")
if ((fault & 0x10) == 1):
raise FaultError("Cold-Junction Low Fault")
if ((fault & 0x08) == 1):
raise FaultError("Thermocouple Temperature High Fault")
if ((fault & 0x04) == 1):
raise FaultError("Thermocouple Temperature Low Fault")
if ((fault & 0x02) == 1):
raise FaultError("Overvoltage or Undervoltage Input Fault")
if ((fault & 0x01) == 1):
raise FaultError("Thermocouple Open-Circuit Fault")
return temp_C
def readJunctionTemp(self):
self.requestTempConv()
# read 3 registers starting with register 9
out = self.readRegisters(0x09, 3)
offset = out[0]
[junc_msb, junc_lsb] = [out[1], out[2]]
temp = ((junc_msb << 8) | junc_lsb) >> 2
temp = offset + temp
if (junc_msb & 0x80):
temp -= 0x4000
temp_C = temp * 0.015625
return temp_C
def requestTempConv(self):
#
# Config Register 1
# ------------------
# bit 7: Conversion Mode -> 0 (Normally Off Mode)
# bit 6: 1-shot -> 1 (ON)
# bit 5: open-circuit fault detection -> 0 (off)
# bit 4: open-circuit fault detection -> 0 (off)
# bit 3: Cold-junction temerature sensor enabled -> 0 (default)
# bit 2: Fault Mode -> 0 (default)
# bit 1: fault status clear -> 1 (clear any fault)
# bit 0: 50/60 Hz filter select -> 0 (60Hz)
#
# write config register 0
self.writeRegister(0, 0x42)
# conversion time is less than 150ms
time.sleep(.2) #give it 200ms for conversion
def writeRegister(self, regNum, dataByte):
GPIO.output(self.csPin, GPIO.LOW)
# 0x8x to specify 'write register value'
addressByte = 0x80 | regNum;
# first byte is address byte
self.sendByte(addressByte)
# the rest are data bytes
self.sendByte(dataByte)
GPIO.output(self.csPin, GPIO.HIGH)
def readRegisters(self, regNumStart, numRegisters):
out = []
GPIO.output(self.csPin, GPIO.LOW)
# 0x to specify 'read register value'
self.sendByte(regNumStart)
for byte in range(numRegisters):
data = self.recvByte()
out.append(data)
GPIO.output(self.csPin, GPIO.HIGH)
return out
def sendByte(self,byte):
for bit in range(8):
GPIO.output(self.clkPin, GPIO.HIGH)
if (byte & 0x80):
GPIO.output(self.mosiPin, GPIO.HIGH)
else:
GPIO.output(self.mosiPin, GPIO.LOW)
byte <<= 1
GPIO.output(self.clkPin, GPIO.LOW)
def recvByte(self):
byte = 0x00
for bit in range(8):
GPIO.output(self.clkPin, GPIO.HIGH)
byte <<= 1
if GPIO.input(self.misoPin):
byte |= 0x1
GPIO.output(self.clkPin, GPIO.LOW)
return byte
class FaultError(Exception):
pass
if __name__ == "__main__":
import max31856
cs0Pin = 8
cs1Pin = 25
misoPin = 9
mosiPin = 10
clkPin = 11
max0 = max31856.max31856(cs0Pin,misoPin,mosiPin,clkPin)
thermo0TempC = max0.readThermocoupleTemp()
thermo0TempF = (thermo0TempC * 9.0/5.0) + 32
print "Thermocouple 0 Temp: %f degF" % thermo0TempF
max1 = max31856.max31856(cs1Pin,misoPin,mosiPin,clkPin)
thermo1TempC = max1.readThermocoupleTemp()
thermo1TempF = (thermo1TempC * 9.0/5.0) + 32
print "Thermocouple 1 Temp: %f degF" % thermo1TempF
juncTempC = max0.readJunctionTemp()
juncTempF = (juncTempC * 9.0/5.0) + 32
print "Cold Junction Temp: %f degF" % juncTempF
GPIO.cleanup()