|
19 | 19 |
|
20 | 20 | #include "MAX31855.h"
|
21 | 21 |
|
| 22 | +const double MAX31855Class::Jm210_760[] ; |
| 23 | +const double MAX31855Class::J760_1200[] ; |
| 24 | +const double MAX31855Class::Km270_0[] ; |
| 25 | +const double MAX31855Class::K0_1372[] ; |
| 26 | + |
| 27 | +const double MAX31855Class::InvJ_neg[] ; |
| 28 | +const double MAX31855Class::InvJ0_760[] ; |
| 29 | +const double MAX31855Class::InvJ760_1200[] ; |
| 30 | + |
| 31 | +const double MAX31855Class::InvK_neg[] ; |
| 32 | +const double MAX31855Class::InvK0_500[] ; |
| 33 | +const double MAX31855Class::InvK500_1372[] ; |
| 34 | + |
| 35 | +const MAX31855Class::coefftable MAX31855Class::CoeffJ[]; |
| 36 | +const MAX31855Class::coefftable MAX31855Class::CoeffK[]; |
| 37 | + |
| 38 | +const MAX31855Class::coefftable MAX31855Class::InvCoeffJ[]; |
| 39 | +const MAX31855Class::coefftable MAX31855Class::InvCoeffK[]; |
| 40 | + |
22 | 41 | MAX31855Class::MAX31855Class(int cs, SPIClass& spi) :
|
23 | 42 | _cs(cs),
|
24 | 43 | _spi(&spi),
|
@@ -72,34 +91,128 @@ uint32_t MAX31855Class::readSensor()
|
72 | 91 | return read;
|
73 | 92 | }
|
74 | 93 |
|
| 94 | +double MAX31855Class::polynomial(double value, int tableEntries, coefftable const (*table) ) |
| 95 | +{ |
| 96 | + double output = 0; |
| 97 | + double valuePower = 1; |
| 98 | + for (int i=0;i<tableEntries;i++) { |
| 99 | + if (value < table[i].max) { |
| 100 | + if (table[i].size ==0) { |
| 101 | + return NAN; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + output = 0; |
| 106 | + for (int j = 0; j<table[i].size;j++) { |
| 107 | + output += valuePower*table[i].coeffs[j]; |
| 108 | + valuePower *=value; |
| 109 | + } |
| 110 | + return output; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return NAN; |
| 115 | +} |
| 116 | + |
| 117 | +double MAX31855Class::coldTempTomv(int type, double temp) { |
| 118 | + coefftable const (*table); |
| 119 | + int tableEntries; |
| 120 | + double voltage; |
| 121 | + |
| 122 | + switch (type) { |
| 123 | + case PROBE_J: |
| 124 | + table = CoeffJ; |
| 125 | + tableEntries = sizeof(CoeffJ)/sizeof(coefftable); |
| 126 | + break; |
| 127 | + case PROBE_K: |
| 128 | + table = CoeffK; |
| 129 | + tableEntries = sizeof(CoeffJ)/sizeof(coefftable); |
| 130 | + break; |
| 131 | + } |
| 132 | + voltage = polynomial(temp, tableEntries, table); |
| 133 | + // special case... for K probes in temperature range 0-1372 we need |
| 134 | + // to add an extra term |
| 135 | + if (type==PROBE_K && temp>0) { |
| 136 | + voltage += 0.118597600000E+00 * exp( -0.118343200000E-03 * pow(temp-0.126968600000E+03, 2)); |
| 137 | + } |
| 138 | + return voltage; |
| 139 | +} |
| 140 | + |
| 141 | +double MAX31855Class::mvtoTemp(int type, double voltage) { |
| 142 | + coefftable const (*table); |
| 143 | + int tableEntries; |
| 144 | + |
| 145 | + switch (type) { |
| 146 | + case PROBE_J: |
| 147 | + table = InvCoeffJ; |
| 148 | + tableEntries = sizeof(InvCoeffJ)/sizeof(coefftable); |
| 149 | + break; |
| 150 | + case PROBE_K: |
| 151 | + table = InvCoeffK; |
| 152 | + tableEntries = sizeof(InvCoeffJ)/sizeof(coefftable); |
| 153 | + break; |
| 154 | + } |
| 155 | + return polynomial(voltage, tableEntries, table); |
| 156 | +} |
| 157 | + |
75 | 158 | float MAX31855Class::readTemperature(int type)
|
76 | 159 | {
|
77 | 160 | uint32_t rawword;
|
78 |
| - float celsius; |
| 161 | + int32_t measuredTempInt; |
| 162 | + int32_t measuredColdInt; |
| 163 | + double measuredTemp; |
| 164 | + double measuredCold; |
| 165 | + double measuredVolt; |
79 | 166 |
|
80 | 167 | rawword = readSensor();
|
81 | 168 |
|
82 | 169 | // Check for reading error
|
83 | 170 | if (rawword & 0x7) {
|
84 | 171 | return NAN;
|
85 | 172 | }
|
86 |
| - // The temperature is stored in the last 14 word's bits |
| 173 | + // The cold junction temperature is stored in the last 14 word's bits |
| 174 | + // whereas the ttermocouple temperature (non linearized) is in the topmost 18 bits |
87 | 175 | // sent by the Thermocouple-to-Digital Converter
|
| 176 | + |
| 177 | + // sign extend thermocouple value |
88 | 178 | if (rawword & 0x80000000) {
|
89 | 179 | // Negative value, drop the lower 18 bits and explicitly extend sign bits.
|
90 |
| - rawword = 0xFFFFC000 | ((rawword >> 18) & 0x00003FFFF); |
| 180 | + measuredTempInt = 0xFFFC0000 | ((rawword >> 18) & 0x00003FFFF); |
91 | 181 | } else {
|
92 | 182 | // Positive value, just drop the lower 18 bits.
|
93 |
| - rawword >>= 18; |
| 183 | + measuredTempInt = rawword>>18; |
94 | 184 | }
|
95 | 185 |
|
96 |
| - // multiply for the LSB value |
97 |
| - celsius = rawword * 0.25f; |
98 |
| - if (type == PROBE_J) { |
99 |
| - // conversion factor from K type to J type |
100 |
| - celsius = celsius * 4/5; |
| 186 | + // convert it to degrees |
| 187 | + measuredTemp = measuredTempInt * 0.25f; |
| 188 | + |
| 189 | + // sign extend cold junction temperature |
| 190 | + measuredColdInt = (rawword>>4)&0xfff; |
| 191 | + if (measuredColdInt&0x800) { |
| 192 | + // Negative value, sign extend |
| 193 | + measuredColdInt |= 0xfffff000; |
101 | 194 | }
|
102 |
| - return celsius; |
| 195 | + |
| 196 | + // convert it to degrees |
| 197 | + measuredCold = measuredColdInt/16.0f; |
| 198 | + |
| 199 | + // now the tricky part... since MAX31855K is considering a linear response |
| 200 | + // and is trimemd for K thermocouples, we have to convert the reading back |
| 201 | + // to mV and then use NIST polynomial approximation to determine temperature |
| 202 | + // we know that reading from chip is calculated as: |
| 203 | + // temp = chip_temperature + thermocouple_voltage/0.041276f |
| 204 | + // |
| 205 | + // convert temperature to mV is accomplished converting the chip temperature |
| 206 | + // to mV using NIST polynomial and then by adding the measured voltage |
| 207 | + // calculated inverting the function above |
| 208 | + // this way we calculate the voltage we would have measured if cold junction |
| 209 | + // was at 0 degrees celsius |
| 210 | + |
| 211 | + measuredVolt = coldTempTomv(type, measuredCold)+(measuredTemp-measuredCold) * 0.041276f; |
| 212 | + |
| 213 | + // finally from the cold junction compensated voltage we calculate the temperature |
| 214 | + // using NIST polynomial approximation for the thermocouple type we are using |
| 215 | + return mvtoTemp(type,measuredVolt); |
103 | 216 | }
|
104 | 217 |
|
105 | 218 | float MAX31855Class::readReferenceTemperature(int type)
|
|
0 commit comments