Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:b42b7148fbe0, committed 2019-03-29
- Comitter:
- freakone
- Date:
- Fri Mar 29 14:09:06 2019 +0000
- Parent:
- 1:f5d6dfd8b4a6
- Commit message:
- new library content and calculations
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31865.cpp Fri Mar 29 14:09:06 2019 +0000
@@ -0,0 +1,179 @@
+#include "MAX31865.h"
+
+MAX31865::MAX31865(SPI &_spi, PinName _ncs)
+ : spi(_spi), ncs(_ncs, 1), sensorPresent(false)
+{
+}
+
+bool MAX31865::begin(max31865_numwires_t wires)
+{
+ setWires(wires);
+ enableBias(true);
+ autoConvert(true);
+ clearFault();
+
+ return true;
+}
+
+uint8_t MAX31865::readFault(void)
+{
+ return readRegister8(MAX31865_FAULTSTAT_REG);
+}
+
+void MAX31865::clearFault(void)
+{
+ volatile uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ t &= ~0x2C;
+ t |= MAX31865_CONFIG_FAULTSTAT;
+ writeRegister8(MAX31865_CONFIG_REG, t);
+}
+
+void MAX31865::enableBias(bool b)
+{
+ volatile uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ if (b)
+ {
+ t |= MAX31865_CONFIG_BIAS; // enable bias
+ }
+ else
+ {
+ t &= ~MAX31865_CONFIG_BIAS; // disable bias
+ }
+ writeRegister8(MAX31865_CONFIG_REG, t);
+}
+
+void MAX31865::autoConvert(bool b)
+{
+ uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ if (b)
+ {
+ t |= MAX31865_CONFIG_MODEAUTO; // enable autoconvert
+ }
+ else
+ {
+ t &= ~MAX31865_CONFIG_MODEAUTO; // disable autoconvert
+ }
+ writeRegister8(MAX31865_CONFIG_REG, t);
+}
+
+void MAX31865::setWires(max31865_numwires_t wires)
+{
+ uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ if (wires == MAX31865_3WIRE)
+ {
+ t |= MAX31865_CONFIG_3WIRE;
+ }
+ else
+ {
+ // 2 or 4 wire
+ t &= ~MAX31865_CONFIG_3WIRE;
+ }
+ writeRegister8(MAX31865_CONFIG_REG, t);
+}
+
+float MAX31865::temperature(float RTDnominal, float refResistor, uint16_t rtdVal)
+{
+ // http://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf
+
+ float Z1, Z2, Z3, Z4, Rt, temp;
+
+ if (rtdVal == 0)
+ {
+ Rt = readRTD();
+
+ if (!sensorPresent)
+ {
+ return 0.0f;
+ }
+ }
+ else
+ {
+ Rt = rtdVal;
+ }
+ Rt /= 32768;
+ Rt *= refResistor;
+
+ Z1 = -RTD_A;
+ Z2 = RTD_A * RTD_A - (4 * RTD_B);
+ Z3 = (4 * RTD_B) / RTDnominal;
+ Z4 = 2 * RTD_B;
+
+ temp = Z2 + (Z3 * Rt);
+ temp = (sqrt(temp) + Z1) / Z4;
+
+ if (temp >= 0)
+ return temp;
+
+ // ugh.
+ float rpoly = Rt;
+
+ temp = -242.02f;
+ temp += 2.2228f * rpoly;
+ rpoly *= Rt; // square
+ temp += (float)2.5859e-3 * rpoly;
+ rpoly *= Rt; // ^3
+ temp -= (float)4.8260e-6 * rpoly;
+ rpoly *= Rt; // ^4
+ temp -= (float)2.8183e-8 * rpoly;
+ rpoly *= Rt; // ^5
+ temp += (float)1.5243e-10 * rpoly;
+
+ return temp;
+}
+
+uint16_t MAX31865::readRTD(void)
+{
+ uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
+ sensorPresent = readFault() == 0;
+ clearFault();
+
+ rtd >>= 1;
+
+ return rtd;
+}
+
+/**********************************************/
+
+uint8_t MAX31865::readRegister8(uint8_t addr)
+{
+ uint8_t ret[] = {0};
+ readRegisterN(addr, ret, 1);
+ return ret[0];
+}
+
+uint16_t MAX31865::readRegister16(uint8_t addr)
+{
+ uint8_t buffer[2] = {0, 0};
+ readRegisterN(addr, buffer, 2);
+
+ uint16_t ret = buffer[0];
+ ret <<= 8;
+ ret |= buffer[1];
+
+ return ret;
+}
+
+void MAX31865::writeRegister8(uint8_t addr, uint8_t data)
+{
+ ncs = 0; //select chip
+ spi.write(addr | 0x80); // make sure top bit is set
+ spi.write(data);
+ ncs = 1;
+}
+
+void MAX31865::readRegisterN(uint8_t address, uint8_t buffer[], uint8_t n)
+{
+ address &= 0x7F; // make sure top bit is not set
+
+ ncs = 0;
+
+ spi.write(address);
+
+ for (uint8_t i = 0; i < n; i++)
+ {
+ buffer[i] = spi.write(0x00);
+ }
+
+ ncs = 1;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31865.h Fri Mar 29 14:09:06 2019 +0000
@@ -0,0 +1,90 @@
+/***************************************************
+ This is a library for the Adafruit PT100/P1000 RTD Sensor w/MAX31865
+
+ Designed specifically to work with the Adafruit RTD Sensor
+ ----> https://www.adafruit.com/products/3328
+
+ This sensor uses SPI to communicate, 4 pins are required to
+ interface
+ Adafruit invests time and resources providing this open source code,
+ please support Adafruit and open-source hardware by purchasing
+ products from Adafruit!
+
+ Written by Limor Fried/Ladyada for Adafruit Industries.
+ BSD license, all text above must be included in any redistribution
+
+ Modified for mbed project - Dan Julio - 5/2017
+ Improvements - freakone 3/2019
+ ****************************************************/
+#ifndef MAX31865_H
+#define MAX31865_H
+
+#include <stdint.h>
+#include "mbed.h"
+
+#define MAX31865_CONFIG_REG 0x00
+#define MAX31865_CONFIG_BIAS 0x80
+#define MAX31865_CONFIG_MODEAUTO 0x40
+#define MAX31865_CONFIG_MODEOFF 0x00
+#define MAX31865_CONFIG_1SHOT 0x20
+#define MAX31865_CONFIG_3WIRE 0x10
+#define MAX31865_CONFIG_24WIRE 0x00
+#define MAX31865_CONFIG_FAULTSTAT 0x02
+#define MAX31865_CONFIG_FILT50HZ 0x01
+#define MAX31865_CONFIG_FILT60HZ 0x00
+
+#define MAX31865_RTDMSB_REG 0x01
+#define MAX31865_RTDLSB_REG 0x02
+#define MAX31865_HFAULTMSB_REG 0x03
+#define MAX31865_HFAULTLSB_REG 0x04
+#define MAX31865_LFAULTMSB_REG 0x05
+#define MAX31865_LFAULTLSB_REG 0x06
+#define MAX31865_FAULTSTAT_REG 0x07
+
+#define MAX31865_FAULT_HIGHTHRESH 0x80
+#define MAX31865_FAULT_LOWTHRESH 0x40
+#define MAX31865_FAULT_REFINLOW 0x20
+#define MAX31865_FAULT_REFINHIGH 0x10
+#define MAX31865_FAULT_RTDINLOW 0x08
+#define MAX31865_FAULT_OVUV 0x04
+
+#define RTD_A 3.9083e-3
+#define RTD_B -5.775e-7
+
+typedef enum max31865_numwires
+{
+ MAX31865_2WIRE = 0,
+ MAX31865_3WIRE = 1,
+ MAX31865_4WIRE = 0
+} max31865_numwires_t;
+
+class MAX31865
+{
+public:
+ MAX31865(SPI &_spi, PinName _ncs);
+
+ bool begin(max31865_numwires_t x = MAX31865_2WIRE);
+
+ uint8_t isSensorPresent() { return sensorPresent; };
+ uint8_t readFault(void);
+ void clearFault(void);
+ uint16_t readRTD();
+
+ void setWires(max31865_numwires_t wires);
+ void autoConvert(bool b);
+ void enableBias(bool b);
+ float temperature(float RTDnominal, float refResistor, uint16_t rtdVal = 0);
+
+private:
+ SPI &spi;
+ DigitalOut ncs;
+ bool sensorPresent;
+
+ void readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n);
+ uint8_t readRegister8(uint8_t addr);
+ uint16_t readRegister16(uint8_t addr);
+ void writeRegister8(uint8_t addr, uint8_t reg);
+};
+
+#endif
+
--- a/max31865.cpp Fri Mar 22 10:47:38 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,179 +0,0 @@
-#include "max31865.h"
-#include "mbed.h"
-
-max31865::max31865(PinName MOSI, PinName MISO, PinName SCLK, PinName CS) : spi(MOSI, MISO, SCLK), cs(CS) // mosi, miso, sclk
-{
- //constructor
- cs = 1; //deselect chip
- spi.format(8, 1); //set mode 1 and 8 bit
- spi.frequency(500000); //set frequency to 0.5mhz
- RTDpresent = false;
- RTDtype = PT100;
-}
-
-float max31865::ReadRTD(void)
-{
- volatile uint16_t reg_rtd = ReadRegistor16(MAX31856_RTDMSB_REG);
- RTDpresent = ReadRegistor8(MAX31856_FAULTSTAT_REG) == 0;
- ClearFault();
-
- reg_rtd >>= 1;
-
- float Z1, Z2, Z3, Z4, Rt, temp;
-
- float RTDnominal = 100, refResistor = 400;
-
- if (RTDtype == PT1000)
- {
- RTDnominal = 1000;
- refResistor = 4000;
- }
-
- Rt = reg_rtd;
- Rt /= 32768;
- Rt *= refResistor;
-
- Z1 = -RTD_A;
- Z2 = RTD_A * RTD_A - (4 * RTD_B);
- Z3 = (4 * RTD_B) / RTDnominal;
- Z4 = 2 * RTD_B;
-
- temp = Z2 + (Z3 * Rt);
- temp = (sqrt(temp) + Z1) / Z4;
-
- if (temp >= 0)
- return temp;
-
- float rpoly = Rt;
-
- temp = -242.02;
- temp += 2.2228 * rpoly;
- rpoly *= Rt; // square
- temp += 2.5859e-3 * rpoly;
- rpoly *= Rt; // ^3
- temp -= 4.8260e-6 * rpoly;
- rpoly *= Rt; // ^4
- temp -= 2.8183e-8 * rpoly;
- rpoly *= Rt; // ^5
- temp += 1.5243e-10 * rpoly;
-
- return temp;
-}
-
-void max31865::Begin(max31865_numwires_t wires, max31865_rtd_t type)
-{
- cs = 1;
- RTDtype = type;
- SetWires(wires);
- EnableBias(true);
- AutoConvert(true);
- ClearFault();
-}
-
-int max31865::ReadFault()
-{
- return ReadRegistor8(MAX31856_FAULTSTAT_REG);
-}
-
-void max31865::ClearFault()
-{
- int t = ReadRegistor8(MAX31856_CONFIG_REG);
- // t &= ~0x2C;
- t |= MAX31856_CONFIG_FAULTSTAT;
- WriteRegistor(MAX31856_CONFIG_REG, t);
-}
-
-void max31865::EnableBias(bool b)
-{
- int t = ReadRegistor8(MAX31856_CONFIG_REG);
- if (b)
- {
- t |= MAX31856_CONFIG_BIAS; // enable bias
- }
- else
- {
- t &= ~MAX31856_CONFIG_BIAS; // disable bias
- }
- WriteRegistor(MAX31856_CONFIG_REG, t);
-}
-
-void max31865::AutoConvert(bool b)
-{
- int t = ReadRegistor8(MAX31856_CONFIG_REG);
- if (b)
- {
- t |= MAX31856_CONFIG_MODEAUTO; // enable autoconvert
- }
- else
- {
- t &= ~MAX31856_CONFIG_MODEAUTO; // disable autoconvert
- }
- WriteRegistor(MAX31856_CONFIG_REG, t);
-}
-
-void max31865::SetWires(max31865_numwires_t wires)
-{
- int t = ReadRegistor8(MAX31856_CONFIG_REG);
-
- if (wires == MAX31865_3WIRE)
- {
- t |= MAX31856_CONFIG_3WIRE;
- }
- else
- {
- // 2 or 4 wire
- t &= ~MAX31856_CONFIG_3WIRE;
- }
- WriteRegistor(MAX31856_CONFIG_REG, t);
-}
-
-int max31865::ReadRegistor8(int address)
-{
- int ret = 0;
- ReadRegistorN(address, &ret, 1);
- return ret;
-}
-
-int max31865::ReadRegistor16(int address)
-{
- int buffer[2] = {0, 0};
- ReadRegistorN(address, buffer, 2);
-
- int ret = buffer[0];
- ret <<= 8;
- ret |= buffer[1];
-
- return ret;
-}
-
-void max31865::ReadRegistorN(int address, int buffer[], int n)
-{
- address &= 0x7F; // make sure top bit is not set
-
- cs = 0;
-
- spiXfer(address);
-
- while (n--)
- {
- buffer[0] = spiXfer(0xFF);
- buffer++;
- }
-
- cs = 1;
-}
-
-void max31865::WriteRegistor(int address, int data)
-{
-
- cs = 0; //select chip
- spiXfer(address | 0x80); // make sure top bit is set
- spiXfer(data);
-
- cs = 1;
-}
-
-int max31865::spiXfer(int x)
-{
- return spi.write(x);
-}
\ No newline at end of file
--- a/max31865.h Fri Mar 22 10:47:38 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-#ifndef MBED_MAX31865_H
-#define MBED_MAX31865_H
-
-#include "mbed.h"
-
-#define MAX31856_CONFIG_REG 0x00
-#define MAX31856_CONFIG_BIAS 0x80
-#define MAX31856_CONFIG_MODEAUTO 0x40
-#define MAX31856_CONFIG_MODEOFF 0x00
-#define MAX31856_CONFIG_1SHOT 0x20
-#define MAX31856_CONFIG_3WIRE 0x10
-#define MAX31856_CONFIG_24WIRE 0x00
-#define MAX31856_CONFIG_FAULTSTAT 0x02
-#define MAX31856_CONFIG_FILT50HZ 0x01
-#define MAX31856_CONFIG_FILT60HZ 0x00
-
-#define MAX31856_RTDMSB_REG 0x01
-#define MAX31856_RTDLSB_REG 0x02
-#define MAX31856_HFAULTMSB_REG 0x03
-#define MAX31856_HFAULTLSB_REG 0x04
-#define MAX31856_LFAULTMSB_REG 0x05
-#define MAX31856_LFAULTLSB_REG 0x06
-#define MAX31856_FAULTSTAT_REG 0x07
-
-#define MAX31865_FAULT_HIGHTHRESH 0x80
-#define MAX31865_FAULT_LOWTHRESH 0x40
-#define MAX31865_FAULT_REFINLOW 0x20
-#define MAX31865_FAULT_REFINHIGH 0x10
-#define MAX31865_FAULT_RTDINLOW 0x08
-#define MAX31865_FAULT_OVUV 0x04
-
-#define RTD_A 3.9083e-3
-#define RTD_B -5.775e-7
-
-typedef enum max31865_numwires
-{
- MAX31865_2WIRE = 0,
- MAX31865_3WIRE = 1,
- MAX31865_4WIRE = 0
-} max31865_numwires_t;
-
-typedef enum
-{
- PT100 = 0,
- PT1000 = 0
-
-} max31865_rtd_t;
-
-class max31865
-{
-public:
- max31865(PinName MOSI, PinName MISO, PinName SCLK, PinName CS);
-
- void Begin(max31865_numwires_t wires, max31865_rtd_t type);
- int ReadFault();
- void ClearFault();
- float ReadRTD();
- bool IsRTDPresent() { return RTDpresent; };
-
- void SetWires(max31865_numwires_t wires);
- void AutoConvert(bool b);
- void EnableBias(bool b);
-
-private:
- SPI spi;
- DigitalOut cs;
- bool RTDpresent;
- max31865_rtd_t RTDtype;
-
- void
- ReadRegistorN(int address, int buffer[], int n);
-
- int ReadRegistor8(int address);
- int ReadRegistor16(int address);
-
- void WriteRegistor(int address, int reg);
- int spiXfer(int address);
-};
-
-#endif
\ No newline at end of file