-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththermometer.py
More file actions
177 lines (141 loc) · 4.58 KB
/
thermometer.py
File metadata and controls
177 lines (141 loc) · 4.58 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
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Ondrej Caletka on 2010-11-13.
Copyright 2010 Ondrej Caletka <ondrej.caletka@gmail.com>
Based on the i2c demo from Peter Huewe.
This file is part of pyBusPirate.
pyBusPirate 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.
pyBusPirate 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 pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from pyBusPirateLite.I2Chigh import *
class DS_Config:
_9BIT = 0x00
_10BIT = 0x04
_11BIT = 0x08
_12BIT = 0x0C
ONESHOT = 0x01
POL = 0x02
NVB = 0x10
TLF = 0x20
TLH = 0x40
DONE = 0x80
class DS1631:
""" DS1631 thermometer intraface. Uses I2Chigh class for communication with device. """
def __init__(self, i2c, address=0x48):
self.i2c = i2c;
self.address = address;
self.soft_por();
def get_temp_reg(self, reg):
temp = self.i2c.get_word(self.address, reg);
if (temp & 0x0f):
raise ValueError, 'Invalid value received!';
if temp < 32768:
return temp/256.0;
else:
return (temp-65536)/256.0;
def set_temp_reg(self, reg, value):
if value >= 0:
rawval = int(round(256*value)) & 0xfff0;
else:
rawval = (65536 + int(round(256*value))) & 0xfff0;
self.i2c.set_word(self.address, reg, rawval);
def get_temp(self):
return self.get_temp_reg(0xAA);
def get_tl(self):
return self.get_temp_reg(0xA1);
def set_tl(self, value):
self.set_temp_reg(0xA1, value);
def get_th(self):
return self.get_temp_reg(0xA2);
def set_th(self, value):
self.set_temp_reg(0xA2, value);
def get_config(self):
return self.i2c.get_byte(self.address, 0xAC);
def set_config(self, config):
self.i2c.set_byte(self.address, 0xAC, config);
def start_convert(self):
self.i2c.command(self.address, 0x51);
def stop_convert(self):
self.i2c.command(self.address, 0x22);
def soft_por(self):
self.i2c.command(self.address, 0x54);
def is_done(self):
return self.get_config() & DS_Config.DONE;
if __name__ == '__main__':
try:
# Serial timeout five seconds for debugging mistakes in I2C class
i2c = I2Chigh("/dev/ttyUSB0", 115200, 5)
except Exception, e:
print "Error",e
sys.exit()
print "Entering binmode: ",
if i2c.BBmode():
print "OK."
else:
print "failed."
sys.exit()
print "Entering raw I2C mode: ",
if i2c.enter_I2C():
print "OK."
else:
print "failed."
sys.exit()
print "Configuring I2C."
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS):
print "Failed to set I2C peripherals."
sys.exit()
if not i2c.set_speed(I2CSpeed._100KHZ):
print "Failed to set I2C Speed."
sys.exit()
i2c.timeout(0.2)
print "Starting..."
try:
ds1 = DS1631(i2c, 0x48);
ds1.set_config(DS_Config._12BIT | DS_Config.ONESHOT | DS_Config.POL);
except Exception, e:
print "Thermometer 1: ",e;
ds1 = None;
try:
ds2 = DS1631(i2c, 0x49);
ds2.set_config(DS_Config._12BIT | DS_Config.ONESHOT | DS_Config.POL);
except Exception, e:
print "Thermometer 2: ",e;
ds2 = None;
print "Thermometers initialized"
temp1=0;
temp2=0;
for i in range(16):
print "Round %00d" % (i+1),;
if (ds1):
ds1.start_convert();
if (ds2):
ds2.start_convert();
print "started..."
while not ((not ds1 or ds1.is_done()) and (not ds2 or ds2.is_done())):
print "In Progress..."
i2c.timeout(0.2)
if (ds1):
tmp=ds1.get_temp();
print "T1: %f " %(tmp);
temp1 += tmp/16.0;
if (ds2):
tmp=ds2.get_temp();
print "T2: %f" %(tmp);
temp2 += tmp/16.0;
print "Final T1: %f T2: %f" %(temp1, temp2);
print "Reset Bus Pirate to user terminal: "
if i2c.resetBP():
print "OK."
else:
print "failed."
sys.exit()