Skip to content

Commit

Permalink
Merge pull request #20 from caternuson/iss19_busio
Browse files Browse the repository at this point in the history
Convert to BusIO
  • Loading branch information
ladyada authored May 20, 2020
2 parents a01a9d9 + 8a958f2 commit efc7104
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 94 deletions.
107 changes: 20 additions & 87 deletions Adafruit_MAX31856.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Arduino platform. It is designed specifically to work with the
* Adafruit MAX31856 breakout: https://www.adafruit.com/products/3263
*
* These sensors use SPI to communicate, 4 pins are required to
* These sensors use SPI to communicate, 4 pins are required to
* interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
Expand Down Expand Up @@ -42,8 +42,6 @@
#include <stdlib.h>
#include <SPI.h>

static SPISettings max31856_spisettings = SPISettings(500000, MSBFIRST, SPI_MODE1);


/**************************************************************************/
/*!
Expand All @@ -55,11 +53,9 @@ static SPISettings max31856_spisettings = SPISettings(500000, MSBFIRST, SPI_MODE
*/
/**************************************************************************/
Adafruit_MAX31856::Adafruit_MAX31856(int8_t spi_cs, int8_t spi_mosi, int8_t spi_miso, int8_t spi_clk) {
_sclk = spi_clk;
_cs = spi_cs;
_miso = spi_miso;
_mosi = spi_mosi;
spi_dev = Adafruit_SPIDevice(spi_cs, spi_clk, spi_miso, spi_mosi, 1000000);

initialized = false;
}

