forked from olewolf/arduino-max31865
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAX31865.cpp
539 lines (476 loc) · 17.3 KB
/
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**************************************************************************
* Arduino driver library for the MAX31865.
*
* Copyright (C) 2015 Ole Wolf <[email protected]>
* PTD100 LUT Copyright (c) 2017, drhaney
* Wire the circuit as follows, assuming that level converters have been
* added for the 3.3V signals:
*
* Arduino Uno --> MAX31865
* ------------------------------------
* CS: any available pin --> CS
* MOSI: pin 11 --> SDI (mandatory for hardware SPI)
* MISO: pin 12 --> SDO (mandatory for hardware SPI)
* SCK: pin 13 --> SCLK (mandatory for hardware SPI)
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Modifications Urs Utzinger 2021:
* Rewrote SPI handling
* Added fucntions to set configuration bits and clear faults
* Added function to read only rtd register
* Incorporated LUT temperature conversion from Daniel R. Haney
**************************************************************************/
#include <SPI.h>
#include <MAX31865.h>
#include <pt100rtd.h>
// MAX 31865 SPI Mode
// -------------------
// CPHA 1
// CPOL 0 or 1 (examples in datasheet shown with CPOL 1)
// ------------------
// SPI Mode - CPOL - CPHA
// 0 0 0
// 1* 0 1
// 2 1 0
// 3* 1 1
SPISettings setMAX31865(16000000, MSBFIRST, SPI_MODE3); // SPI_MODE1
/*
* The constructor for the MAX31865_RTD class registers the CS pin and
* configures it as an output.
*
* @param [in] cs_pin Arduino pin selected for the CS signal.
*/
MAX31865_RTD::MAX31865_RTD( ptd_type type, uint8_t cs_pin )
{
/* Set the type of PTD. */
this->type = type;
/* CS pin for the SPI device. */
this->cs_pin = cs_pin;
pinMode( this->cs_pin, OUTPUT );
/* Pull the CS pin high to avoid conflicts on SPI bus. */
digitalWrite( this->cs_pin, HIGH );
}
/*
* Configure the MAX31865. The parameters correspond to Table 2 in the MAX31865
* datasheet. The parameters are combined into a control bit-field that is stored
* internally in the class for later reconfiguration, as are the fault threshold values.
*
* @param [in] v_bias Vbias enabled (@a true) or disabled (@a false).
* @param [in] conversion_mode Conversion mode auto (@a true) or off (@a false).
* @param [in] one_shot 1-shot measurement enabled (@a true) or disabled (@a false).
* @param [in] three_wire 3-wire enabled (@a true) or 2-wire/4-wire (@a false).
* @param [in] fault_detection Fault detection cycle control (see Table 3 in the MAX31865
* datasheet).
* @param [in] fault_clear Fault status auto-clear (@a true) or manual clear (@a false).
* @param [in] filter_50hz 50 Hz filter enabled (@a true) or 60 Hz filter enabled
* (@a false).
* @param [in] low_threshold Low fault threshold.
* @param [in] high_threshold High fault threshold.
*/
/*
* Configuration register
* ----------------------
* read: 0x00h write: 0x80h
*
* [D7, D6, D5, D4, D3, D2, D1, D0]
* D7 VBIAS 1=On 0=Off
* D6 Conversion Mode 1=Auto 0=Normally Off
* D5 1-Shot 1=1-shot (then auto clear)
* D4 3-wire, 1=3-wire, 0=2 or 4 wire rtd sensor
* D3, D2:
* XXXX00XXb write: no Action read: fault detection finished
* 100X010Xb write: fault detection with automatic delay, read: automatic fault detection still running
* 100X100Xb write: run fault detection with manula delay, read: manual cycle 1 still running, waiting for user to write 11
* 100X110Xb write: finish fault detection with manual delay, read: manucal cycle 2 still running
* D1 Fault Status Clear, 1=clear (then auto clear)
* D0 50/60Hz filter, 1=50Hz, 0=60Hz
*
* Data Registers
* --------------
* read MSB: 0x01h
* read LSB: 0x02h
* [D7 D6 D5 D4 D3 D2 D1 D0][D7 D6 D5 D4 D3 D2 D1 D0]
* MSB - - - - - - - - - - - - - LSB Fault (any)
*
* High Fault Threshold
* --------------------
* read MSB: 0x03h write 0x83h
* read LSB: 0x04h write 0x84h
* [MSB, D6, D5, D4, D3, D2, D1, D0][D7, D6, D5, D4, D3, D2, LSB, X]
*
* Low Fault Threshold
* -------------------
* read 0x05h write 0x85h
* read 0x06h write 0x86h
*
* Fault Status
* ------------
* read: 0x07h
*
* [D7, D6, D5, D4, D3, D2, D1, D0]
* D7 High Threshold
* D6 Low Threhsold
* D5 REFIN > 0.85VBIAS
* D4 FORCE OPEN
* D3 FORCE CLOSE
* D2 Undervoltage fault
* D1 dont care
* D0 dont care
*
*/
void MAX31865_RTD::configure_all ( bool v_bias, bool conversion_mode, bool one_shot,
bool three_wire, uint8_t fault_cycle, bool fault_clear,
bool filter_50hz, uint16_t low_threshold,
uint16_t high_threshold )
{
uint8_t control_bits = 0;
/* Assemble the control bit mask. */
control_bits |= ( v_bias ? 0b10000000 : 0 );
control_bits |= ( conversion_mode ? 0b01000000 : 0 );
control_bits |= ( one_shot ? 0b00100000 : 0 );
control_bits |= ( three_wire ? 0b00010000 : 0 );
control_bits |= fault_cycle & 0b00001100;
control_bits |= ( fault_clear ? 0b00000010 : 0 );
control_bits |= ( filter_50hz ? 0b00000001 : 0 );
/* Store the control bits and the fault threshold limits for reconfiguration
purposes. */
this->configuration_control_bits = control_bits;
this->configuration_low_threshold = low_threshold;
this->configuration_high_threshold = high_threshold;
/* Perform an initial "reconfiguration." */
reconfigure_settings();
reconfigure_thresholds();
}
void MAX31865_RTD::configure_thresholds ( uint16_t low_threshold, uint16_t high_threshold )
{
this->configuration_low_threshold = low_threshold;
this->configuration_high_threshold = high_threshold;
/* Perform an initial "reconfiguration." */ reconfigure_settings();
reconfigure_thresholds();
}
void MAX31865_RTD::reconfigure_thresholds( )
{
/* Write the threshold values. */
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x83 );
SPI.transfer( ( this->configuration_high_threshold >> 8 ) & 0x00ff ); //3
SPI.transfer( this->configuration_high_threshold & 0x00ff ); //4
SPI.transfer( ( this->configuration_low_threshold >> 8 ) & 0x00ff ); //4
SPI.transfer( this->configuration_low_threshold & 0x00ff ); //5
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
}
void MAX31865_RTD::configure_control ( bool v_bias, bool conversion_mode, bool one_shot,
bool three_wire, uint8_t fault_cycle, bool fault_clear,
bool filter_50hz)
{
uint8_t control_bits = 0;
/* Assemble the control bit mask. */
control_bits |= ( v_bias ? 0b10000000 : 0 );
control_bits |= ( conversion_mode ? 0b01000000 : 0 );
control_bits |= ( one_shot ? 0b00100000 : 0 );
control_bits |= ( three_wire ? 0b00010000 : 0 );
control_bits |= fault_cycle & 0b00001100;
control_bits |= ( fault_clear ? 0b00000010 : 0 );
control_bits |= ( filter_50hz ? 0b00000001 : 0 );
/* Store the control bits and the fault threshold limits for reconfiguration
purposes. */
this->configuration_control_bits = control_bits;
/* Perform an initial "reconfiguration." */
reconfigure_settings( );
}
void MAX31865_RTD::reconfigure_settings( )
{
/* Write the configuration to the MAX31865. */
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x80 );
SPI.transfer( this->configuration_control_bits );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
}
void MAX31865_RTD::clearFaults(void) {
// Read configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x00 );
this->measured_configuration = SPI.transfer( 0x00 );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
// modify configuration to clear faults
this->measured_configuration &= 0b11010011;
this->measured_configuration |= 0b00000010;
// write configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x80 );
SPI.transfer( this->measured_configuration );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
}
void MAX31865_RTD::enableBias(bool bias) {
// Read configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x00 );
this->measured_configuration = SPI.transfer( 0x00 );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
// modify configuration to clear faults
if (bias) { this->measured_configuration |= 0b10000000; } // enable bias
else { this->measured_configuration &= 0b01111111; } // disable bias
// write configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x80 );
SPI.transfer( this->measured_configuration );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
}
void MAX31865_RTD::oneShot(void) {
// Read configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x00 );
this->measured_configuration = SPI.transfer( 0x00 );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
// modify configuration to set one shot
this->measured_configuration |= 0b00100000;
// write configuration
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x80 );
SPI.transfer( this->measured_configuration );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
}
/*
* Read all settings and measurements from the MAX31865 and store them
* internally in the class.
*
* @return Fault status byte
*
*/
uint8_t MAX31865_RTD::read_all( ) // 9 bytes sent to sensor
{
uint16_t combined_bytes;
/* Start the read operation. */
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
/* Read the MAX31865 registers in the following order:
Configuration
RTD
High Fault Threshold
Low Fault Threshold
Fault Status */
/* Tell the MAX31865 that we want to read, starting at register 0. */
SPI.transfer( 0x00 );
//00h
this->measured_configuration = SPI.transfer( 0x00 );
//01h
combined_bytes = SPI.transfer( 0x00 ) << 8;
combined_bytes |= SPI.transfer( 0x00 );
this->measured_resistance = combined_bytes >> 1;
// 03h
combined_bytes = SPI.transfer( 0x00 ) << 8;
combined_bytes |= SPI.transfer( 0x00 );
this->measured_high_threshold = combined_bytes >> 1;
// 05h
combined_bytes = SPI.transfer( 0x00 ) << 8;
combined_bytes |= SPI.transfer( 0x00 );
this->measured_low_threshold = combined_bytes >> 1;
// 07h
this->measured_status = SPI.transfer( 0x00 );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
return( this->measured_status );
}
uint8_t MAX31865_RTD::read_rtd_fault( ) // 5 bytes sent to sensor
{
uint16_t combined_bytes;
// Read the RTD register
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x01 );
combined_bytes = SPI.transfer( 0x00 ) << 8;
combined_bytes |= SPI.transfer( 0x00 );
this->measured_resistance = combined_bytes >> 1;
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
// Read the Status register
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x07 );
this->measured_status = SPI.transfer( 0x00 );
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
return( this->measured_status );
}
bool MAX31865_RTD::read_rtd( ) // 3 bytes sent to sensor
{
uint16_t combined_bytes;
// Read the RTD register
SPI.beginTransaction(setMAX31865);
digitalWrite( this->cs_pin, LOW );
SPI.transfer( 0x01 );
combined_bytes = SPI.transfer( 0x00 ) << 8;
combined_bytes |= SPI.transfer( 0x00 );
bool fault = bool(combined_bytes & 0b00000001);
this->measured_resistance = combined_bytes >> 1;
digitalWrite( this->cs_pin, HIGH );
SPI.endTransaction();
return( fault );
}
/*
* Apply the Callendar-Van Dusen equation to convert the RTD resistance to temperature:
* For more information on measuring with an RTD, see:
* https://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
*
* for T>=0
* --------
* T(r) = (Z1 + sqrt(Z2 + Z3*r)) / Z4
* with Z1 = -A
* with Z2 = A^2 -4*B
* with Z3 = 4*B/R0
* with Z4 = 2*B
*
* A and B are the RTD coeffients
*
* for T < 0
* ---------
* T(r) = –242.02 2.2228*r + 2.5859e-3*r^2 – 4.8260r^3 – 2.8183e-8*r^4 +1.5243e-10*r^5
*
* This should result in less than 0.01 deg C error
*
* @param [in] resistance The measured RTD resistance.
* @return Temperature in degrees Celcius.
*/
double MAX31865_RTD::temperature( ) const
{
double Rt = double(resistance());
double Z1, Z2, Z3, Z4, temperature, rpoly;
const double rtdNominal = ( this->type == RTD_PT100 ) ? RTD_RESISTANCE_PT100 : RTD_RESISTANCE_PT1000;
Z1 = -RTD_A;
Z2 = RTD_A * RTD_A - (4 * RTD_B);
Z3 = (4 * RTD_B) / rtdNominal;
Z4 = 2 * RTD_B;
temperature = Z2 + (Z3 * Rt);
temperature = (sqrt(temperature) + Z1) / Z4;
if (temperature >= 0) return temperature;
Rt /= rtdNominal;
Rt *= 100; // normalize to 100 Ohms
rpoly = Rt; // linear
temperature = -242.02;
temperature += 2.2228 * rpoly;
rpoly *= Rt; // square
temperature += 2.5859e-3 * rpoly ;
rpoly *= Rt; // ^3
temperature -= 4.8260e-6 * rpoly;
rpoly *= Rt; // ^4
temperature -= 2.8183e-8 * rpoly;
rpoly *= Rt; // ^5
temperature += 1.5243e-10 * rpoly;
return temperature;
}
/**********************************************************************
** Function Name: find_index
**
** Description: binary search
** if match
** return index of a match
** if no match
** return index of the smallest table value > key
**
** usually requires the maximum of log2(1051) probes, == 10,
** when search key is not an exact match.
**
** Note: search must not return index == 0.
** Calling function must exclude boundary cases
** where (ohmsX100 <= table[0]).
**
** Parameters:
** uint16_t ohmsX100
**
** Uses:
** Returns: int index of nearest resistance value
** Creation: 1/26/2017 4:48a Daniel R. Haney
**********************************************************************/
int MAX31865_RTD::find_index(uint16_t ohmsX100)
{
int lower = 0 ;
int upper = PT100_TABLE_MAXIDX ;
int mid = (lower + upper) / 2 ;
do {
uint16_t pt100val = pgm_read_word_near(&Pt100_table[mid]) ;
if (pt100val == ohmsX100) { break; }
else if (pt100val < ohmsX100) { lower = mid + 1 ; }
else { upper = mid ; }
mid = (lower + upper) / 2 ;
} while (lower < upper) ;
return(mid);
}
/**********************************************************************
** Function Name: ohmsX100_to_celsius
**
** Description:
** Look up (unsigned short int)(Pt100 resistance * 100) in table.
** Interpolate temperature for intermediate resistances.
**
** Parameters:
** uint16_t Rrtd = 100 * (Pt100 RTD resistance in ohms)
**
** Uses: Pt100_table
** Returns: float temperature celsius
**
** Creation: 1/26/2017 10:41a Daniel R. Haney
**********************************************************************/
float MAX31865_RTD::ohmsX100_to_celsius (uint16_t ohmsX100)
{
uint16_t R_upper, R_lower ;
int hundredths = 0 ; // STFU flag for avr-gcc
int iTemp = 0 ;
float celsius ;
// clip overflow
if (ohmsX100 <= pgm_read_word_near(&Pt100_table[0])) { return((float) CELSIUS_MIN); } // return min boundary temperature
else if (ohmsX100 >= pgm_read_word_near(&Pt100_table[PT100_TABLE_MAXIDX])) { return((float) CELSIUS_MAX); } // return max boundary temperature
int index = find_index(ohmsX100) ;
// The minimum integral temperature
iTemp = index - 1 + CELSIUS_MIN ;
// fetch floor() and ceiling() resistances since
// key = intermediate value is the most likely case.
// ACHTUNG! (index == 0) is forbidden!
R_lower = pgm_read_word_near(&Pt100_table[index - 1]) ;
R_upper = pgm_read_word_near(&Pt100_table[index]) ;
// if key == table entry, temp is an integer degree
if (ohmsX100 == R_upper) {
iTemp++ ;
hundredths = 0 ;
} else if (ohmsX100 < R_upper) { // an intermediate resistance is the common case
hundredths = ((100 * (ohmsX100 - R_lower)) / (R_upper - R_lower)) ;
} else if (ohmsX100 > R_upper) {
// two unlikely cases are included for disaster recovery
/*NOTREACHED*/ /*...unless list search was dain bramaged */
iTemp++ ;
// risks index+1 out of range
uint16_t Rnext = pgm_read_word_near(&Pt100_table[index + 1]) ;
hundredths = (100 * (ohmsX100 - R_upper)) / (Rnext - R_upper) ;
} else {
/*NOTREACHED*/ /*...except in cases of excessive tweakage at 2:30am */
hundredths = ((100 * (ohmsX100 - R_lower)) / (R_upper - R_lower)) ;
}
celsius = (float)iTemp + (float)hundredths / 100.0 ;
return(celsius );
}