/
Temp.pde
187 lines (155 loc) · 5.05 KB
/
Temp.pde
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
/*
Copyright (C) 2009, 2010 Matt Reba, Jeremiah Dillingham
This file is part of BrewTroller.
BrewTroller 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.
BrewTroller 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 BrewTroller. If not, see <http://www.gnu.org/licenses/>.
BrewTroller - Open Source Brewing Computer
Software Lead: Matt Reba (matt_AT_brewtroller_DOT_com)
Hardware Lead: Jeremiah Dillingham (jeremiah_AT_brewtroller_DOT_com)
Documentation, Forums and more information available at http://www.brewtroller.com
*/
#include "Config.h"
#include "Enum.h"
#include "HWProfile.h"
#ifdef TS_ONEWIRE
unsigned long convStart;
#ifdef TS_ONEWIRE_GPIO
#include <OneWire.h>
OneWire ds(TEMP_PIN);
#endif
#ifdef TS_ONEWIRE_I2C
#include <DS2482.h>
DS2482 ds(DS2482_ADDR);
#endif
//One Wire Bus on
void tempInit() {
#ifdef TS_ONEWIRE_I2C
ds.configure(DS2482_CONFIG_APU | DS2482_CONFIG_SPU);
#endif
ds.reset();
ds.skip();
ds.write(0x4E, TS_ONEWIRE_PPWR); //Write to scratchpad
ds.write(0x4B, TS_ONEWIRE_PPWR); //Default value of TH reg (user byte 1)
ds.write(0x46, TS_ONEWIRE_PPWR); //Default value of TL reg (user byte 2)
#if TS_ONEWIRE_RES == 12
ds.write(0x7F, TS_ONEWIRE_PPWR); //Config Reg (12-bit)
#elif TS_ONEWIRE_RES == 11
ds.write(0x5F, TS_ONEWIRE_PPWR); //Config Reg (11-bit)
#elif TS_ONEWIRE_RES == 10
ds.write(0x3F, TS_ONEWIRE_PPWR); //Config Reg (10-bit)
#else //Default to 9-bit
ds.write(0x1F, TS_ONEWIRE_PPWR); //Config Reg (9-bit)
#endif
ds.reset();
ds.skip();
ds.write(0x48, TS_ONEWIRE_PPWR); //Copy scratchpad to EEPROM
for (byte i = 0; i < NUM_ZONES; i++) temp[i] = BAD_TEMP;
convertAll();
}
#if TS_ONEWIRE_RES == 12
#define CONV_DELAY 750
#elif TS_ONEWIRE_RES == 11
#define CONV_DELAY 375
#elif TS_ONEWIRE_RES == 10
#define CONV_DELAY 188
#else //Default to 9-bit
#define CONV_DELAY 94
#endif
void convertAll() {
ds.reset();
ds.skip();
ds.write(0x44, TS_ONEWIRE_PPWR); //Start conversion
convStart = millis();
}
void updateTemps() {
unsigned long now = millis();
if (now < convStart) convStart = 0; //Timer overflow has occurred
if (tsReady() || now - convStart >= CONV_DELAY) {
#ifdef DEBUG_TEMP_CONV_T
convStart = now - convStart;
logStart_P(LOGDEBUG);
logField_P(PSTR("TEMP_CONV_T"));
logFieldI(convStart);
logEnd();
#endif
for (byte i = 0; i < NUM_ZONES; i++) temp[i] = read_temp(tSensor[i]);
convertAll();
}
}
boolean tsReady() {
#if TS_ONEWIRE_PPWR == 0 //Poll if parasite power is disabled
if (ds.read() == 0xFF) return 1;
#endif
return 0;
}
boolean validAddr(byte* addr) {
for (byte i = 0; i < 8; i++) if (addr[i]) return 1;
return 0;
}
//This function search for an address that is not currently assigned!
void getDSAddr(byte addrRet[8]){
//Leaving stub for external functions (serial and setup) that use this function
byte scanAddr[8];
ds.reset_search();
byte limit = 0;
//Scan at most 20 sensors (In case the One Wire Search loop issue occurs)
while (limit <= 20) {
if (!ds.search(scanAddr)) {
//No Sensor found, Return
ds.reset_search();
return;
}
boolean found = 0;
for (byte i = 0; i < NUM_ZONES; i++) {
boolean match = 1;
for (byte j = 0; j < 8; j++) {
//Try to confirm a match by checking every byte of the scanned address with those of each sensor.
if (scanAddr[j] != tSensor[i][j]) {
match = 0;
break;
}
}
if (match) {
found = 1;
break;
}
}
if (!found) {
for (byte k = 0; k < 8; k++) addrRet[k] = scanAddr[k];
return;
}
limit++;
}
}
//Returns Int representing hundreths of degree
int read_temp(byte* addr) {
long tempOut;
byte data[9];
if (!validAddr(addr)) return BAD_TEMP;
ds.reset();
ds.select(addr);
ds.write(0xBE, TS_ONEWIRE_PPWR); //Read Scratchpad
for (byte i = 0; i < 9; i++) data[i] = ds.read();
if (ds.crc8( data, 8) != data[8]) return BAD_TEMP;
tempOut = (data[1] << 8) + data[0];
if ( addr[0] == 0x10) tempOut = tempOut * 50; //9-bit DS18S20
else tempOut = tempOut * 25 / 4; //12-bit DS18B20, etc.
#ifdef USEMETRIC
return int(tempOut);
#else
return int((tempOut * 9 / 5) + 3200);
#endif
}
#else
void tempInit() {}
void updateTemps() {}
void getDSAddr(byte addrRet[8]){};
#endif