Skip to content

Commit 8909f42

Browse files
committed
0.3.0 MAX31855_RT
1 parent 9e75649 commit 8909f42

File tree

12 files changed

+516
-112
lines changed

12 files changed

+516
-112
lines changed

libraries/MAX31855_RT/MAX31855.cpp

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//
22
// FILE: MAX31855.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.5
4+
// VERSION: 0.3.0
55
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
66
// DATE: 2014-01-01
77
// URL: https://github.com/RobTillaart/MAX31855_RT
88
//
99
// HISTORY:
10+
// 0.3.0 2021-08-11 VSPI / HSPI support for ESP32
11+
// add setGIOpins - ESP32 specific
12+
// add get/setSPIspeed() - all
1013
// 0.2.5 2021-07-04 fix #14 CS for STM32
1114
// 0.2.4 2020-12-30 arduinoCI, unit test
1215
// 0.2.3 2020-08-30 fix #8 support hardware SPI + example
@@ -30,53 +33,89 @@
3033
#include "MAX31855.h"
3134

3235

33-
MAX31855::MAX31855(const uint8_t cs)
36+
MAX31855::MAX31855(const uint8_t select)
3437
{
35-
_cs = cs;
36-
_hwSPI = true;
37-
38-
_offset = 0;
39-
_SC = K_TC;
40-
_status = STATUS_NOREAD;
41-
_temperature = MAX31855_NO_TEMPERATURE;
42-
_internal = MAX31855_NO_TEMPERATURE;
43-
_rawData = 0;
38+
MAX31855(255, select, 255);
4439
}
4540

4641

47-
MAX31855::MAX31855(const uint8_t sclk, const uint8_t cs, const uint8_t miso)
42+
MAX31855::MAX31855(const uint8_t clock, const uint8_t select, const uint8_t miso)
4843
{
49-
_sclk = sclk;
50-
_cs = cs;
44+
_clock = clock;
45+
_select = select;
5146
_miso = miso;
52-
_hwSPI = false;
53-
54-
_offset = 0;
55-
_SC = K_TC;
56-
_status = STATUS_NOREAD;
57-
_temperature = MAX31855_NO_TEMPERATURE;
58-
_internal = MAX31855_NO_TEMPERATURE;
59-
_rawData = 0;
47+
_hwSPI = (clock == 255);
48+
49+
_lastTimeRead = 0;
50+
_offset = 0;
51+
_SeebeckC = K_TC;
52+
_status = STATUS_NOREAD;
53+
_temperature = MAX31855_NO_TEMPERATURE;
54+
_internal = MAX31855_NO_TEMPERATURE;
55+
_rawData = 0;
6056
}
6157

6258

6359
void MAX31855::begin()
6460
{
65-
pinMode(_cs, OUTPUT);
66-
digitalWrite(_cs, HIGH);
61+
pinMode(_select, OUTPUT);
62+
digitalWrite(_select, HIGH);
63+
64+
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
65+
6766
if (_hwSPI)
6867
{
69-
SPI.begin();
68+
#if defined(ESP32)
69+
if (_useHSPI) // HSPI
70+
{
71+
mySPI = new SPIClass(HSPI);
72+
mySPI->end();
73+
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
74+
}
75+
else // VSPI
76+
{
77+
mySPI = new SPIClass(VSPI);
78+
mySPI->end();
79+
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
80+
}
81+
#else // generic hardware SPI
82+
mySPI = &SPI;
83+
mySPI->end();
84+
mySPI->begin();
85+
#endif
7086
delay(1);
7187
}
7288
else
7389
{
74-
pinMode(_sclk, OUTPUT);
90+
pinMode(_clock, OUTPUT);
91+
digitalWrite(_clock, LOW);
7592
pinMode(_miso, INPUT);
7693
}
7794
}
7895

7996

97+
void MAX31855::setSPIspeed(uint32_t speed)
98+
{
99+
_SPIspeed = speed;
100+
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
101+
};
102+
103+
104+
#if defined(ESP32)
105+
void MAX31855::setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select)
106+
{
107+
_clock = clock;
108+
_miso = miso;
109+
_select = select;
110+
pinMode(_select, OUTPUT);
111+
digitalWrite(_select, HIGH);
112+
113+
mySPI->end(); // disable SPI
114+
mySPI->begin(clock, miso, mosi, select);
115+
}
116+
#endif
117+
118+
80119
uint8_t MAX31855::read()
81120
{
82121
// return value of _read()
@@ -98,7 +137,7 @@ uint8_t MAX31855::read()
98137
return _status;
99138
}
100139

