1
- /* **************************************************
2
- This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
3
-
4
- Designed specifically to work with the Adafruit Thermocouple Sensor
5
- ----> https://www.adafruit.com/products/269
6
-
7
- These displays use SPI to communicate, 3 pins are required to
8
- interface
9
- Adafruit invests time and resources providing this open source code,
10
- please support Adafruit and open-source hardware by purchasing
11
- products from Adafruit!
12
-
13
- Written by Limor Fried/Ladyada for Adafruit Industries.
14
- BSD license, all text above must be included in any redistribution
15
- ****************************************************/
1
+ /* !
2
+ * @file Adafruit_MAX31855.cpp
3
+ *
4
+ * @mainpage Adafruit MAX31855 Thermocouple Breakout Driver
5
+ *
6
+ * @section intro_sec Introduction
7
+ *
8
+ * This is the documentation for Adafruit's MAX31855 thermocouple breakout
9
+ * driver for the Arduino platform. It is designed specifically to work with
10
+ * the Adafruit MAX31855 breakout: https://www.adafruit.com/products/269
11
+ *
12
+ * This breakout uses SPI to communicate, 3 pins are required
13
+ * to interface with the breakout.
14
+ *
15
+ * Adafruit invests time and resources providing this open source code,
16
+ * please support Adafruit and open-source hardware by purchasing
17
+ * products from Adafruit!
18
+ *
19
+ * @section dependencies Dependencies
20
+ *
21
+ * This library depends on <a
22
+ * href="https://github.com/adafruit/Adafruit_BusIO"> Adafruit_BusIO</a> being
23
+ * present on your system. Please make sure you have installed the latest
24
+ * version before using this library.
25
+ *
26
+ * @section author Author
27
+ *
28
+ * Written by Limor Fried/Ladyada for Adafruit Industries.
29
+ *
30
+ * @section license License
31
+ *
32
+ * BSD license, all text above must be included in any redistribution.
33
+ *
34
+ */
16
35
17
36
#include " Adafruit_MAX31855.h"
18
37
#ifdef __AVR
19
- #include < avr/pgmspace.h>
38
+ #include < avr/pgmspace.h>
20
39
#elif defined(ESP8266)
21
- #include < pgmspace.h>
40
+ #include < pgmspace.h>
22
41
#endif
23
42
24
43
#include < stdlib.h>
25
44
45
+ /* *************************************************************************/
46
+ /* !
47
+ @brief Instantiates a new Adafruit_MAX31855 class using software SPI.
26
48
49
+ @param _sclk The pin to use for SPI Serial Clock.
50
+ @param _cs The pin to use for SPI Chip Select.
51
+ @param _miso The pin to use for SPI Master In Slave Out.
52
+ */
53
+ /* *************************************************************************/
27
54
Adafruit_MAX31855::Adafruit_MAX31855 (int8_t _sclk, int8_t _cs, int8_t _miso) {
28
55
spi_dev = Adafruit_SPIDevice (_cs, _sclk, _miso, -1 , 4000000 );
29
56
30
57
initialized = false ;
31
58
}
32
59
60
+ /* *************************************************************************/
61
+ /* !
62
+ @brief Instantiates a new Adafruit_MAX31855 class using hardware SPI.
63
+
64
+ @param _cs The pin to use for SPI Chip Select.
65
+ */
66
+ /* *************************************************************************/
33
67
Adafruit_MAX31855::Adafruit_MAX31855 (int8_t _cs) {
34
68
spi_dev = Adafruit_SPIDevice (_cs, 4000000 );
35
69
36
70
initialized = false ;
37
71
}
38
72
39
- void Adafruit_MAX31855::begin (void ) {
40
- initialized = spi_dev.begin ();
41
- }
73
+ /* *************************************************************************/
74
+ /* !
75
+ @brief Setup the HW
76
+
77
+ @return True if the device was successfully initialized, otherwise false.
78
+ */
79
+ /* *************************************************************************/
80
+ void Adafruit_MAX31855::begin (void ) { initialized = spi_dev.begin (); }
81
+
82
+ /* *************************************************************************/
83
+ /* !
84
+ @brief Read the internal temperature.
42
85
86
+ @return The internal temperature in degrees Celsius.
87
+ */
88
+ /* *************************************************************************/
43
89
double Adafruit_MAX31855::readInternal (void ) {
44
90
uint32_t v;
45
91
@@ -57,17 +103,24 @@ double Adafruit_MAX31855::readInternal(void) {
57
103
internal = tmp;
58
104
}
59
105
internal *= 0.0625 ; // LSB = 0.0625 degrees
60
- // Serial.print("\tInternal Temp: "); Serial.println(internal);
106
+ // Serial.print("\tInternal Temp: "); Serial.println(internal);
61
107
return internal;
62
108
}
63
109
110
+ /* *************************************************************************/
111
+ /* !
112
+ @brief Read the thermocouple temperature.
113
+
114
+ @return The thermocouple temperature in degrees Celsius.
115
+ */
116
+ /* *************************************************************************/
64
117
double Adafruit_MAX31855::readCelsius (void ) {
65
118
66
119
int32_t v;
67
120
68
121
v = spiread32 ();
69
122
70
- // Serial.print("0x"); Serial.println(v, HEX);
123
+ // Serial.print("0x"); Serial.println(v, HEX);
71
124
72
125
/*
73
126
float internal = (v >> 4) & 0x7FF;
@@ -85,12 +138,11 @@ double Adafruit_MAX31855::readCelsius(void) {
85
138
if (v & 0x80000000 ) {
86
139
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
87
140
v = 0xFFFFC000 | ((v >> 18 ) & 0x00003FFFF );
88
- }
89
- else {
141
+ } else {
90
142
// Positive value, just drop the lower 18 bits.
91
143
v >>= 18 ;
92
144
}
93
- // Serial.println(v, HEX);
145
+ // Serial.println(v, HEX);
94
146
95
147
double centigrade = v;
96
148
@@ -99,25 +151,44 @@ double Adafruit_MAX31855::readCelsius(void) {
99
151
return centigrade;
100
152
}
101
153
102
- uint8_t Adafruit_MAX31855::readError () {
103
- return spiread32 () & 0x7 ;
104
- }
154
+ /* *************************************************************************/
155
+ /* !
156
+ @brief Read the error state.
157
+
158
+ @return The error state.
159
+ */
160
+ /* *************************************************************************/
161
+ uint8_t Adafruit_MAX31855::readError () { return spiread32 () & 0x7 ; }
105
162
106
- double Adafruit_MAX31855::readFarenheit (void ) {
163
+ /* *************************************************************************/
164
+ /* !
165
+ @brief Read the thermocouple temperature.
166
+
167
+ @return The thermocouple temperature in degrees Fahrenheit.
168
+ */
169
+ /* *************************************************************************/
170
+ double Adafruit_MAX31855::readFahrenheit (void ) {
107
171
float f = readCelsius ();
108
172
f *= 9.0 ;
109
173
f /= 5.0 ;
110
174
f += 32 ;
111
175
return f;
112
176
}
113
177
178
+ /* *************************************************************************/
179
+ /* !
180
+ @brief Read 4 bytes (32 bits) from breakout over SPI.
181
+
182
+ @return The raw 32 bit value read.
183
+ */
184
+ /* *************************************************************************/
114
185
uint32_t Adafruit_MAX31855::spiread32 (void ) {
115
186
int i;
116
187
uint32_t d = 0 ;
117
188
uint8_t buf[4 ];
118
189
119
190
// backcompatibility!
120
- if (! initialized) {
191
+ if (!initialized) {
121
192
begin ();
122
193
}
123
194
@@ -128,10 +199,10 @@ uint32_t Adafruit_MAX31855::spiread32(void) {
128
199
d |= buf[1 ];
129
200
d <<= 8 ;
130
201
d |= buf[2 ];
131
- d <<=8 ;
202
+ d <<= 8 ;
132
203
d |= buf[3 ];
133
204
134
- // Serial.println(d, HEX);
205
+ // Serial.println(d, HEX);
135
206
136
207
return d;
137
208
}
0 commit comments