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 0:9294dd756a0c, committed 2017-06-11
- Comitter:
- danjulio
- Date:
- Sun Jun 11 04:00:56 2017 +0000
- Commit message:
- Initial commit of max31865 ported to mbed with swspi
Changed in this revision
| Adafruit_MAX31865.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Adafruit_MAX31865.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9294dd756a0c Adafruit_MAX31865.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_MAX31865.cpp Sun Jun 11 04:00:56 2017 +0000
@@ -0,0 +1,207 @@
+/***************************************************
+ 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
+ ****************************************************/
+#include "Adafruit_MAX31865.h"
+
+Adafruit_MAX31865::Adafruit_MAX31865(swspi& spiO, int ssNum, PinName rdyPin)
+ :
+ ssN(ssNum),
+ spi(spiO)
+{
+ debug = false;
+ rdyP = new DigitalIn(rdyPin);
+}
+
+bool Adafruit_MAX31865::begin(max31865_numwires_t wires) {
+
+ /*
+ for (uint8_t i=0; i<8; i++) {
+ readRegister8(i);
+ }
+ */
+
+ setWires(wires);
+ enableBias(false);
+ autoConvert(false);
+ clearFault();
+
+ if (debug) {
+ printf("config: 0x%2x\r\n", readRegister8(MAX31865_CONFIG_REG));
+ }
+ return true;
+}
+
+
+uint8_t Adafruit_MAX31865::readFault(void) {
+ return readRegister8(MAX31865_FAULTSTAT_REG);
+}
+
+void Adafruit_MAX31865::clearFault(void) {
+ uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ t &= ~0x2C;
+ t |= MAX31865_CONFIG_FAULTSTAT;
+ writeRegister8(MAX31865_CONFIG_REG, t);
+}
+
+void Adafruit_MAX31865::enableBias(bool b) {
+ 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 Adafruit_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 Adafruit_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 Adafruit_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();
+ } else {
+ Rt = rtdVal;
+ }
+ Rt /= 32768;
+ Rt *= refResistor;
+
+ if (debug) {
+ printf("Resistance: %1.4f\r\n", Rt);
+ }
+
+ 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;
+}
+
+
+bool Adafruit_MAX31865::isReady(uint16_t* rtdVal) {
+ if (rdyP->read() == 0) {
+ *rtdVal = readRegister16(MAX31865_RTDMSB_REG);
+
+ // remove fault
+ *rtdVal >>= 1;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+
+uint16_t Adafruit_MAX31865::readRTD (void) {
+ clearFault();
+ enableBias(true);
+ wait_ms(10);
+ uint8_t t = readRegister8(MAX31865_CONFIG_REG);
+ t |= MAX31865_CONFIG_1SHOT;
+ writeRegister8(MAX31865_CONFIG_REG, t);
+ wait_ms(65);
+
+ uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);
+
+ // remove fault
+ rtd >>= 1;
+
+ return rtd;
+}
+
+/**********************************************/
+
+uint8_t Adafruit_MAX31865::readRegister8(uint8_t addr) {
+ return spi.spiRead(addr, 1, ssN);
+}
+
+uint16_t Adafruit_MAX31865::readRegister16(uint8_t addr) {
+ uint8_t buffer[2];
+
+ spi.spiBurstRead(addr, buffer, 2, 1, ssN);
+
+ uint16_t ret = buffer[0];
+ ret <<= 8;
+ ret |= buffer[1];
+
+ return ret;
+}
+
+
+void Adafruit_MAX31865::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) {
+ addr &= 0x7F; // make sure top bit is not set
+
+ spi.spiBurstRead(addr, buffer, n, 1, ssN);
+
+ if (debug) {
+ printf("$0x%2x: ", addr);
+ while (n--) {
+ printf(" 0x%2x", *buffer++);
+ }
+ printf("\n");
+ }
+}
+
+
+void Adafruit_MAX31865::writeRegister8(uint8_t addr, uint8_t data) {
+ spi.spiWrite(addr, data, 1, ssN);
+
+ if (debug) {
+ printf("$0x%2x = 0x%2x\r\n", addr, data);
+ }
+}
+
diff -r 000000000000 -r 9294dd756a0c Adafruit_MAX31865.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Adafruit_MAX31865.h Sun Jun 11 04:00:56 2017 +0000
@@ -0,0 +1,94 @@
+/***************************************************
+ 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
+ ****************************************************/
+#ifndef ADAFRUIT_MAX31865_H
+#define ADAFRUIT_MAX31865_H
+
+#include <stdint.h>
+#include "mbed.h"
+#include "swspi.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 Adafruit_MAX31865 {
+ public:
+ Adafruit_MAX31865(swspi& spiO, int ssNum, PinName rdyPin);
+
+ bool begin(max31865_numwires_t x = MAX31865_2WIRE);
+
+ uint8_t readFault(void);
+ void clearFault(void);
+ uint16_t readRTD();
+
+ void setWires(max31865_numwires_t wires);
+ void autoConvert(bool b);
+ void enableBias(bool b);
+ bool isReady(uint16_t* rtdVal);
+ float temperature(float RTDnominal, float refResistor, uint16_t rtdVal = 0);
+
+ private:
+ bool debug;
+ int ssN; // Slave select for swspi
+ swspi& spi;
+ DigitalIn* rdyP; // Ready Pin
+
+ 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