101-
_lastRead = millis();
140+
_lastTimeRead = millis();
102141

103142
// process status bit 0-2
104143
_status = value & 0x0007;
@@ -115,7 +154,7 @@ uint8_t MAX31855::read()
115154
// process internal bit 4-15
116155
_internal = (value & 0x07FF) * 0.0625;
117156
// negative flag set ?
118-
if (value & 0x0800)
157+
if (value & 0x0800)
119158
{
120159
_internal = -128 + _internal;
121160
}
@@ -142,32 +181,32 @@ uint8_t MAX31855::read()
142181
uint32_t MAX31855::_read(void)
143182
{
144183
_rawData = 0;
145-
184+
// DATA TRANSFER
146185
if (_hwSPI)
147186
{
148-
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
149-
digitalWrite(_cs, LOW); // must be after SPI.beginTransaction() - see #14 STM32
187+
mySPI->beginTransaction(_spi_settings);
188+
digitalWrite(_select, LOW); // must be after mySPI->beginTransaction() - see #14 STM32
150189
for (uint8_t i = 0; i < 4; i++)
151190
{
152191
_rawData <<= 8;
153-
_rawData += SPI.transfer(0);
192+
_rawData += mySPI->transfer(0);
154193
}
155-
digitalWrite(_cs, HIGH);
156-
SPI.endTransaction();
194+
digitalWrite(_select, HIGH);
195+
mySPI->endTransaction();
157196
}
158-
else
197+
else // Software SPI
159198
{
160-
digitalWrite(_cs, LOW);
199+
digitalWrite(_select, LOW);
161200
for (int8_t i = 31; i >= 0; i--)
162201
{
163202
_rawData <<= 1;
164-
digitalWrite(_sclk, LOW);
203+
digitalWrite(_clock, LOW);
165204
// delayMicroseconds(1); // DUE
166205
if ( digitalRead(_miso) ) _rawData++;
167-
digitalWrite(_sclk, HIGH);
206+
digitalWrite(_clock, HIGH);
168207
// delayMicroseconds(1); // DUE
169208
}
170-
digitalWrite(_cs, HIGH);
209+
digitalWrite(_select, HIGH);
171210
}
172211

173212
return _rawData;
@@ -179,15 +218,16 @@ float MAX31855::getTemperature()
179218
// offset needs to be added after multiplication TCfactor
180219
// not before otherwise offset will be larger / smaller
181220
// default behaviour
182-
if (_SC == K_TC) return _temperature + _offset;
221+
if (_SeebeckC == K_TC) return _temperature + _offset;
183222

184223
// EXPERIMENTAL OTHER THERMOCOUPLES
224+
// to be tested
185225
// in practice this works also for K_TC but is way slower..
186-
// 1: reverse calculate the Voltage measured
226+
// 1: reverse calculate the Voltage measured (is this correct?)
187227
float Vout = K_TC * (_temperature - _internal); // PAGE 8 datasheet
188228

189229
// 2: from Voltage to corrected temperature using the Seebeck Coefficient
190-
float _temp = Vout / _SC + _internal;
230+
float _temp = Vout / _SeebeckC + _internal + _offset;
191231
return _temp;
192232
}
193233

libraries/MAX31855_RT/MAX31855.h

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: MAX31855.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.5
5+
// VERSION: 0.3.0
66
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
77
// DATE: 2014-01-01
88
// URL: https://github.com/RobTillaart/MAX31855_RT
@@ -13,7 +13,7 @@
1313
//
1414
// +---------+
1515
// Vin | o |
16-
// 3Vo | o |
16+
// 3V3 | o |
1717
// GND | o O | Thermocouple
1818
// D0 | o O | Thermocouple
1919
// CS | o |
@@ -25,11 +25,11 @@
2525
#include "SPI.h"
2626

2727

28-
#define MAX31855_VERSION (F("0.2.5"))
28+
#define MAX31855_VERSION (F("0.3.0"))
2929

3030
#define MAX31855_NO_TEMPERATURE -999
3131

