1
- /*
2
- This file is part of the MachineControl library.
3
- Copyright (c) 2021 Arduino SA.
4
-
5
- This library is free software; you can redistribute it and/or
6
- modify it under the terms of the GNU Lesser General Public
7
- License as published by the Free Software Foundation; either
8
- version 2.1 of the License, or (at your option) any later version.
9
-
10
- This library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- Lesser General Public License for more details.
14
-
15
- You should have received a copy of the GNU Lesser General Public
16
- License along with this library; if not, write to the Free Software
17
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
- */
19
-
20
1
#include " MAX31865.h"
21
2
22
-
23
3
MAX31865Class::MAX31865Class (PinName cs) : _spi(SPI), _cs(cs) {
24
4
}
25
5
@@ -30,11 +10,11 @@ bool MAX31865Class::begin(int wires) {
30
10
31
11
pinMode (_cs, OUTPUT);
32
12
digitalWrite (_cs, HIGH);
13
+
33
14
// sets 2 or 4 wire
34
15
if (wires == THREE_WIRE) {
35
16
writeByte (MAX31856_CONFIG_REG, (readByte (MAX31856_CONFIG_REG) | MAX31856_CONFIG_3_WIRE));
36
17
} else {
37
-
38
18
writeByte (MAX31856_CONFIG_REG, (readByte (MAX31856_CONFIG_REG) & MAX31856_CONFIG_WIRE_MASK));
39
19
}
40
20
@@ -76,23 +56,20 @@ bool MAX31865Class::getLowThresholdFault(uint8_t fault) {
76
56
}
77
57
78
58
bool MAX31865Class::getLowREFINFault (uint8_t fault) {
79
-
80
59
if (fault & MAX31865_FAULT_LOW_REFIN) {
81
60
return true ;
82
61
}
83
62
return false ;
84
63
}
85
64
86
65
bool MAX31865Class::getHighREFINFault (uint8_t fault) {
87
-
88
66
if (fault & MAX31865_FAULT_HIGH_REFIN) {
89
67
return true ;
90
68
}
91
69
return false ;
92
70
}
93
71
94
72
bool MAX31865Class::getLowRTDINFault (uint8_t fault) {
95
-
96
73
if (fault & MAX31865_FAULT_LOW_RTDIN) {
97
74
return true ;
98
75
}
@@ -113,7 +90,6 @@ float MAX31865Class::readTemperature(float RTDnominal, float refResistor) {
113
90
Rt /= 32768 ;
114
91
Rt *= refResistor;
115
92
116
-
117
93
Z1 = -RTD_A;
118
94
Z2 = RTD_A * RTD_A - (4 * RTD_B);
119
95
Z3 = (4 * RTD_B) / RTDnominal;
@@ -146,7 +122,6 @@ float MAX31865Class::readTemperature(float RTDnominal, float refResistor) {
146
122
}
147
123
148
124
uint32_t MAX31865Class::readRTD () {
149
-
150
125
// clear fault
151
126
writeByte (MAX31856_CONFIG_REG, (readByte (MAX31856_CONFIG_REG) & MAX31856_CONFIG_CLEAR_FAULT_CYCLE) | MAX31856_CONFIG_CLEAR_FAULT);
152
127
@@ -161,6 +136,7 @@ uint32_t MAX31865Class::readRTD() {
161
136
// readings bytes
162
137
uint16_t read = (readBytes (MAX31856_RTD_MSB_REG));
163
138
read = read >>1 ;
139
+
164
140
// disable bias
165
141
writeByte (MAX31856_CONFIG_REG, readByte (MAX31856_CONFIG_REG) & (MAX31856_CONFIG_BIAS_MASK));
166
142
@@ -170,6 +146,7 @@ uint32_t MAX31865Class::readRTD() {
170
146
uint8_t MAX31865Class::readByte (uint8_t addr) {
171
147
addr &= 0x7F ;
172
148
uint8_t read = 0 ;
149
+
173
150
digitalWrite (_cs, LOW);
174
151
175
152
_spi.beginTransaction (_spiSettings);
@@ -183,16 +160,16 @@ uint8_t MAX31865Class::readByte(uint8_t addr) {
183
160
}
184
161
185
162
uint16_t MAX31865Class::readBytes (uint8_t addr) {
186
- digitalWrite (_cs, LOW);
187
163
uint16_t read = 0x00 ;
164
+
165
+ digitalWrite (_cs, LOW);
166
+
188
167
_spi.beginTransaction (_spiSettings);
189
168
_spi.transfer (addr);
190
- int i;
191
- for (i = 0 ; i<2 ; i++) {
169
+ for (int i = 0 ; i < 2 ; i++) {
192
170
read = read << 8 ;
193
171
read |= _spi.transfer (0 );
194
172
}
195
-
196
173
_spi.endTransaction ();
197
174
198
175
digitalWrite (_cs, HIGH);
@@ -203,11 +180,11 @@ uint16_t MAX31865Class::readBytes(uint8_t addr) {
203
180
void MAX31865Class::writeByte (uint8_t addr, uint8_t data) {
204
181
addr |= 0x80 ; // make sure top bit is set
205
182
uint8_t buffer[2 ] = {addr, data};
183
+
206
184
digitalWrite (_cs, LOW);
207
185
208
186
_spi.beginTransaction (_spiSettings);
209
187
_spi.transfer (buffer,2 );
210
-
211
188
_spi.endTransaction ();
212
189
213
190
digitalWrite (_cs, HIGH);
0 commit comments