-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMAX31855.cpp
231 lines (197 loc) · 6.48 KB
/
MAX31855.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
/***************************************************************************
Library for the MAX31855* thermocouple amplifier from Maxim Semiconductor
Written by Cory J. Fowler
BSD license, all text above must be included in any redistribution.
***************************************************************************/
#include "MAX31855.h"
#include <SPI.h>
/***************************************************************************
*** Function Name: MAX31855
*** Description: Initializes MAX31855 class and Chip Select pin.
*** Also initializes pins associated with the multiplexer.
***************************************************************************/
MAX31855::MAX31855(int _CS) {
CS = _CS;
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
#if MUX
pinMode(AD0, OUTPUT);
pinMode(AD1, OUTPUT);
pinMode(AD2, OUTPUT);
pinMode(MX_EN, OUTPUT);
digitalWrite(AD0, LOW);
digitalWrite(AD1, LOW);
digitalWrite(AD2, LOW);
digitalWrite(MX_EN, LOW);
#endif
}
/***************************************************************************
*** Function Name: begin
*** Description: Initializes SPI.
***************************************************************************/
void MAX31855::begin(void) {
initRead = 1;
SPI.begin();
}
/***************************************************************************
*** Function Name: intTemp
*** Description: Gets MAX31855 die (cold junction) temperature.
***************************************************************************/
double MAX31855::intTemp(int _SCALE) {
if (initRead | ((millis() - previous) >= TIME)){
previous = millis();
initRead = 0;
ReadSPI();
}
int inTemp;
double _internal;
// Dispose of un-needed bits and preserve sign.
if(data & 0x00008000){
inTemp = ((data >> 4) & 0x00000FFF) | 0xF000;
} else {
inTemp = (data >> 4) & 0x00000FFF;
}
// Compute real world value...
if(_SCALE){
_internal = (inTemp * 0.1125) + 32;
} else {
_internal = inTemp * 0.0625;
}
return _internal;
}
/***************************************************************************
*** Function Name: extTemp
*** Description: Gets thermocouple temperature.
***************************************************************************/
double MAX31855::extTemp(int _SCALE) {
if (initRead | ((millis() - previous) >= TIME)){
previous = millis();
initRead = 0;
ReadSPI();
}
int exTemp;
double _external;
// Dispose of un-needed bits and preserve sign.
if(data & 0x80000000){
exTemp = ((data >> 18) & 0x000003FFF) | 0xC000;
} else {
exTemp = (data >> 18) & 0x000003FFF;
}
// Compute real world value...
if(_SCALE){
_external = (exTemp * 0.45) + 32;
} else {
_external = exTemp* 0.25;
}
return _external;
}
/***************************************************************************
*** Function Name: tempErr
*** Description: Returns MAX31855 error bits.
***************************************************************************/
byte MAX31855::tempErr() {
if (initRead | ((millis() - previous) >= TIME)){
previous = millis();
initRead = 0;
ReadSPI();
}
return (data & 0x00000007);
}
/***************************************************************************
*** Function Name: getTemp
*** Description: Returns external, internal, and error data.
***************************************************************************/
void MAX31855::getTemp(double &_external, double &_internal, byte _SCALE, byte &_error) {
previous = millis();
initRead = 0;
ReadSPI();
int inTemp;
int exTemp;
// Dispose of un-needed bits and preserve sign.
if(data & 0x00008000){
inTemp = ((data >> 4) & 0x00000FFF) | 0xF000;
} else {
inTemp = (data >> 4) & 0x00000FFF;
}
// Dispose of un-needed bits and perserve sign.
if(data & 0x80000000){
exTemp = ((data >> 18) & 0x000003FFF) | 0xC000;
} else {
exTemp = (data >> 18) & 0x000003FFF;
}
#if DEBUG
Serial.println(inTemp);
Serial.println(exTemp);
#endif
// Calculate real world value for farenheit or celsius scales.
switch(_SCALE){
case(3):
_internal = (inTemp * 0.1125) + 273.15;
_external = (exTemp * 0.45) + 273.15;
break;
case(2):
_internal = (inTemp * 0.1125) + 32;
_external = (exTemp * 0.45) + 32;
break;
case(1):
_internal = (inTemp * 0.0625) + 273.15;
_external = (exTemp * 0.25) + 273.15;
break;
default:
_internal = inTemp * 0.0625;
_external = exTemp * 0.25;
break;
}
#if DEBUG
Serial.println(_internal);
Serial.println(_external);
#endif
_error = data & 0x00000007;
}
/***************************************************************************
*** Function Name: setMUX
*** Description: Sets the address and enable bits of an ADG608 multiplexer.
***************************************************************************/
void MAX31855::setMUX(byte _MUX) {
#if MUX
digitalWrite(AD0, (0x01 & _MUX));
digitalWrite(AD1, (0x01 & (_MUX >> 1)));
digitalWrite(AD2, (0x01 & (_MUX >> 2)));
digitalWrite(MX_EN, HIGH);
digitalWrite(CS, LOW);
delayMicroseconds(50);
digitalWrite(CS, HIGH);
delay(100);
#endif
}
/***************************************************************************
*** Function Name: ReadSPI
*** Description: Private Function - Reads data from the MAX31855 using SPI.
***************************************************************************/
void MAX31855::ReadSPI(void) {
#if DEBUG
Serial.print("millis() = ");
Serial.println(millis());
Serial.print("TIME = ");
Serial.println(TIME);
Serial.println("Reading from MAX31855");
Serial.println("=================");
#endif
unsigned long _data = 0x00000000;
digitalWrite(CS, LOW);
delayMicroseconds(1);
for (int i = 0; i < 4; i++)
_data = (_data << 8) + SPI.transfer(0x00);
digitalWrite(CS, HIGH);
#if DEBUG
Serial.print("Raw Data = ");
Serial.println(_data,HEX);
#endif
data = _data;
#if MUX
digitalWrite(MX_EN, LOW);
#endif
}
/***************************************************************************
END FILE
***************************************************************************/