1
- /* **************************************************
1
+ /* **************************************************
2
2
This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
3
3
4
4
Designed specifically to work with the Adafruit Thermocouple Sensor
5
5
----> https://www.adafruit.com/products/269
6
6
7
- These displays use SPI to communicate, 3 pins are required to
7
+ These displays use SPI to communicate, 3 pins are required to
8
8
interface
9
- Adafruit invests time and resources providing this open source code,
10
- please support Adafruit and open-source hardware by purchasing
9
+ Adafruit invests time and resources providing this open source code,
10
+ please support Adafruit and open-source hardware by purchasing
11
11
products from Adafruit!
12
12
13
- Written by Limor Fried/Ladyada for Adafruit Industries.
13
+ Written by Limor Fried/Ladyada for Adafruit Industries.
14
14
BSD license, all text above must be included in any redistribution
15
15
****************************************************/
16
16
22
22
#endif
23
23
24
24
#include < stdlib.h>
25
- #include < SPI.h>
26
25
27
26
28
27
Adafruit_MAX31855::Adafruit_MAX31855 (int8_t _sclk, int8_t _cs, int8_t _miso) {
29
- sclk = _sclk;
30
- cs = _cs;
31
- miso = _miso;
28
+ spi_dev = Adafruit_SPIDevice (_cs, _sclk, _miso, -1 , 4000000 );
32
29
33
30
initialized = false ;
34
31
}
35
32
36
33
Adafruit_MAX31855::Adafruit_MAX31855 (int8_t _cs) {
37
- cs = _cs;
38
- sclk = miso = -1 ;
34
+ spi_dev = Adafruit_SPIDevice (_cs, 4000000 );
39
35
40
36
initialized = false ;
41
37
}
42
38
43
39
void Adafruit_MAX31855::begin (void ) {
44
- // define pin modes
45
- pinMode (cs, OUTPUT);
46
- digitalWrite (cs, HIGH);
47
-
48
- if (sclk == -1 ) {
49
- // hardware SPI
50
- // start and configure hardware SPI
51
- SPI.begin ();
52
- } else {
53
- pinMode (sclk, OUTPUT);
54
- pinMode (miso, INPUT);
55
- }
56
- initialized = true ;
40
+ initialized = spi_dev.begin ();
57
41
}
58
42
59
43
double Adafruit_MAX31855::readInternal (void ) {
@@ -88,14 +72,14 @@ double Adafruit_MAX31855::readCelsius(void) {
88
72
/*
89
73
float internal = (v >> 4) & 0x7FF;
90
74
internal *= 0.0625;
91
- if ((v >> 4) & 0x800)
75
+ if ((v >> 4) & 0x800)
92
76
internal *= -1;
93
77
Serial.print("\tInternal Temp: "); Serial.println(internal);
94
78
*/
95
79
96
80
if (v & 0x7 ) {
97
81
// uh oh, a serious problem!
98
- return NAN;
82
+ return NAN;
99
83
}
100
84
101
85
if (v & 0x80000000 ) {
@@ -107,7 +91,7 @@ double Adafruit_MAX31855::readCelsius(void) {
107
91
v >>= 18 ;
108
92
}
109
93
// Serial.println(v, HEX);
110
-
94
+
111
95
double centigrade = v;
112
96
113
97
// LSB = 0.25 degrees C
@@ -127,52 +111,27 @@ double Adafruit_MAX31855::readFarenheit(void) {
127
111
return f;
128
112
}
129
113
130
- uint32_t Adafruit_MAX31855::spiread32 (void ) {
114
+ uint32_t Adafruit_MAX31855::spiread32 (void ) {
131
115
int i;
132
116
uint32_t d = 0 ;
117
+ uint8_t buf[4 ];
133
118
134
119
// backcompatibility!
135
120
if (! initialized) {
136
121
begin ();
137
122
}
138
123
139
- digitalWrite (cs, LOW);
140
- delay (1 );
141
-
142
- if (sclk == -1 ) {
143
- // hardware SPI
144
-
145
- SPI.beginTransaction (SPISettings (4000000 , MSBFIRST, SPI_MODE0));
146
-
147
- d = SPI.transfer (0 );
148
- d <<= 8 ;
149
- d |= SPI.transfer (0 );
150
- d <<= 8 ;
151
- d |= SPI.transfer (0 );
152
- d <<= 8 ;
153
- d |= SPI.transfer (0 );
154
-
155
- SPI.endTransaction ();
156
- } else {
157
- // software SPI
158
-
159
- digitalWrite (sclk, LOW);
160
- delay (1 );
161
-
162
- for (i=31 ; i>=0 ; i--) {
163
- digitalWrite (sclk, LOW);
164
- delay (1 );
165
- d <<= 1 ;
166
- if (digitalRead (miso)) {
167
- d |= 1 ;
168
- }
169
-
170
- digitalWrite (sclk, HIGH);
171
- delay (1 );
172
- }
173
- }
124
+ spi_dev.read (buf, 4 );
125
+
126
+ d = buf[0 ];
127
+ d <<= 8 ;
128
+ d |= buf[1 ];
129
+ d <<= 8 ;
130
+ d |= buf[2 ];
131
+ d <<=8 ;
132
+ d |= buf[3 ];
174
133
175
- digitalWrite (cs, HIGH);
176
134
// Serial.println(d, HEX);
135
+
177
136
return d;
178
137
}
0 commit comments