/**************************************************************************/
Expand All @@ -69,8 +65,9 @@ Adafruit_MAX31856::Adafruit_MAX31856(int8_t spi_cs, int8_t spi_mosi, int8_t spi_
*/
/**************************************************************************/
Adafruit_MAX31856::Adafruit_MAX31856(int8_t spi_cs) {
_cs = spi_cs;
_sclk = _miso = _mosi = -1;
spi_dev = Adafruit_SPIDevice(spi_cs, 1000000, SPI_BITORDER_MSBFIRST, SPI_MODE1);

initialized = false;
}

/**************************************************************************/
Expand All @@ -80,22 +77,13 @@ Adafruit_MAX31856::Adafruit_MAX31856(int8_t spi_cs) {
*/
/**************************************************************************/
boolean Adafruit_MAX31856::begin(void) {
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);

if (_sclk != -1) {
//define pin modes
pinMode(_sclk, OUTPUT);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);
} else {
//start and configure hardware SPI
SPI.begin();
}
initialized = spi_dev.begin();

if (!initialized) return false;

// assert on any fault
writeRegister8(MAX31856_MASK_REG, 0x0);

writeRegister8(MAX31856_CR0_REG, MAX31856_CR0_OCFAULT0);
setThermocoupleType(MAX31856_TCTYPE_K);

Expand Down Expand Up @@ -140,7 +128,7 @@ uint8_t Adafruit_MAX31856::readFault(void) {

/**************************************************************************/
/*!
@brief Sets the threshhold for internal chip temperature range
@brief Sets the threshhold for internal chip temperature range
for fault detection. NOT the thermocouple temperature range!
@param low Low (min) temperature, signed 8 bit so -128 to 127 degrees C
@param high High (max) temperature, signed 8 bit so -128 to 127 degrees C
Expand All @@ -153,7 +141,7 @@ void Adafruit_MAX31856::setColdJunctionFaultThreshholds(int8_t low, int8_t high)

/**************************************************************************/
/*!
@brief Sets the mains noise filter. Can be set to 50 or 60hz.
@brief Sets the mains noise filter. Can be set to 50 or 60hz.
Defaults to 60hz. You need to call this if you live in a 50hz country.
@param noiseFilter One of MAX31856_NOISE_FILTER_50HZ or MAX31856_NOISE_FILTER_60HZ
*/
Expand All @@ -170,7 +158,7 @@ void Adafruit_MAX31856::setNoiseFilter(max31856_noise_filter_t noiseFilter) {

/**************************************************************************/
/*!
@brief Sets the threshhold for thermocouple temperature range
@brief Sets the threshhold for thermocouple temperature range
for fault detection. NOT the internal chip temperature range!
@param flow Low (min) temperature, floating point
@param fhigh High (max) temperature, floating point
Expand Down Expand Up @@ -265,7 +253,7 @@ uint16_t Adafruit_MAX31856::readRegister16(uint8_t addr) {
uint16_t ret = buffer[0];
ret <<= 8;
ret |= buffer[1];

return ret;
}

Expand All @@ -278,75 +266,20 @@ uint32_t Adafruit_MAX31856::readRegister24(uint8_t addr) {
ret |= buffer[1];
ret <<= 8;
ret |= buffer[2];

return ret;
}


void Adafruit_MAX31856::readRegisterN(uint8_t addr, uint8_t buffer[], uint8_t n) {
addr &= 0x7F; // make sure top bit is not set

if (_sclk == -1)
SPI.beginTransaction(max31856_spisettings);
else
digitalWrite(_sclk, HIGH);

digitalWrite(_cs, LOW);

spixfer(addr);

//Serial.print("$"); Serial.print(addr, HEX); Serial.print(": ");
while (n--) {
buffer[0] = spixfer(0xFF);
//Serial.print(" 0x"); Serial.print(buffer[0], HEX);
buffer++;
}
//Serial.println();

if (_sclk == -1)
SPI.endTransaction();
addr &= 0x7F; // MSB=0 for read, make sure top bit is not set

digitalWrite(_cs, HIGH);
spi_dev.write_then_read(&addr, 1, buffer, n);
}


void Adafruit_MAX31856::writeRegister8(uint8_t addr, uint8_t data) {
addr |= 0x80; // make sure top bit is set

if (_sclk == -1)
SPI.beginTransaction(max31856_spisettings);
else
digitalWrite(_sclk, HIGH);

digitalWrite(_cs, LOW);

spixfer(addr);
spixfer(data);

//Serial.print("$"); Serial.print(addr, HEX); Serial.print(" = 0x"); Serial.println(data, HEX);
addr |= 0x80; // MSB=1 for write, make sure top bit is set

if (_sclk == -1)
SPI.endTransaction();
uint8_t buffer[2] = {addr, data};

digitalWrite(_cs, HIGH);
}



uint8_t Adafruit_MAX31856::spixfer(uint8_t x) {
if (_sclk == -1)
return SPI.transfer(x);

// software spi
//Serial.println("Software SPI");
uint8_t reply = 0;
for (int i=7; i>=0; i--) {
reply <<= 1;
digitalWrite(_sclk, LOW);
digitalWrite(_mosi, x & (1<<i));
digitalWrite(_sclk, HIGH);
if (digitalRead(_miso))
reply |= 1;
}
return reply;
spi_dev.write(buffer, 2);
}
14 changes: 8 additions & 6 deletions Adafruit_MAX31856.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Arduino platform. It is designed specifically to work with the
* Adafruit MAX31856 breakout: https://www.adafruit.com/products/3263
*
* These sensors use SPI to communicate, 4 pins are required to
* These sensors use SPI to communicate, 4 pins are required to
* interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
Expand Down Expand Up @@ -38,10 +38,10 @@
#define MAX31856_LTHFTL_REG 0x06 ///< Linearized Temperature High Fault Threshold Register, LSB
#define MAX31856_LTLFTH_REG 0x07 ///< Linearized Temperature Low Fault Threshold Register, MSB
#define MAX31856_LTLFTL_REG 0x08 ///< Linearized Temperature Low Fault Threshold Register, LSB
#define MAX31856_CJTO_REG 0x09 ///< Cold-Junction Temperature Offset Register
#define MAX31856_CJTO_REG 0x09 ///< Cold-Junction Temperature Offset Register
#define MAX31856_CJTH_REG 0x0A ///< Cold-Junction Temperature Register, MSB
#define MAX31856_CJTL_REG 0x0B ///< Cold-Junction Temperature Register, LSB
#define MAX31856_LTCBH_REG 0x0C ///< Linearized TC Temperature, Byte 2
#define MAX31856_LTCBH_REG 0x0C ///< Linearized TC Temperature, Byte 2
#define MAX31856_LTCBM_REG 0x0D ///< Linearized TC Temperature, Byte 1
#define MAX31856_LTCBL_REG 0x0E ///< Linearized TC Temperature, Byte 0
#define MAX31856_SR_REG 0x0F ///< Fault Status Register
Expand Down Expand Up @@ -83,8 +83,10 @@ typedef enum
#include "WProgram.h"
#endif

#include <Adafruit_SPIDevice.h>

/**************************************************************************/
/*!
/*!
@brief Class that stores state and functions for interacting with MAX31856
*/
/**************************************************************************/
Expand All @@ -110,7 +112,8 @@ class Adafruit_MAX31856 {
void setNoiseFilter(max31856_noise_filter_t noiseFilter);

private:
int8_t _sclk, _miso, _mosi, _cs;
Adafruit_SPIDevice spi_dev = NULL;
boolean initialized;

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

Expand All @@ -119,7 +122,6 @@ class Adafruit_MAX31856 {
uint32_t readRegister24(uint8_t addr);

void writeRegister8(uint8_t addr, uint8_t reg);
uint8_t spixfer(uint8_t addr);
};

#endif
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name=Adafruit MAX31856 library
version=1.0.3
version=1.1.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Library for the Adafruit Thermocouple Amplifier breakout with MAX31856
paragraph=Library for the Adafruit Thermocouple Amplifier breakout with MAX31856
category=Sensors
url=https://github.com/adafruit/Adafruit_MAX31856
architectures=*
depends=Adafruit BusIO

0 comments on commit efc7104

Please sign in to comment.