-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCP9808.py
313 lines (222 loc) · 9.54 KB
/
MCP9808.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
"""MCP9808, python module for the MCP9808 digital temperature
sensor
created April 4, 2019
last modified April 4, 2019"""
"""
Copyright 2019 Owain Martin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import time, sys, smbus
class MCP9808:
def __init__(self, i2cAddress):
self.i2cAddress = i2cAddress
self.bus =smbus.SMBus(1)
return
def single_access_read(self, reg=0x00):
"""single_access_read, function to read a single 8 bit data register
of the MCP9808 digital temperature sensor"""
dataTransfer=self.bus.read_byte_data(self.i2cAddress,reg)
return dataTransfer
def single_word_read(self, reg=0x00):
"""single_word_read, function to read a single 16 bit data register
of the MCP9808 digital temperature sensor
data returned from sensor is LSB first (i.e. in the MSB place of
dataTransfer). Function reorders data to read normally"""
dataTransfer=self.bus.read_word_data(self.i2cAddress,reg)
dataTransfer = ((dataTransfer & 0x00FF)<<8) + ((dataTransfer & 0xFF00)>>8)
return dataTransfer
def single_access_write(self, reg=0x00, regValue = 0):
"""single_access_write, function to write to a single 8 bit data register
of the MCP9808 digital temperature sensor"""
self.bus.write_byte_data(self.i2cAddress,reg, regValue)
return
def single_word_write(self, reg=0x00, regValue = 0):
"""single_word_write, function to write to a single 16 bit data register
of the MCP9808 digital temperature sensor
data returned from sensor is LSB first (i.e. in the MSB place of
dataTransfer). Function reorders data to read normally"""
regValue = ((regValue & 0x00FF)<<8) + ((regValue & 0xFF00)>>8)
self.bus.write_word_data(self.i2cAddress, reg, regValue)
return
def twos_complement_conversion(self, tempData):
"""twos_complement_conversion, function to change the 13 bit value
split across 2 bytes from 2s complement to normal binary/decimal"""
signBit= (tempData & 0x1000)>>12
tempData = tempData & 0xFFF # strip off sign bit
if signBit == 1: # negative number
x = tempData
x = x^0xFFF
x = -(x + 1)
else: # positive number
x = tempData
temperature = x/16
return temperature
def conversion_to_twos_complement(self, num2convert):
"""conversion_to_twos_complement, function to change a number
into twos complement 13 bit number"""
if num2convert < 0:
x = abs(num2convert*16)
x = int(x) & 0xFFF
x = x^0x1FFF
x = x+1
else:
x = int(num2convert*16)
x = x & 0xFFF
return x
def read_temperature(self):
"""read_temperature, function to read the temperature data
from register 0x05 of the MCP9808 and convert to decimal format"""
# read data from MCP9808 regisiter
tempData = self.single_word_read(0x05)
# reorder data with MSB first then LSB, put this into single_word_read funct
#tempData = ((tempData & 0x00FF)<<8) + ((tempData & 0xFF00)>>8)
# take off alert flags from temerature data
tempData = tempData & 0x1FFF
temperature = self.twos_complement_conversion(tempData)
return temperature
def set_t_critical(self, temperature):
"""set_t_critical, function to set the Tcritical value kept in
register 0x04"""
temperature = self.conversion_to_twos_complement(temperature)
self.single_word_write(0x04, temperature)
return
def set_t_upper(self, temperature):
"""set_t_upper, function to set the Tupper value kept in
register 0x02"""
temperature = self.conversion_to_twos_complement(temperature)
self.single_word_write(0x02, temperature)
return
def set_t_lower(self, temperature):
"""set_t_lower, function to set the Tlower value kept in
register 0x03"""
temperature = self.conversion_to_twos_complement(temperature)
self.single_word_write(0x03, temperature)
return
def set_resolution(self, res = 0.0625):
"""set_resolution, function to set the sensor resolution to one of
four values, 0.5, 0.25, 0.125 or 0.0625 degrees C. This sets bits 0
& 1 of register 0x08"""
if res == 0.5:
resBits = 0b00
elif res == 0.25:
resBits = 0b01
elif res == 0.125:
resBits = 0b10
else:
resBits = 0b11
self.single_access_write(0x08, resBits)
return
def set_hysteresis(self, hys = 0):
"""set_hysteresis, function to set the sensor hysteresis to one of
four values, 0, 1.5, 3.0, 6.0 degrees C. This sets bits 9 & 10 of
register 0x01"""
if hys == 1.5:
hysBits = 0b01
elif hys == 3.0:
hysBits = 0b10
elif hys == 6.0:
hysBits = 0b11
else:
hysBits = 0b00
configReg = self.single_word_read(0x01)
configReg = configReg & 0x01FF
configReg = configReg | (hysBits<<9)
self.single_word_write(0x01, configReg)
return
def set_shutdown(self, mode=True):
"""set_shutdown, function to set the sensor shutdown mode bit, bit 8
of register 0x01. True = shutdown/low power mode, False = continuous
conversion"""
if mode == False:
modeBit = 0
else:
modeBit = 1
configReg = self.single_word_read(0x01)
configReg = configReg & 0x06FF
configReg = configReg | (modeBit<<8)
self.single_word_write(0x01, configReg)
return
def set_critical_lock(self, critLock = True):
"""set__critical_lock, function to enable the sensor critical lock
function. This sets bit 7 of register 0x01"""
# Note: Once critcal lock is set it can only be cleared by a power
# on reset
if critLock == False:
modeBit = 0
else:
modeBit = 1
configReg = self.single_word_read(0x01)
configReg = configReg & 0x077F
configReg = configReg | (modeBit<<7)
self.single_word_write(0x01, configReg)
return
def set_window_lock(self, winLock = True):
"""set__window_lock, function to enable the sensor window lock
function. This sets bit 6 of register 0x01"""
# Note: Once window lock is set it can only be cleared by a power
# on reset
if winLock == False:
modeBit = 0
else:
modeBit = 1
configReg = self.single_word_read(0x01)
configReg = configReg & 0x07BF
configReg = configReg | (modeBit<<6)
self.single_word_write(0x01, configReg)
return
def set_alerts(self, control = True, select = "all", polarity = "low", mode = "comparator"):
"""set_alerts, function to set the alert properties of the sensor including
control - True/False, select - all/critcal, polarity = high/low, mode = compartor/
interrupt. This sets bits 0-3 of register 0x01"""
if control == False:
cntBit = 0
else:
cntBit = 1
if select == "crtical":
selectBit = 1
else:
selectBit = 0
if polarity == "high":
polBit = 1
else:
polBit = 0
if mode == "interrupt":
modeBit = 1
else:
modeBit = 0
configReg = self.single_word_read(0x01)
configReg = configReg & 0x07F0
configReg = configReg | (cntBit<<3) + (selectBit<<2) + (polBit<<1) + modeBit
self.single_word_write(0x01, configReg)
return
def get_alerts(self):
"""get_alerts, function to get the sensor alert data including Ta vs
Tup, Tlow, Tcrit, bits 13-15 in register 0x05 and alert status from
bit 4 of register 0x01"""
# get Ta vs Tcritical, Tupper, Tlower status bits from reg 0x05
# read data from MCP9808 regisiter
tempData = self.single_word_read(0x05)
# take off alert flags from temerature data
alertData = (tempData & 0xE000)>>13
# get alert status (bit 4) from reg 0x01
alertStatus = self.single_word_read(0x01)
alertStatus = (alertStatus & 0x0010)>>4
return alertData, alertStatus
def clear_interrupt(self):
"""clear_interrupt, function to set the interrupt clear bit
to clear the sensor interrupt, when in interrupt mode. Sets bit 5
of register 0x01"""
configReg = self.single_word_read(0x01)
configReg = configReg | 1<<5
self.single_word_write(0x01, configReg)
return