-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdafruit_MAX31865.cpp
811 lines (673 loc) · 21 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/***************************************************
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
****************************************************/
//#define DEBUG_STM32
//#define DEBUG_STM32_SPI
//#define DEBUG_LPC_SPI
//#define DEBUG_LPC
#if defined(ARDUINO_ARCH_STM32) && defined(DEBUG_STM32)
#define HAS_STM32_DEBUG 1
#endif
#if defined(ARDUINO_ARCH_STM32) && defined(DEBUG_STM32_SPI)
#define HAS_STM32_DEBUG_SPI 1
#endif
#if defined(TARGET_LPC1768) && defined(DEBUG_LPC)
#define HAS_LPC1768_DEBUG 1
#endif
#if defined(TARGET_LPC1768) && defined(DEBUG_LPC_SPI)
#define HAS_LPC1768_DEBUG_SPI 1
#endif
#if HAS_LPC1768_DEBUG || HAS_LPC1768_DEBUG_SPI
#include "../../../../Marlin/src/inc/MarlinConfig.h"
#endif
#include "Adafruit_MAX31865.h"
#if !defined(__AVR__)
#include "../../../../Marlin/src/HAL/shared/Delay.h"
#endif
#ifdef __AVR__
#define AVR_FLAG 1
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <stdlib.h>
#include <SPI.h>
#if defined(TEMP_MODE)
#if TEMP_MODE == 3
#define MAX31865_SPI_MODE SPI_MODE3
#elif TEMP_MODE == 2
#define MAX31865_SPI_MODE SPI_MODE2
#elif TEMP_MODE == 1
#define MAX31865_SPI_MODE SPI_MODE1
#else
#define MAX31865_SPI_MODE SPI_MODE0
#endif
#else
// default to origial settings
#define MAX31865_SPI_MODE SPI_MODE1
#endif
#if defined(TARGET_LPC1768)
static SPISettings max31865_spisettings =
SPISettings(SPI_QUARTER_SPEED, MSBFIRST, MAX31865_SPI_MODE);
#elif defined(ARDUINO_ARCH_STM32)
static SPISettings max31865_spisettings =
SPISettings(SPI_CLOCK_DIV4, MSBFIRST, MAX31865_SPI_MODE);
#else
static SPISettings max31865_spisettings =
SPISettings(500000, MSBFIRST, MAX31865_SPI_MODE);
#endif
/**************************************************************************/
/*!
@brief Create the interface object using software (bitbang) SPI for
PIN values which are larger than 127. If you have PIN values less than
or equal to 127 use the other call for SW 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
@param pin_mapping set to 1 for positive pin values
*/
/**************************************************************************/
Adafruit_MAX31865::Adafruit_MAX31865(uint32_t spi_cs, uint32_t spi_mosi,
uint32_t spi_miso, uint32_t spi_clk,
uint8_t pin_mapping) {
__cs = spi_cs;
__mosi = spi_mosi;
__miso = spi_miso;
__sclk = spi_clk;
__pin_mapping = pin_mapping;
if (__pin_mapping == 0) {
_cs = __cs;
_mosi = __mosi;
_miso = __miso;
_sclk = __sclk;
}
}
/**************************************************************************/
/*!
@brief Create the interface object using hardware SPI for PIN values
which are larger than 127. If you have PIN values less than
or equal to 127 use the other call for HW SPI
@param spi_cs the SPI CS pin to use along with the default SPI device
@param pin_mapping set to 1 for positive pin values
*/
/**************************************************************************/
Adafruit_MAX31865::Adafruit_MAX31865(uint32_t spi_cs, uint8_t pin_mapping) {
__cs = spi_cs;
__sclk = __miso = __mosi = -1UL; //-1UL or 0xFFFFFFFF or 4294967295
__pin_mapping = pin_mapping;
if (__pin_mapping == 0) {
_cs = __cs;
_mosi = -1;
_miso = -1;
_sclk = -1;
}
}
/**************************************************************************/
/*!
@brief Create the interface object using software (bitbang) SPI for PIN
values less than or equal to 127.
@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) {
_cs = spi_cs;
_mosi = spi_mosi;
_miso = spi_miso;
_sclk = spi_clk;
//initalize __pin_mapping variables
__cs = 0;
__mosi = 0;
__miso = 0;
__sclk = 0;
__pin_mapping = 0;
}
/**************************************************************************/
/*!
@brief Create the interface object using hardware SPI for PIN for PIN
values less than or equal to 127.
@param spi_cs the SPI CS pin to use along with the default SPI device
*/
/**************************************************************************/
Adafruit_MAX31865::Adafruit_MAX31865(int8_t spi_cs) {
_cs = spi_cs;
_sclk = _miso = _mosi = -1;
//initialzie __pin_mapping variables
__cs = 0;
__mosi = 0;
__miso = 0;
__sclk = 0;
__pin_mapping = 0;
}
/**************************************************************************/
/*!
@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) {
#if AVR_FLAG
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
if (_sclk != -1) {
// define pin modes
pinMode(_sclk, OUTPUT);
digitalWrite(_sclk, LOW);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
} else {
// start and configure hardware SPI
SPI.begin();
}
for (uint8_t i = 0; i < 16; i++) {
// readRegister8(i);
}
setWires(wires);
enableBias(false);
autoConvert(false);
clearFault();
// Serial.print("config: ");
// Serial.println(readRegister8(MAX31856_CONFIG_REG), HEX);
return true;
#else // AVR_FLAG
if (!__pin_mapping) {
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
}
else {
pinMode(__cs, OUTPUT);
digitalWrite(__cs, HIGH);
}
// define pin modes
if (_sclk != -1 || __sclk != -1UL) {
if (!__pin_mapping) {
pinMode(_sclk, OUTPUT);
digitalWrite(_sclk, LOW);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
}
else {
pinMode(__sclk, OUTPUT);
digitalWrite(__sclk, LOW);
pinMode(__mosi, OUTPUT);
pinMode(__miso, INPUT);
}
}
else {
// start and configure hardware SPI
SPI.begin();
}
for (uint8_t i = 0; i < 16; i++) {
// readRegister8(i);
}
setWires(wires);
enableBias(false);
autoConvert(false);
clearFault();
#if HAS_STM32_DEBUG
//Serial.print("config: ");
//Serial.println(readRegister8(MAX31856_CONFIG_REG), HEX);
#endif
#if HAS_STM32_DEBUG_SPI
if (!__pin_mapping) {
Serial.print("\n\n_cs: ");
Serial.print(_cs);
Serial.print(" _cs: 0x");
Serial.print(_cs, HEX);
uint32_t cs2 = 0;
cs2 = _cs;
Serial.print("\n uint32_t cs2: ");
Serial.print(cs2);
Serial.print(" cs2: 0x");
Serial.print(cs2, HEX);
Serial.print("\n _miso: ");
Serial.print(_miso);
Serial.print(" _miso: 0x");
Serial.print(_miso, HEX);
Serial.print("\n _sclk: ");
Serial.print(_sclk);
Serial.print(" _sclk: 0x");
Serial.print(_sclk, HEX);
Serial.print("\n _mosi: ");
Serial.print(_mosi);
Serial.print(" _mosi: 0x");
Serial.print(_mosi, HEX);
Serial.print("\n\n");
}
else {
Serial.print("\n\n__cs: ");
Serial.print(__cs);
Serial.print(" __cs: 0x");
Serial.print(__cs, HEX);
Serial.print("\n __miso: ");
Serial.print(__miso);
Serial.print(" __miso: 0x");
Serial.print(__miso, HEX);
Serial.print("\n __sclk: ");
Serial.print(__sclk);
Serial.print(" __sclk: 0x");
Serial.print(__sclk, HEX);
Serial.print("\n __mosi: ");
Serial.print(__mosi);
Serial.print(" __mosi: 0x");
Serial.print(__mosi, HEX);
Serial.print("\n __pin_mapping: ");
Serial.print(__pin_mapping);
Serial.print("\n\n");
}
#endif
#if HAS_LPC1768_DEBUG_SPI
// for testing
if (!__pin_mapping) {
SERIAL_ECHOLN();
SERIAL_ECHOLNPAIR("Regular call for _cs: ", _cs ," _miso: ", _miso ," _sclk: ", _sclk," _mosi: ",_mosi);
SERIAL_PRINTF("Regular call for _cs: 0x%X _miso: 0x%X _sclk: 0x%X _mosi: 0x%X ", _cs, _miso, _sclk, _mosi);
SERIAL_ECHOLN();
SERIAL_ECHOLN();
}
else {
SERIAL_ECHOLN();
SERIAL_ECHOLNPAIR("PIN_MAPPING call for __cs: ", __cs ," __miso: ", __miso ," __sclk: ", __sclk," __mosi: ",__mosi);
SERIAL_PRINTF("PIN_MAPPING call for __cs: 0x%X __miso: 0x%X __sclk: 0x%X __mosi: 0x%X ", __cs, __miso, __sclk, __mosi);
SERIAL_ECHOLN();
SERIAL_ECHOLN();
}
#endif
return true;
#endif //end AVR_FLAG
}
/**************************************************************************/
/*!
@brief Read the raw 8-bit FAULTSTAT register
@return The raw unsigned 8-bit FAULT status register
*/
/**************************************************************************/
uint8_t Adafruit_MAX31865::readFault(void) {
return readRegister8(MAX31856_FAULTSTAT_REG);
}
/**************************************************************************/
/*!
@brief Clear all faults in FAULTSTAT
*/
/**************************************************************************/
void Adafruit_MAX31865::clearFault(void) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t &= ~0x2C;
t |= MAX31856_CONFIG_FAULTSTAT;
writeRegister8(MAX31856_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(MAX31856_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_BIAS; // enable bias
} else {
t &= ~MAX31856_CONFIG_BIAS; // disable bias
}
writeRegister8(MAX31856_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(MAX31856_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_MODEAUTO; // enable autoconvert
} else {
t &= ~MAX31856_CONFIG_MODEAUTO; // disable autoconvert
}
writeRegister8(MAX31856_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(MAX31856_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_FILT50HZ;
} else {
t &= ~MAX31856_CONFIG_FILT50HZ;
}
writeRegister8(MAX31856_CONFIG_REG, t);
}
/**************************************************************************/
/*!
@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(MAX31856_CONFIG_REG);
if (wires == MAX31865_3WIRE) {
t |= MAX31856_CONFIG_3WIRE;
} else {
// 2 or 4 wire
t &= ~MAX31856_CONFIG_3WIRE;
}
writeRegister8(MAX31856_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) {
float Z1, Z2, Z3, Z4, Rt, temp;
Rt = readRTD();
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) {
#if AVR_FLAG
clearFault();
enableBias(true);
delay(10);
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
writeRegister8(MAX31856_CONFIG_REG, t);
delay(65);
uint16_t rtd = readRegister16(MAX31856_RTDMSB_REG);
// remove fault
rtd >>= 1;
return rtd;
#else //AVR_FLAG
clearFault();
enableBias(true);
DELAY_US(10000);
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
writeRegister8(MAX31856_CONFIG_REG, t);
DELAY_US(65000);
uint16_t rtd = readRegister16(MAX31856_RTDMSB_REG);
// remove fault
rtd >>= 1;
return rtd;
#endif //end AVR_FLAG
}
/**************************************************************************/
/*!
@brief Read the raw 16-bit value from the RTD_REG in one shot mode and
calucalte the RTD resistance value
@param refResistor The value of the matching reference resistor, usually
430 or 4300
@return The raw unsigned 16-bit RTD resistance value, NOT temperature!
*/
/**************************************************************************/
uint16_t Adafruit_MAX31865::readRTD_Resistance(uint32_t refResistor) {
uint32_t Rt;
uint16_t rtd;
rtd = readRTD();
Rt = rtd;
Rt *= refResistor;
Rt >>= 16;
rtd = 0;
rtd = Rt;
return rtd;
}
/**************************************************************************/
/*!
@brief Read the raw 16-bit value from the RTD_REG in one shot mode
@return The raw unsigned 16-bit RTD value with ERROR bit attached, NOT temperature!
*/
/**************************************************************************/
uint16_t Adafruit_MAX31865::readRTD_with_Fault(void) {
#if AVR_FLAG
clearFault();
enableBias(true);
delay(10);
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
writeRegister8(MAX31856_CONFIG_REG, t);
delay(65);
uint16_t rtd = readRegister16(MAX31856_RTDMSB_REG);
return rtd;
#else //AVR_FLAG
uint16_t rtd = 0;
clearFault();
enableBias(true);
DELAY_US(10000);
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
writeRegister8(MAX31856_CONFIG_REG, t);
DELAY_US(65000);
rtd = readRegister16(MAX31856_RTDMSB_REG);
#if HAS_STM32_DEBUG || HAS_LPC1768_DEBUG
uint16_t rtd_MSB = rtd >> 8;
uint16_t rtd_LSB = rtd & 0x00FF;
#endif
#if HAS_STM32_DEBUG
Serial.println(" ");
Serial.print("RTD MSB : 0x");
Serial.print(rtd_MSB, HEX);
Serial.print(" : ");
Serial.print(rtd_MSB);
Serial.print(" RTD LSB : 0x");
Serial.print(rtd_LSB, HEX);
Serial.print(" : ");
Serial.println(rtd_LSB);
Serial.println(" ");
#endif
#if HAS_LPC1768_DEBUG
SERIAL_ECHOLN();
SERIAL_ECHO("RTD MSB : ");
SERIAL_PRINTF(" 0x%X ", rtd_MSB);
SERIAL_ECHOPAIR(" : ", rtd_MSB);
SERIAL_ECHO(" RTD LSB : ");
SERIAL_PRINTF(" 0x%X ", rtd_LSB);
SERIAL_ECHOLNPAIR(" : ", rtd_LSB," ");
SERIAL_ECHOLN();
#endif
return rtd;
#endif //end AVR_FLAG
}
/**********************************************/
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) {
#if AVR_FLAG
addr &= 0x7F; // make sure top bit is not set
if (_sclk == -1)
SPI.beginTransaction(max31865_spisettings);
else
digitalWrite(_sclk, LOW);
digitalWrite(_cs, LOW);
spixfer(addr); //ga4
// Serial.print("$"); Serial.print(addr, HEX); Serial.print(": ");
while (n--) {
buffer[0] = spixfer(0xFF); //ga4
// Serial.print(" 0x"); Serial.print(buffer[0], HEX);
buffer++;
}
// Serial.println();
if (_sclk == -1)
SPI.endTransaction();
digitalWrite(_cs, HIGH);
#else //AVR_FLAG
addr &= 0x7F; // make sure top bit is not set
if (_sclk == -1 || __sclk == -1UL)
SPI.beginTransaction(max31865_spisettings);
else
if (!__pin_mapping)
digitalWrite(_sclk, LOW);
else
digitalWrite(__sclk, LOW);
if (!__pin_mapping)
digitalWrite(_cs, LOW);
else
digitalWrite(__cs, LOW);
spixfer(addr);
// Serial.print("$"); Serial.print(addr, HEX); Serial.print(": ");
while (n--) {
buffer[0] = spixfer(0xFF);
// Serial.print(" 0x"); Serial.print(buffer[0], HEX);
buffer++;
}
// Serial.println();
if (_sclk == -1 || __sclk == -1UL)
SPI.endTransaction();
if (!__pin_mapping)
digitalWrite(_cs, HIGH);
else
digitalWrite(__cs, HIGH);
#endif //end AVR_FLAG
}
void Adafruit_MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
#if AVR_FLAG
if (_sclk == -1)
SPI.beginTransaction(max31865_spisettings);
else
digitalWrite(_sclk, LOW);
digitalWrite(_cs, LOW);
spixfer(addr | 0x80); // make sure top bit is set
spixfer(data);
// Serial.print("$"); Serial.print(addr, HEX); Serial.print(" = 0x");
// Serial.println(data, HEX);
if (_sclk == -1)
SPI.endTransaction();
digitalWrite(_cs, HIGH);
#else //AVR_FLAG
if (_sclk == -1 || __sclk == -1UL)
SPI.beginTransaction(max31865_spisettings);
else
if (!__pin_mapping)
digitalWrite(_sclk, LOW);
else
digitalWrite(__sclk, LOW);
if (!__pin_mapping)
digitalWrite(_cs, LOW);
else
digitalWrite(__cs, LOW);
spixfer(addr | 0x80); // make sure top bit is set
spixfer(data);
// Serial.print("$"); Serial.print(addr, HEX); Serial.print(" = 0x");
// Serial.println(data, HEX);
if (_sclk == -1 || __sclk == -1UL)
SPI.endTransaction();
if (!__pin_mapping)
digitalWrite(_cs, HIGH);
else
digitalWrite(__cs, HIGH);
#endif //end AVR_FLAG
}
uint8_t Adafruit_MAX31865::spixfer(uint8_t x) {
#if AVR_FLAG
if (_sclk == -1)
return SPI.transfer(x);
// software spi
// Serial.println("Software SPI");
uint8_t reply = 0;
for (int i = 7; i >= 0; i--) {
reply <<= 1;
digitalWrite(_sclk, HIGH);
digitalWrite(_mosi, x & (1 << i));
digitalWrite(_sclk, LOW);
if (digitalRead(_miso))
reply |= 1;
}
return reply;
#else //AVR_FLAG
if (_sclk == -1 || __sclk == -1UL)
return SPI.transfer(x);
// software spi
// Serial.println("Software SPI");
uint8_t reply = 0;
if (!__pin_mapping) {
for (int i = 7; i >= 0; i--) {
reply <<= 1;
digitalWrite(_sclk, HIGH);
digitalWrite(_mosi, x & (1 << i));
digitalWrite(_sclk, LOW);
if (digitalRead(_miso))
reply |= 1;
}
}
else {
for (int i = 7; i >= 0; i--) {
reply <<= 1;
digitalWrite(__sclk, HIGH);
digitalWrite(__mosi, x & (1 << i));
digitalWrite(__sclk, LOW);
if (digitalRead(__miso))
reply |= 1;
}
}
return reply;
#endif //end AVR_FLAG
}