forked from adafruit/Adafruit-MAX31855-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_MAX31855.cpp
177 lines (139 loc) · 3.57 KB
/
Adafruit_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
/***************************************************
This is a library for the Adafruit Thermocouple Sensor w/MAX31855K
Designed specifically to work with the Adafruit Thermocouple Sensor
----> https://www.adafruit.com/products/269
These displays use SPI to communicate, 3 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
****************************************************/
#include "Adafruit_MAX31855.h"
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <stdlib.h>
#include <SPI.h>
Adafruit_MAX31855::Adafruit_MAX31855(int8_t _sclk, int8_t _cs, int8_t _miso) {
sclk = _sclk;
cs = _cs;
miso = _miso;
initialized = false;
}
Adafruit_MAX31855::Adafruit_MAX31855(int8_t _cs) {
cs = _cs;
sclk = miso = -1;
initialized = false;
}
void Adafruit_MAX31855::begin(void) {
//define pin modes
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
if (sclk == -1) {
// hardware SPI
//start and configure hardware SPI
SPI.begin();
} else {
pinMode(sclk, OUTPUT);
pinMode(miso, INPUT);
}
initialized = true;
}
double Adafruit_MAX31855::readInternal(void) {
uint32_t v;
v = spiread32();
// ignore bottom 4 bits - they're just thermocouple data
v >>= 4;
// pull the bottom 11 bits off
float internal = v & 0x7FF;
// check sign bit!
if (v & 0x800) {
// Convert to negative value by extending sign and casting to signed type.
int16_t tmp = 0xF800 | (v & 0x7FF);
internal = tmp;
}
internal *= 0.0625; // LSB = 0.0625 degrees
//Serial.print("\tInternal Temp: "); Serial.println(internal);
return internal;
}
double Adafruit_MAX31855::readCelsius(void) {
int32_t v;
v = spiread32();
//Serial.print("0x"); Serial.println(v, HEX);
/*
float internal = (v >> 4) & 0x7FF;
internal *= 0.0625;
if ((v >> 4) & 0x800)
internal *= -1;
Serial.print("\tInternal Temp: "); Serial.println(internal);
*/
if (v & 0x7) {
// uh oh, a serious problem!
return NAN;
}
if (v & 0x80000000) {
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
v = 0xFFFFC000 | ((v >> 18) & 0x00003FFFF);
}
else {
// Positive value, just drop the lower 18 bits.
v >>= 18;
}
//Serial.println(v, HEX);
double centigrade = v;
// LSB = 0.25 degrees C
centigrade *= 0.25;
return centigrade;
}
uint8_t Adafruit_MAX31855::readError() {
return spiread32() & 0x7;
}
double Adafruit_MAX31855::readFahrenheit(void) {
float f = readCelsius();
f *= 1.8;
f += 32;
return f;
}
uint32_t Adafruit_MAX31855::spiread32(void) {
int i;
uint32_t d = 0;
// backcompatibility!
if (! initialized) {
begin();
}
digitalWrite(cs, LOW);
delay(1);
if(sclk == -1) {
// hardware SPI
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
d = SPI.transfer(0);
d <<= 8;
d |= SPI.transfer(0);
d <<= 8;
d |= SPI.transfer(0);
d <<= 8;
d |= SPI.transfer(0);
SPI.endTransaction();
} else {
// software SPI
digitalWrite(sclk, LOW);
delay(1);
for (i=31; i>=0; i--) {
digitalWrite(sclk, LOW);
delay(1);
d <<= 1;
if (digitalRead(miso)) {
d |= 1;
}
digitalWrite(sclk, HIGH);
delay(1);
}
}
digitalWrite(cs, HIGH);
//Serial.println(d, HEX);
return d;
}