-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMAX31855.h
142 lines (112 loc) · 3.88 KB
/
MAX31855.h
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
#pragma once
//
// FILE: MAX31855.h
// AUTHOR: Rob Tillaart
// VERSION: 0.6.1
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
// http://forum.arduino.cc/index.php?topic=208061
// Breakout board
//
// +---------+
// Vin | o |
// 3V3 | o |
// GND | o O | Thermocouple
// D0 | o O | Thermocouple
// CS | o |
// CLK | o |
// +---------+
#include "Arduino.h"
#include "SPI.h"
#define MAX31855_VERSION (F("0.6.1"))
#ifndef __SPI_CLASS__
// MBED must be tested before RP2040
#if defined(ARDUINO_ARCH_MBED)
#define __SPI_CLASS__ SPIClass
#elif defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif
#define MAX31855_NO_TEMPERATURE -999
// STATE constants returned by read()
#define STATUS_OK 0x00
#define STATUS_OPEN_CIRCUIT 0x01
#define STATUS_SHORT_TO_GND 0x02
#define STATUS_SHORT_TO_VCC 0x04
#define STATUS_ERROR 0x07
#define STATUS_NOREAD 0x80
#define STATUS_NO_COMMUNICATION 0x81
// Thermocouples working is based upon Seebeck effect.
// Different TC have a different Seebeck Coefficient (µV/°C)
// See http://www.analog.com/library/analogDialogue/archives/44-10/thermocouple.html
//
// As the MAX31855 is designed for K type sensors, one can calculate
// the factor needed to convert other sensors measurements.
// NOTE: this is only a linear approximation.
//
// Seebeck Coefficients (sensitivity) from the MAX31855 datasheet page 8
// to be used in setSeebeckCoefficient()
//
// TYPE COEFFICIENT
#define E_TC 76.373
#define J_TC 57.953
#define K_TC 41.276
#define N_TC 36.256
#define R_TC 10.506
#define S_TC 9.587
#define T_TC 52.18
class MAX31855
{
public:
// HW SPI
MAX31855(uint8_t select, __SPI_CLASS__ * mySPI);
// SW SPI
MAX31855(uint8_t select, uint8_t miso, uint8_t clock);
void begin();
// returns state - bit field: 0 = STATUS_OK
uint8_t read();
float getInternal(void) const { return _internal; }
float getTemperature(void);
uint8_t getStatus(void) const { return _status; };
inline bool openCircuit() { return _status == STATUS_OPEN_CIRCUIT; };
inline bool shortToGND() { return _status == STATUS_SHORT_TO_GND; };
inline bool shortToVCC() { return _status == STATUS_SHORT_TO_VCC; };
inline bool genericError() { return _status == STATUS_ERROR; };
inline bool noRead() { return _status == STATUS_NOREAD; };
inline bool noCommunication() { return _status == STATUS_NO_COMMUNICATION; };
// use offset to calibrate the TC.
void setOffset(const float t) { _offset = t; };
float getOffset() const { return _offset; };
// set the above E_TC or other Seebeck Coefficients
// one can also set your own optimized values.
void setSeebeckCoefficient(const float SC) { _SeebeckC = SC; };
float getSeebeckCoefficient() const { return _SeebeckC; };
uint32_t lastRead() { return _lastTimeRead; };
uint32_t getRawData() { return _rawData;};
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
void setSWSPIdelay(uint16_t del = 0) { _swSPIdelay = del; };
uint16_t getSWSPIdelay() { return _swSPIdelay; };
private:
uint32_t _read();
uint8_t _status;
float _internal;
float _temperature;
float _offset;
float _SeebeckC;
uint32_t _lastTimeRead;
uint32_t _rawData;
bool _hwSPI;
uint8_t _clock;
uint8_t _miso;
uint8_t _select;
uint16_t _swSPIdelay = 0;
uint32_t _SPIspeed;
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};
// -- END OF FILE --