32-
// STATE constants returnd by read()
32+
// STATE constants returned by read()
3333
#define STATUS_OK 0x00
3434
#define STATUS_OPEN_CIRCUIT 0x01
3535
#define STATUS_SHORT_TO_GND 0x02
@@ -64,52 +64,76 @@ class MAX31855
6464
{
6565
public:
6666
// HW SPI
67-
MAX31855(uint8_t CS);
67+
MAX31855(uint8_t select);
6868
// SW SPI
69-
MAX31855(uint8_t SCLK, uint8_t CS, uint8_t MISO);
70-
void begin();
69+
MAX31855(uint8_t clock, uint8_t select, uint8_t miso);
70+
71+
void begin();
7172

7273
// returns state - bit field: 0 = STATUS_OK
73-
uint8_t read();
74+
uint8_t read();
7475

75-
float getInternal(void) const { return _internal; }
76-
float getTemperature(void);
76+
float getInternal(void) const { return _internal; }
77+
float getTemperature(void);
7778

78-
uint8_t getStatus(void) const { return _status; };
79-
inline bool openCircuit() { return _status == STATUS_OPEN_CIRCUIT; };
80-
inline bool shortToGND() { return _status == STATUS_SHORT_TO_GND; };
81-
inline bool shortToVCC() { return _status == STATUS_SHORT_TO_VCC; };
82-
inline bool genericError() { return _status == STATUS_ERROR; };
83-
inline bool noRead() { return _status == STATUS_NOREAD; };
84-
inline bool noCommunication() { return _status == STATUS_NO_COMMUNICATION; };
79+
uint8_t getStatus(void) const { return _status; };
80+
inline bool openCircuit() { return _status == STATUS_OPEN_CIRCUIT; };
81+
inline bool shortToGND() { return _status == STATUS_SHORT_TO_GND; };
82+
inline bool shortToVCC() { return _status == STATUS_SHORT_TO_VCC; };
83+
inline bool genericError() { return _status == STATUS_ERROR; };
84+
inline bool noRead() { return _status == STATUS_NOREAD; };
85+
inline bool noCommunication() { return _status == STATUS_NO_COMMUNICATION; };
8586

8687
// use offset to calibrate the TC.
87-
void setOffset(const float t) { _offset = t; };
88-
float getOffset() const { return _offset; };
88+
void setOffset(const float t) { _offset = t; };
89+
float getOffset() const { return _offset; };
8990

90-
// set the above E_TC (etc) Seebeck Coefficients
91+
// set the above E_TC or other Seebeck Coefficients
9192
// one can also set your own optimized values.
92-
void setSeebeckCoefficient(const float SC) { _SC = SC; };
93-
float getSeebeckCoefficient() const { return _SC; };
93+
void setSeebeckCoefficient(const float SC) { _SeebeckC = SC; };
94+
float getSeebeckCoefficient() const { return _SeebeckC; };
95+
96+
uint32_t lastRead() { return _lastTimeRead; };
97+
uint32_t getRawData() { return _rawData;};
98+
99+
// speed in Hz
100+
void setSPIspeed(uint32_t speed);
101+
uint32_t getSPIspeed() { return _SPIspeed; };
94102

95-
uint32_t lastRead() { return _lastRead; };
96-
uint32_t getRawData() { return _rawData;};
103+
// ESP32 specific
104+
#if defined(ESP32)
105+
void selectHSPI() { _useHSPI = true; };
106+
void selectVSPI() { _useHSPI = false; };
107+
bool usesHSPI() { return _useHSPI; };
108+
bool usesVSPI() { return !_useHSPI; };
109+
110+
// to overrule ESP32 default hardware pins
111+
void setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select);
112+
#endif
97113

98114

99115
private:
100116
uint32_t _read();
117+
101118
uint8_t _status;
102119
float _internal;
103120
float _temperature;
104121
float _offset;
105-
float _SC;
106-
uint32_t _lastRead;
122+
float _SeebeckC;
123+
uint32_t _lastTimeRead;
107124
uint32_t _rawData;
108125
bool _hwSPI;
109126

110-
uint8_t _sclk;
127+
uint8_t _clock;
111128
uint8_t _miso;
112-
uint8_t _cs;
129+
uint8_t _select;
130+
131+
uint32_t _SPIspeed = 1000000;
132+
SPIClass * mySPI;
133+
SPISettings _spi_settings;
134+
#if defined(ESP32)
135+
bool _useHSPI = true;
136+
#endif
113137
};
114138

115139

0 commit comments

Comments
 (0)