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.
Fork of MAX31856_example_program by
MAX31856.cpp
- Committer:
- DevinAlexander
- Date:
- 2017-07-31
- Revision:
- 7:2e45068189b1
- Parent:
- 6:e1200ae7d6a3
- Child:
- 8:8723d0006097
File content as of revision 7:2e45068189b1:
#include <mbed.h>
#include "MAX31856.h"
#define LOG(args...) printf(args)
MAX31856::MAX31856(SPI& _spi, PinName _ncs, uint8_t _type, uint8_t _fltr, uint8_t _samples, uint8_t _conversion_mode) : spi(_spi), ncs(_ncs), samples(_samples) {
spi.format(8,3); //configure the correct SPI mode to beable to program the registers intially correctly
setThermocoupleType(_type);
setEmiFilterFreq(_fltr);
setNumSamplesAvg(_samples);
setConversionMode(_conversion_mode);
}
//*****************************************************************************
float MAX31856::readTC()
{
int32_t temp;
uint8_t buf_read[3], buf_write[3]={ADDRESS_LTCBH_READ,ADDRESS_LTCBM_READ,ADDRESS_LTCBL_READ};
// uint32_t time = us_ticker_read();
// uint32_t duration = time - lastReadTime;
// if (duration > 200000) { // more than 250ms
for(int i=0; i<3; i++) {
spiEnable();
buf_read[i]=spi.write(buf_write[i]);
buf_read[i]=spi.write(buf_write[i]);
spiDisable();
}
//Convert the registers contents into the correct value
temp =((buf_read[0] & 0xFF) << 11); //Shift Byte 2 into place
temp|=((buf_read[1] & 0xFF) << 3); //Shift Byte 1 into place
temp|=((buf_read[2] & 0xFF) >> 5); //Shift Byte 0 into place
float val=(temp/128.0f); //Divide the binary string by 2 to the 7th power
return val;
// }
}
//*****************************************************************************
float MAX31856::readCJ()
{
int32_t temp;
uint8_t buf_read[3], buf_write=ADDRESS_CJTH_READ;
spiEnable();
for(int i=0; i<3; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Convert the registers contents into the correct value
temp =((int32_t)(buf_read[1] << 6)); //Shift the MSB into place
temp|=((int32_t)(buf_read[2] >> 2)); //Shift the LSB into place
float val=((float)(temp/64.0)); //Divide the binary string by 2 to the 6th power
return val;
}
//Register:CR0 Bits: 7
//*****************************************************************************
bool MAX31856::setConversionMode(uint8_t val)
{
if (val==CR0_CONV_MODE_NORMALLY_OFF) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
conversion_mode=0;
LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_OFF\r\n");
}
else if (val==CR0_CONV_MODE_NORMALLY_ON) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
conversion_mode=1;
LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_ON\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 7. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 6
//*****************************************************************************
bool MAX31856::setOneShotMode(uint8_t val)
{
if (val==CR0_1_SHOT_MODE_NO_CONVERSION) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
LOG("Register containing\t\tsetOneShotMode\t\twas programmed with the parameter\t\tCR0_1_SHOT_MODE_NO_CONVERSIONS\r\n");
}
else if (val==CR0_1_SHOT_MODE_ONE_CONVERSION) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
LOG("Register containing\t\tsetOneShotMode\t\twas programmed with the parameter\t\tCR0_1_SHOT_MODE_NO_CONVERSIONS\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 6. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 5:4
//*****************************************************************************
bool MAX31856::setOpenCircuitFaultDetection(uint8_t val)
{
if (val==CR0_OC_DETECT_DISABLED) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_DISABLED\r\n");
}
else if (val==CR0_OC_DETECT_ENABLED_R_LESS_5k) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_R_LESS_5k\r\n");
}
else if (val==CR0_OC_DETECT_ENABLED_TC_LESS_2ms) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_LESS_2ms\r\n");
}
else if (val==CR0_OC_DETECT_ENABLED_TC_MORE_2ms) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_MORE_2ms\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bits 5:4. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 3
//*****************************************************************************
bool MAX31856::setColdJunctionDisable(uint8_t val)
{
if (val==CR0_COLD_JUNC_ENABLE) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
cold_junction_enabled=1;
LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_ENABLE\r\n");
}
else if (val==CR0_COLD_JUNC_DISABLE) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
cold_junction_enabled=0;
LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_DISABLE\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 3. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 2
//*****************************************************************************
bool MAX31856::setFaultMode(uint8_t val)
{
if (val==CR0_FAULT_MODE_COMPARATOR) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_COMPARATOR\r\n");
}
else if (val==CR0_FAULT_MODE_INTERUPT) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_INTERUPT\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 2. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 1
//*****************************************************************************
bool MAX31856::setFaultStatusClear(uint8_t val)
{
if (val==CR0_FAULTCLR_DEFAULT_VAL) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_DEFAULT_VAL\r\n");
}
else if (val==CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_RETURN_FAULTS_TO_ZERO\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 1. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR0 Bits: 0
//*****************************************************************************
bool MAX31856::setEmiFilterFreq(uint8_t val)
{
if (val==CR0_FILTER_OUT_60Hz) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
filter_mode=0;
LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_60Hz\r\n");
}
else if (val==CR0_FILTER_OUT_50Hz) {
return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
filter_mode=1;
LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_50Hz\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 0 (CR0) bit 0. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR1 Bits: 6:4
//*****************************************************************************
bool MAX31856::setNumSamplesAvg(uint8_t val)
{
if (val==CR1_AVG_TC_SAMPLES_1) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
samples=1;
LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_1\r\n");
}
else if (val==CR1_AVG_TC_SAMPLES_2) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
samples=2;
LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_2\r\n");
}
else if (val==CR1_AVG_TC_SAMPLES_4) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
samples=4;
LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_4\r\n");
}
else if (val==CR1_AVG_TC_SAMPLES_8) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
samples=8;
LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_8\r\n");
}
else if (val==CR1_AVG_TC_SAMPLES_16) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
samples=16;
LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_16\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 1 (CR1) bits 6:4. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:CR1 Bits: 3:0
//*****************************************************************************
bool MAX31856::setThermocoupleType(uint8_t val)
{
if (val==CR1_TC_TYPE_B) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_B\r\n");
}
else if (val==CR1_TC_TYPE_E) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_E\r\n");
}
else if (val==CR1_TC_TYPE_J) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_J\r\n");
}
else if (val==CR1_TC_TYPE_K) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_K\r\n");
}
else if (val==CR1_TC_TYPE_N) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_N\r\n");
}
else if (val==CR1_TC_TYPE_R) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_R\r\n");
}
else if (val==CR1_TC_TYPE_S) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_S\r\n");
}
else if (val==CR1_TC_TYPE_T) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=false;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_T\r\n");
}
else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_8) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=true;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_8\r\n");
}
else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_32) {
return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
voltage_mode=true;
LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_32\r\n");
}
else {
LOG("Incorrect parameter selected for Control Register 1 (CR1) bits 3:0. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:MASK Bits: 5:0
//*****************************************************************************
bool MAX31856::setFaultMasks(uint8_t val, bool enable)
{
if(enable)
val=0;
;
if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_5, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_HIGH\r\n");
}
else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_4, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_LOW\r\n");
}
else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_3, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_HIGH\r\n");
}
else if (val==MASK_TC_FAULT_THRESHOLD_LOW) { //Thermocouple Low Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_2, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_LOW\r\n");
}
else if (val==MASK_OVER_UNDER_VOLT_FAULT) { //Over-Voltage/Under-Voltage Input Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_1, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OVER_UNDER_VOLT_FAULT\r\n");
}
else if (val==MASK_OPEN_CIRCUIT_FAULT) { //Thermocouple Open-Circuit Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_0, val);
LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OPEN_CIRCUIT_FAULT\r\n");
}
else {
LOG("Incorrect parameter selected for Mask Register bits 5:0. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//Register:MASK Bits: 5:0
//******************************************************************************
float MAX31856::setFaultThresholds(uint8_t val, bool enable_mask, float temperature)
{
float return_val;
uint8_t temp_val;
if(enable_mask) {
temp_val=0;
}
else {
temp_val=val;
}
if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_5, temp_val);
int8_t temperature_byte=temperature;
if (temperature_byte>CJ_MAX_VAL_FAULT)
temperature_byte=CJ_MAX_VAL_FAULT;
else if (temperature_byte<=0 || temperature_byte>=CJ_MIN_VAL_FAULT)
temperature_byte=twosComplimentToSigned8(temperature_byte); //Convert the 2's compliment int into a signed value
else if (temperature_byte<CJ_MIN_VAL_FAULT)
temperature_byte=twosComplimentToSigned8(CJ_MIN_VAL_FAULT); //Convert the 2's compliment int into a signed value
//else the data is within range, no more manipulation of data is needed
return_val=registerWriteByte(ADDRESS_CJHF_WRITE, temperature_byte);
}
else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_4, temp_val);
int8_t temperature_byte=temperature;
if (temperature_byte>CJ_MAX_VAL_FAULT)
temperature_byte=CJ_MAX_VAL_FAULT;
else if (temperature_byte<=0 || temperature_byte>=CJ_MIN_VAL_FAULT)
temperature_byte=twosComplimentToSigned8(temperature_byte); //Convert the 2's compliment int into a signed value
else if (temperature_byte<CJ_MIN_VAL_FAULT)
temperature_byte=twosComplimentToSigned8(CJ_MIN_VAL_FAULT); //Convert the 2's compliment int into a signed value
//else the data is within range, no more manipulation of data is needed
return_val=registerWriteByte(ADDRESS_CJLF_WRITE, temperature_byte);
}
else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_3, temp_val);
if (temperature) {
int8_t temperature_byte[2];
int16_t temperature_multi_byte =(int16_t)(temperature*4.0);
if (temperature_multi_byte>(TC_MAX_VAL_FAULT*4.0f))
temperature_multi_byte=TC_MAX_VAL_FAULT*4.0f;
else if (temperature_multi_byte<=0 || temperature_multi_byte>=(TC_MIN_VAL_FAULT*4.0f))
temperature_multi_byte=twosComplimentToSigned16(temperature_multi_byte); //Convert the 2's compliment int into a signed value
else if (temperature_multi_byte<(TC_MIN_VAL_FAULT*4.0f))
temperature_multi_byte=twosComplimentToSigned16(TC_MIN_VAL_FAULT*4.0f); //Convert the 2's compliment int into a signed value
//now split up the 32bit int into two bytes to program the registers with
temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
return_val=registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]);
return_val=registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);
return_val=temperature;
}
// else {
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////return_val="WHATEVER IS IN THE REGISTERS WHEN YOU SET THE FLAG JUST INCASE YOU NEED TO SEE WHAT IS INSISIDE THE REGISTER";
// }
}
else if (val==MASK_TC_FAULT_THRESHOLD_LOW) //Thermocouple Low Threshold Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_2, temp_val);
else if (val==MASK_OVER_UNDER_VOLT_FAULT) //Over-Voltage/Under-Voltage Input Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_1, temp_val);
else if (val==MASK_OPEN_CIRCUIT_FAULT) //Thermocouple Open-Circuit Fault Mask
return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_0, temp_val);
else {
LOG("Incorrect parameter selected for MASK Register. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
//The following functions are for internal library use only
//******************************************************************************
void MAX31856::spiEnable()
{
ncs=0; //Set CS low to start transmission (interrupts conversion)
return;
}
//******************************************************************************
void MAX31856::spiDisable()
{
ncs=1; //Set CS high to stop transmission (restarts conversion)
return;
}
//******************************************************************************
bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, uint8_t clear_bits, uint8_t val)
{
uint8_t buf_read[2];
//Read the current contents of a register
spiEnable();
for(int i=0; i<2; i++) {
buf_read[i]=spi.write(read_address);
}
spiDisable();
//Modify contents pulled from the register
buf_read[1]&=clear_bits; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
buf_read[1]|=val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
val=buf_read[1];
//Write the updated byte to the register
spiEnable();
buf_read[0]=spi.write(write_address);
buf_read[1]=spi.write(val);
spiDisable();
return true;
}
//******************************************************************************
bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val)
{
//Write the updated byte to the register
spiEnable();
spi.write(write_address);
spi.write(val);
spiDisable();
return true;
}
//******************************************************************************
int8_t MAX31856::twosComplimentToSigned8(int8_t temp)
{
temp=(~(temp)+1); //Take two's complement of the negative number
temp|=(int8_t)(0x80UL); //And convert it into 7-bit val with msb as sign bit
return temp;
}
//******************************************************************************
int16_t MAX31856::twosComplimentToSigned16(int16_t temp)
{
temp=(~(temp)+1); //Take two's complement of the negative number
temp|=(int16_t)(0x8000UL); //And convert it into 15-bit val with msb as sign bit
return temp;
}
//******************************************************************************
MAX31856::~MAX31856(void)
{
//empty block
}
//bool MAX31856::checkForFaults() {
//
//}
///Define parameters for control register one (CR1)
/** Adding Samples increases the conversion time and reduces noise.
Typical conversion times:
1-shot or first conversion in Auto mode:
= t_Conversion + (samples-1)*33.33mS (60Hz rejection)
= t_Conversion + (samples-1)*40.00mS (50Hz rejection)
2 thru n conversions in Auto mode:
= t_Conversion + (samples-1)*16.67mS (60Hz rejection)
= t_Conversion + (samples-1)*20.00mS (50Hz rejection)
*/
//uint32_t MAX31856::calculateDelayTime() {
// uint32_t delayTime;
//
// if (auto_convert_mode==1 && samples==1) { //single conversion
// if (filter_mode==0) //60Hz
// delayTime=82+(samples-1)*33.33f;
// if (filter_mode==1) //50Hz
// delayTime=98+(samples-1)*40.00f;
// }
// else if (auto_convert_mode==1 && samples>1) { //single conversion
// if (filter_mode==0) //60Hz
// delayTime=82+(samples-1)*33.33f;
// if (filter_mode==1) //50Hz
// delayTime=98+(samples-1)*40.00f;
// }
//
//
//
//
//auto 50 169
//auto 60 143
//1shot 50 98
//1shot 60 82
