-
Notifications
You must be signed in to change notification settings - Fork 81
/
Adafruit_MAX31865.cpp
347 lines (302 loc) · 11.2 KB
/
Adafruit_MAX31865.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***************************************************
This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
Designed specifically to work with the Adafruit RTD Sensor
----> https://www.adafruit.com/products/3328
This sensor uses SPI to communicate, 4 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_MAX31865.h"
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <stdlib.h>
/**************************************************************************/
/*!
@brief Create the interface object using software (bitbang) SPI
@param spi_cs the SPI CS pin to use
@param spi_mosi the SPI MOSI pin to use
@param spi_miso the SPI MISO pin to use
@param spi_clk the SPI clock pin to use
*/
/**************************************************************************/
//
Adafruit_MAX31865::Adafruit_MAX31865(int8_t spi_cs, int8_t spi_mosi,
int8_t spi_miso, int8_t spi_clk)
: spi_dev(spi_cs, spi_clk, spi_miso, spi_mosi, 1000000,
SPI_BITORDER_MSBFIRST, SPI_MODE1) {}
/**************************************************************************/
/*!
@brief Create the interface object using hardware SPI
@param spi_cs the SPI chip select pin to use
@param theSPI the SPI device to use, default is SPI
*/
/**************************************************************************/
Adafruit_MAX31865::Adafruit_MAX31865(int8_t spi_cs, SPIClass *theSPI)
: spi_dev(spi_cs, 1000000, SPI_BITORDER_MSBFIRST, SPI_MODE1, theSPI) {}
/**************************************************************************/
/*!
@brief Initialize the SPI interface and set the number of RTD wires used
@param wires The number of wires in enum format. Can be MAX31865_2WIRE,
MAX31865_3WIRE, or MAX31865_4WIRE
@return True
*/
/**************************************************************************/
bool Adafruit_MAX31865::begin(max31865_numwires_t wires) {
spi_dev.begin();
setWires(wires);
enableBias(false);
autoConvert(false);
setThresholds(0, 0xFFFF);
clearFault();
// Serial.print("config: ");
// Serial.println(readRegister8(MAX31865_CONFIG_REG), HEX);
return true;
}
/**************************************************************************/
/*!
@brief Read the raw 8-bit FAULTSTAT register
@param fault_cycle The fault cycle type to run. Can be MAX31865_FAULT_NONE,
MAX31865_FAULT_AUTO, MAX31865_FAULT_MANUAL_RUN, or
MAX31865_FAULT_MANUAL_FINISH
@return The raw unsigned 8-bit FAULT status register
*/
/**************************************************************************/
uint8_t Adafruit_MAX31865::readFault(max31865_fault_cycle_t fault_cycle) {
if (fault_cycle) {
uint8_t cfg_reg = readRegister8(MAX31865_CONFIG_REG);
cfg_reg &= 0x11; // mask out wire and filter bits
switch (fault_cycle) {
case MAX31865_FAULT_AUTO:
writeRegister8(MAX31865_CONFIG_REG, (cfg_reg | 0b10000100));
delay(1);
break;
case MAX31865_FAULT_MANUAL_RUN:
writeRegister8(MAX31865_CONFIG_REG, (cfg_reg | 0b10001000));
return 0;
case MAX31865_FAULT_MANUAL_FINISH:
writeRegister8(MAX31865_CONFIG_REG, (cfg_reg | 0b10001100));
return 0;
case MAX31865_FAULT_NONE:
default:
break;
}
}
return readRegister8(MAX31865_FAULTSTAT_REG);
}
/**************************************************************************/
/*!
@brief Clear all faults in FAULTSTAT
*/
/**************************************************************************/
void Adafruit_MAX31865::clearFault(void) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
t &= ~0x2C;
t |= MAX31865_CONFIG_FAULTSTAT;
writeRegister8(MAX31865_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@brief Enable the bias voltage on the RTD sensor
@param b If true bias is enabled, else disabled
*/
/**************************************************************************/
void Adafruit_MAX31865::enableBias(bool b) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31865_CONFIG_BIAS; // enable bias
} else {
t &= ~MAX31865_CONFIG_BIAS; // disable bias
}
writeRegister8(MAX31865_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@brief Whether we want to have continuous conversions (50/60 Hz)
@param b If true, auto conversion is enabled
*/
/**************************************************************************/
void Adafruit_MAX31865::autoConvert(bool b) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31865_CONFIG_MODEAUTO; // enable autoconvert
} else {
t &= ~MAX31865_CONFIG_MODEAUTO; // disable autoconvert
}
writeRegister8(MAX31865_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@brief Whether we want filter out 50Hz noise or 60Hz noise
@param b If true, 50Hz noise is filtered, else 60Hz(default)
*/
/**************************************************************************/
void Adafruit_MAX31865::enable50Hz(bool b) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31865_CONFIG_FILT50HZ;
} else {
t &= ~MAX31865_CONFIG_FILT50HZ;
}
writeRegister8(MAX31865_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@brief Write the lower and upper values into the threshold fault
register to values as returned by readRTD()
@param lower raw lower threshold
@param upper raw upper threshold
*/
/**************************************************************************/
void Adafruit_MAX31865::setThresholds(uint16_t lower, uint16_t upper) {
writeRegister8(MAX31865_LFAULTLSB_REG, lower & 0xFF);
writeRegister8(MAX31865_LFAULTMSB_REG, lower >> 8);
writeRegister8(MAX31865_HFAULTLSB_REG, upper & 0xFF);
writeRegister8(MAX31865_HFAULTMSB_REG, upper >> 8);
}
/**************************************************************************/
/*!
@brief Read the raw 16-bit lower threshold value
@return The raw unsigned 16-bit value, NOT temperature!
*/
/**************************************************************************/
uint16_t Adafruit_MAX31865::getLowerThreshold(void) {
return readRegister16(MAX31865_LFAULTMSB_REG);
}
/**************************************************************************/
/*!
@brief Read the raw 16-bit lower threshold value
@return The raw unsigned 16-bit value, NOT temperature!
*/
/**************************************************************************/
uint16_t Adafruit_MAX31865::getUpperThreshold(void) {
return readRegister16(MAX31865_HFAULTMSB_REG);
}
/**************************************************************************/
/*!
@brief How many wires we have in our RTD setup, can be MAX31865_2WIRE,
MAX31865_3WIRE, or MAX31865_4WIRE
@param wires The number of wires in enum format
*/
/**************************************************************************/
void Adafruit_MAX31865::setWires(max31865_numwires_t wires) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (wires == MAX31865_3WIRE) {
t |= MAX31865_CONFIG_3WIRE;
} else {
// 2 or 4 wire
t &= ~MAX31865_CONFIG_3WIRE;
}
writeRegister8(MAX31865_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@brief Read the temperature in C from the RTD through calculation of the
resistance. Uses
http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
technique
@param RTDnominal The 'nominal' resistance of the RTD sensor, usually 100
or 1000
@param refResistor The value of the matching reference resistor, usually
430 or 4300
@returns Temperature in C
*/
/**************************************************************************/
float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) {
return calculateTemperature(readRTD(), RTDnominal, refResistor);
}
/**************************************************************************/
/*!
@brief Calculate the temperature in C from the RTD through calculation of
the resistance. Uses
http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
technique
@param RTDraw The raw 16-bit value from the RTD_REG
@param RTDnominal The 'nominal' resistance of the RTD sensor, usually 100
or 1000
@param refResistor The value of the matching reference resistor, usually
430 or 4300
@returns Temperature in C
*/
/**************************************************************************/
float Adafruit_MAX31865::calculateTemperature(uint16_t RTDraw, float RTDnominal,
float refResistor) {
float Z1, Z2, Z3, Z4, Rt, temp;
Rt = RTDraw;
Rt /= 32768;
Rt *= refResistor;
// Serial.print("\nResistance: "); Serial.println(Rt, 8);
Z1 = -RTD_A;
Z2 = RTD_A * RTD_A - (4 * RTD_B);
Z3 = (4 * RTD_B) / RTDnominal;
Z4 = 2 * RTD_B;
temp = Z2 + (Z3 * Rt);
temp = (sqrt(temp) + Z1) / Z4;
if (temp >= 0)
return temp;
// ugh.
Rt /= RTDnominal;
Rt *= 100; // normalize to 100 ohm
float rpoly = Rt;
temp = -242.02;
temp += 2.2228 * rpoly;
rpoly *= Rt; // square
temp += 2.5859e-3 * rpoly;
rpoly *= Rt; // ^3
temp -= 4.8260e-6 * rpoly;
rpoly *= Rt; // ^4
temp -= 2.8183e-8 * rpoly;
rpoly *= Rt; // ^5
temp += 1.5243e-10 * rpoly;
return temp;
}
/**************************************************************************/
/*!
@brief Read the raw 16-bit value from the RTD_REG in one shot mode
@return The raw unsigned 16-bit value, NOT temperature!
*/
/**************************************************************************/
uint16_t Adafruit_MAX31865::readRTD(void) {
clearFault();
enableBias(true);
delay(10);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
t |= MAX31865_CONFIG_1SHOT;
writeRegister8(MAX31865_CONFIG_REG, t);
delay(65);
uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
enableBias(false); // Disable bias current again to reduce selfheating.
// remove fault
rtd >>= 1;
return rtd;
}
/**********************************************/
uint8_t Adafruit_MAX31865::readRegister8(uint8_t addr) {
uint8_t ret = 0;
readRegisterN(addr, &ret, 1);
return ret;
}
uint16_t Adafruit_MAX31865::readRegister16(uint8_t addr) {
uint8_t buffer[2] = {0, 0};
readRegisterN(addr, buffer, 2);
uint16_t ret = buffer[0];
ret <<= 8;
ret |= buffer[1];
return ret;
}
void Adafruit_MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[],
uint8_t n) {
addr &= 0x7F; // make sure top bit is not set
spi_dev.write_then_read(&addr, 1, buffer, n);
}
void Adafruit_MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
addr |= 0x80; // make sure top bit is set
uint8_t buffer[2] = {addr, data};
spi_dev.write(buffer, 2);
}