Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some old issues #16

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 83 additions & 53 deletions Adafruit_MAX31865.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,51 @@ bool Adafruit_MAX31865::begin(max31865_numwires_t wires) {
spi_dev.begin();

setWires(wires);
readFault(true); // perform a selftest
enableBias(false);
autoConvert(false);
clearFault();

// Serial.print("config: ");
// Serial.println(readRegister8(MAX31856_CONFIG_REG), HEX);
// Serial.println(readRegister8(MAX31865_CONFIG_REG), HEX);
return true;
}

/**************************************************************************/
/*!
@brief Read the stored fault status bit included in every RTD read
@return true if the bit was set during the last readRTD()
*/
/**************************************************************************/
bool Adafruit_MAX31865::checkFault(void) { return _fault; }

/**************************************************************************/
/*!
@brief Read the raw 8-bit FAULTSTAT register
@return The raw unsigned 8-bit FAULT status register
*/
/**************************************************************************/
uint8_t Adafruit_MAX31865::readFault(void) {
return readRegister8(MAX31856_FAULTSTAT_REG);
return readRegister8(MAX31865_FAULTSTAT_REG);
}

/**************************************************************************/
/*!
@brief Read the raw 8-bit FAULTSTAT register
@param b If true a automatic fault-detection cycle is performed
@return The raw unsigned 8-bit FAULT status register
*/
/**************************************************************************/
uint8_t Adafruit_MAX31865::readFault(boolean b) {
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31865_CONFIG_FAULTDETCYCLE; // trigger automatic fault-detection
// cycle
writeRegister8(MAX31865_CONFIG_REG, t);
delay(5); // wait for 5ms
}

return readRegister8(MAX31865_FAULTSTAT_REG);
}

/**************************************************************************/
Expand All @@ -87,10 +115,11 @@ uint8_t Adafruit_MAX31865::readFault(void) {
*/
/**************************************************************************/
void Adafruit_MAX31865::clearFault(void) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
t &= ~0x2C;
t |= MAX31856_CONFIG_FAULTSTAT;
writeRegister8(MAX31856_CONFIG_REG, t);
t |= MAX31865_CONFIG_FAULTSTAT;
writeRegister8(MAX31865_CONFIG_REG, t);
_fault = false;
}

/**************************************************************************/
Expand All @@ -100,13 +129,13 @@ void Adafruit_MAX31865::clearFault(void) {
*/
/**************************************************************************/
void Adafruit_MAX31865::enableBias(bool b) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_BIAS; // enable bias
t |= MAX31865_CONFIG_BIAS; // enable bias
} else {
t &= ~MAX31856_CONFIG_BIAS; // disable bias
t &= ~MAX31865_CONFIG_BIAS; // disable bias
}
writeRegister8(MAX31856_CONFIG_REG, t);
writeRegister8(MAX31865_CONFIG_REG, t);
}

/**************************************************************************/
Expand All @@ -116,13 +145,13 @@ void Adafruit_MAX31865::enableBias(bool b) {
*/
/**************************************************************************/
void Adafruit_MAX31865::autoConvert(bool b) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_MODEAUTO; // enable autoconvert
t |= MAX31865_CONFIG_MODEAUTO; // enable autoconvert
} else {
t &= ~MAX31856_CONFIG_MODEAUTO; // disable autoconvert
t &= ~MAX31865_CONFIG_MODEAUTO; // disable autoconvert
}
writeRegister8(MAX31856_CONFIG_REG, t);
writeRegister8(MAX31865_CONFIG_REG, t);
}

/**************************************************************************/
Expand All @@ -133,13 +162,13 @@ void Adafruit_MAX31865::autoConvert(bool b) {
/**************************************************************************/

void Adafruit_MAX31865::enable50Hz(bool b) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (b) {
t |= MAX31856_CONFIG_FILT50HZ;
t |= MAX31865_CONFIG_FILT50HZ;
} else {
t &= ~MAX31856_CONFIG_FILT50HZ;
t &= ~MAX31865_CONFIG_FILT50HZ;
}
writeRegister8(MAX31856_CONFIG_REG, t);
writeRegister8(MAX31865_CONFIG_REG, t);
}

