From 62727a3f3d5ac517c0d4cea14df45cba4e8b305f Mon Sep 17 00:00:00 2001 From: Tomas Dobrovolny Date: Wed, 9 Mar 2022 00:04:37 +0100 Subject: [PATCH 1/4] overload temperature function Allow conversion of allready readed RTD value --- Adafruit_MAX31865.cpp | 35 +++++++++++++++++++++++++++-------- Adafruit_MAX31865.h | 1 + 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Adafruit_MAX31865.cpp b/Adafruit_MAX31865.cpp index 32bac94..315adf5 100644 --- a/Adafruit_MAX31865.cpp +++ b/Adafruit_MAX31865.cpp @@ -173,7 +173,27 @@ void Adafruit_MAX31865::setWires(max31865_numwires_t wires) { float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) { float Z1, Z2, Z3, Z4, Rt, temp; - Rt = readRTD(); + return temperature(readRTD(), RTDnominal, refResistor); +} +/**************************************************************************/ +/*! + @brief Calculate the temperature in C from the RTD through calculation of + the resistance. Uses + http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf + technique + @param RTDraw The raw 16-bit value from the RTD_REG + @param RTDnominal The 'nominal' resistance of the RTD sensor, usually 100 + or 1000 + @param refResistor The value of the matching reference resistor, usually + 430 or 4300 + @returns Temperature in C +*/ +/**************************************************************************/ +float Adafruit_MAX31865::temperature(uint16_t RTDraw, float RTDnominal, + float refResistor) { + float Z1, Z2, Z3, Z4, Rt, temp; + + Rt = RTDraw; Rt /= 32768; Rt *= refResistor; @@ -187,24 +207,23 @@ float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) { temp = Z2 + (Z3 * Rt); temp = (sqrt(temp) + Z1) / Z4; - if (temp >= 0) - return temp; + if (temp >= 0) return temp; // ugh. Rt /= RTDnominal; - Rt *= 100; // normalize to 100 ohm + Rt *= 100; // normalize to 100 ohm float rpoly = Rt; temp = -242.02; temp += 2.2228 * rpoly; - rpoly *= Rt; // square + rpoly *= Rt; // square temp += 2.5859e-3 * rpoly; - rpoly *= Rt; // ^3 + rpoly *= Rt; // ^3 temp -= 4.8260e-6 * rpoly; - rpoly *= Rt; // ^4 + rpoly *= Rt; // ^4 temp -= 2.8183e-8 * rpoly; - rpoly *= Rt; // ^5 + rpoly *= Rt; // ^5 temp += 1.5243e-10 * rpoly; return temp; diff --git a/Adafruit_MAX31865.h b/Adafruit_MAX31865.h index bad2d45..003236a 100644 --- a/Adafruit_MAX31865.h +++ b/Adafruit_MAX31865.h @@ -79,6 +79,7 @@ class Adafruit_MAX31865 { void enableBias(bool b); float temperature(float RTDnominal, float refResistor); + float temperature(uint16_t RTDraw, float RTDnominal, float refResistor); private: Adafruit_SPIDevice spi_dev; From 80f3484aff9709ded8461942187d8d5c268a48b5 Mon Sep 17 00:00:00 2001 From: Tomas Dobrovolny Date: Wed, 9 Mar 2022 00:30:48 +0100 Subject: [PATCH 2/4] remove unused variables --- Adafruit_MAX31865.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/Adafruit_MAX31865.cpp b/Adafruit_MAX31865.cpp index 315adf5..a974d05 100644 --- a/Adafruit_MAX31865.cpp +++ b/Adafruit_MAX31865.cpp @@ -171,8 +171,6 @@ void Adafruit_MAX31865::setWires(max31865_numwires_t wires) { */ /**************************************************************************/ float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) { - float Z1, Z2, Z3, Z4, Rt, temp; - return temperature(readRTD(), RTDnominal, refResistor); } /**************************************************************************/ From 1408b597778a8ae5c69163d0274240dd96141111 Mon Sep 17 00:00:00 2001 From: Tomas Dobrovolny Date: Wed, 9 Mar 2022 00:35:15 +0100 Subject: [PATCH 3/4] fix clang format --- Adafruit_MAX31865.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Adafruit_MAX31865.cpp b/Adafruit_MAX31865.cpp index a974d05..b0a3de6 100644 --- a/Adafruit_MAX31865.cpp +++ b/Adafruit_MAX31865.cpp @@ -205,23 +205,24 @@ float Adafruit_MAX31865::temperature(uint16_t RTDraw, float RTDnominal, temp = Z2 + (Z3 * Rt); temp = (sqrt(temp) + Z1) / Z4; - if (temp >= 0) return temp; + if (temp >= 0) + return temp; // ugh. Rt /= RTDnominal; - Rt *= 100; // normalize to 100 ohm + Rt *= 100; // normalize to 100 ohm float rpoly = Rt; temp = -242.02; temp += 2.2228 * rpoly; - rpoly *= Rt; // square + rpoly *= Rt; // square temp += 2.5859e-3 * rpoly; - rpoly *= Rt; // ^3 + rpoly *= Rt; // ^3 temp -= 4.8260e-6 * rpoly; - rpoly *= Rt; // ^4 + rpoly *= Rt; // ^4 temp -= 2.8183e-8 * rpoly; - rpoly *= Rt; // ^5 + rpoly *= Rt; // ^5 temp += 1.5243e-10 * rpoly; return temp; From b37cf46dc5cf06ad882bc18da0b0248a5fd71f73 Mon Sep 17 00:00:00 2001 From: Tomas Dobrovolny Date: Wed, 9 Mar 2022 00:47:03 +0100 Subject: [PATCH 4/4] change the name of one of the overloaded functions temperature -> calculateTemperature --- Adafruit_MAX31865.cpp | 6 +++--- Adafruit_MAX31865.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Adafruit_MAX31865.cpp b/Adafruit_MAX31865.cpp index b0a3de6..67e9260 100644 --- a/Adafruit_MAX31865.cpp +++ b/Adafruit_MAX31865.cpp @@ -171,7 +171,7 @@ void Adafruit_MAX31865::setWires(max31865_numwires_t wires) { */ /**************************************************************************/ float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) { - return temperature(readRTD(), RTDnominal, refResistor); + return calculateTemperature(readRTD(), RTDnominal, refResistor); } /**************************************************************************/ /*! @@ -187,8 +187,8 @@ float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) { @returns Temperature in C */ /**************************************************************************/ -float Adafruit_MAX31865::temperature(uint16_t RTDraw, float RTDnominal, - float refResistor) { +float Adafruit_MAX31865::calculateTemperature(uint16_t RTDraw, float RTDnominal, + float refResistor) { float Z1, Z2, Z3, Z4, Rt, temp; Rt = RTDraw; diff --git a/Adafruit_MAX31865.h b/Adafruit_MAX31865.h index 003236a..88a8c1f 100644 --- a/Adafruit_MAX31865.h +++ b/Adafruit_MAX31865.h @@ -79,7 +79,8 @@ class Adafruit_MAX31865 { void enableBias(bool b); float temperature(float RTDnominal, float refResistor); - float temperature(uint16_t RTDraw, float RTDnominal, float refResistor); + float calculateTemperature(uint16_t RTDraw, float RTDnominal, + float refResistor); private: Adafruit_SPIDevice spi_dev;