/**************************************************************************/
Expand All @@ -150,14 +179,14 @@ void Adafruit_MAX31865::enable50Hz(bool b) {
*/
/**************************************************************************/
void Adafruit_MAX31865::setWires(max31865_numwires_t wires) {
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
if (wires == MAX31865_3WIRE) {
t |= MAX31856_CONFIG_3WIRE;
t |= MAX31865_CONFIG_3WIRE;
} else {
// 2 or 4 wire
t &= ~MAX31856_CONFIG_3WIRE;
t &= ~MAX31865_CONFIG_3WIRE;
}
writeRegister8(MAX31856_CONFIG_REG, t);
writeRegister8(MAX31865_CONFIG_REG, t);
}

/**************************************************************************/
Expand All @@ -182,34 +211,32 @@ float Adafruit_MAX31865::temperature(float RTDnominal, float refResistor) {

// Serial.print("\nResistance: "); Serial.println(Rt, 8);

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;
// first, if needed, normalize to 100 ohm
if (RTDnominal != 100.0)
Rt *= 100.0 / RTDnominal;

// ugh.
Rt /= RTDnominal;
Rt *= 100; // normalize to 100 ohm

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;
if (Rt >= 100.0) { // above 100 Ohm temperature will become positive
Z1 = -RTD_A;
Z2 = RTD_A * RTD_A - (4 * RTD_B);
Z3 = (4 * RTD_B) / 100.0;
Z4 = 2 * RTD_B;

temp = Z2 + (Z3 * Rt);
temp = (sqrt(temp) + Z1) / Z4;
} else {
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;
}

Expand All @@ -223,15 +250,18 @@ uint16_t Adafruit_MAX31865::readRTD(void) {
clearFault();
enableBias(true);
delay(10);
uint8_t t = readRegister8(MAX31856_CONFIG_REG);
t |= MAX31856_CONFIG_1SHOT;
writeRegister8(MAX31856_CONFIG_REG, t);
uint8_t t = readRegister8(MAX31865_CONFIG_REG);
t |= MAX31865_CONFIG_1SHOT;
writeRegister8(MAX31865_CONFIG_REG, t);
delay(65);

uint16_t rtd = readRegister16(MAX31856_RTDMSB_REG);
uint16_t rtd = readRegister16(MAX31865_RTDMSB_REG);

enableBias(false); // to lessen sensor self-heating

_fault = rtd & 0x0001; // Store the fault flag

// remove fault
rtd >>= 1;
rtd >>= 1; // remove fault

return rtd;
}
Expand Down
42 changes: 23 additions & 19 deletions Adafruit_MAX31865.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@
#ifndef ADAFRUIT_MAX31865_H
#define ADAFRUIT_MAX31865_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_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_FAULTDETCYCLE 0x84
#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
Expand Down Expand Up @@ -70,6 +71,8 @@ class Adafruit_MAX31865 {
bool begin(max31865_numwires_t x = MAX31865_2WIRE);

uint8_t readFault(void);
uint8_t readFault(boolean b);
bool checkFault(void);
void clearFault(void);
uint16_t readRTD();

Expand All @@ -81,7 +84,8 @@ class Adafruit_MAX31865 {
float temperature(float RTDnominal, float refResistor);

private:
Adafruit_SPIDevice spi_dev = NULL;
int8_t _sclk, _miso, _mosi, _cs;
bool _fault;

void readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n);

Expand Down
44 changes: 23 additions & 21 deletions examples/max31865/max31865.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,30 @@ void loop() {
Serial.print("Temperature = "); Serial.println(thermo.temperature(RNOMINAL, RREF));

// Check and print any faults
uint8_t fault = thermo.readFault();
if (fault) {
Serial.print("Fault 0x"); Serial.println(fault, HEX);
if (fault & MAX31865_FAULT_HIGHTHRESH) {
Serial.println("RTD High Threshold");
if (thermo.checkFault()) {
uint8_t fault = thermo.readFault();
if (fault) {
Serial.print("Fault 0x"); Serial.println(fault, HEX);
if (fault & MAX31865_FAULT_HIGHTHRESH) {
Serial.println("RTD High Threshold");
}
if (fault & MAX31865_FAULT_LOWTHRESH) {
Serial.println("RTD Low Threshold");
}
if (fault & MAX31865_FAULT_REFINLOW) {
Serial.println("REFIN- > 0.85 x Bias");
}
if (fault & MAX31865_FAULT_REFINHIGH) {
Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_RTDINLOW) {
Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_OVUV) {
Serial.println("Under/Over voltage");
}
thermo.clearFault();
}
if (fault & MAX31865_FAULT_LOWTHRESH) {
Serial.println("RTD Low Threshold");
}
if (fault & MAX31865_FAULT_REFINLOW) {
Serial.println("REFIN- > 0.85 x Bias");
}
if (fault & MAX31865_FAULT_REFINHIGH) {
Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_RTDINLOW) {
Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
}
if (fault & MAX31865_FAULT_OVUV) {
Serial.println("Under/Over voltage");
}
thermo.clearFault();
}
Serial.println();
delay(1000);
Expand Down