Y SI / lib_MAX31856

Dependents:   lib_MAX31856_example

Committer:
YSI
Date:
Tue Jun 30 09:21:19 2020 +0000
Revision:
0:9aa539a3d0f8
Child:
1:916d3fb3b21d
fix negative temperature

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:9aa539a3d0f8 1 /******************************************************************//**
YSI 0:9aa539a3d0f8 2 * @file lib_MAX31856.cpp
YSI 0:9aa539a3d0f8 3 *
YSI 0:9aa539a3d0f8 4 * @author Devin Alexander
YSI 0:9aa539a3d0f8 5 *
YSI 0:9aa539a3d0f8 6 * @version 1.0
YSI 0:9aa539a3d0f8 7 *
YSI 0:9aa539a3d0f8 8 * Started: SEPTEMBER 14th 2017
YSI 0:9aa539a3d0f8 9 *
YSI 0:9aa539a3d0f8 10 * Updated:
YSI 0:9aa539a3d0f8 11 *
YSI 0:9aa539a3d0f8 12 * @brief Source file for MAX3185 class
YSI 0:9aa539a3d0f8 13 *
YSI 0:9aa539a3d0f8 14 ***********************************************************************
YSI 0:9aa539a3d0f8 15 *
YSI 0:9aa539a3d0f8 16 * @copyright
YSI 0:9aa539a3d0f8 17 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
YSI 0:9aa539a3d0f8 18 *
YSI 0:9aa539a3d0f8 19 * Permission is hereby granted, free of charge, to any person obtaining a
YSI 0:9aa539a3d0f8 20 * copy of this software and associated documentation files (the "Software"),
YSI 0:9aa539a3d0f8 21 * to deal in the Software without restriction, including without limitation
YSI 0:9aa539a3d0f8 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
YSI 0:9aa539a3d0f8 23 * and/or sell copies of the Software, and to permit persons to whom the
YSI 0:9aa539a3d0f8 24 * Software is furnished to do so, subject to the following conditions:
YSI 0:9aa539a3d0f8 25 *
YSI 0:9aa539a3d0f8 26 * The above copyright notice and this permission notice shall be included
YSI 0:9aa539a3d0f8 27 * in all copies or substantial portions of the Software.
YSI 0:9aa539a3d0f8 28 *
YSI 0:9aa539a3d0f8 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
YSI 0:9aa539a3d0f8 30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
YSI 0:9aa539a3d0f8 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
YSI 0:9aa539a3d0f8 32 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
YSI 0:9aa539a3d0f8 33 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
YSI 0:9aa539a3d0f8 34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
YSI 0:9aa539a3d0f8 35 * OTHER DEALINGS IN THE SOFTWARE.
YSI 0:9aa539a3d0f8 36 *
YSI 0:9aa539a3d0f8 37 * Except as contained in this notice, the name of Maxim Integrated
YSI 0:9aa539a3d0f8 38 * Products, Inc. shall not be used except as stated in the Maxim Integrated
YSI 0:9aa539a3d0f8 39 * Products, Inc. Branding Policy.
YSI 0:9aa539a3d0f8 40 *
YSI 0:9aa539a3d0f8 41 * The mere transfer of this software does not imply any licenses
YSI 0:9aa539a3d0f8 42 * of trade secrets, proprietary technology, copyrights, patents,
YSI 0:9aa539a3d0f8 43 * trademarks, maskwork rights, or any other form of intellectual
YSI 0:9aa539a3d0f8 44 * property whatsoever. Maxim Integrated Products, Inc. retains all
YSI 0:9aa539a3d0f8 45 * ownership rights.
YSI 0:9aa539a3d0f8 46 **********************************************************************/
YSI 0:9aa539a3d0f8 47
YSI 0:9aa539a3d0f8 48 #include <mbed.h>
YSI 0:9aa539a3d0f8 49 #include "lib_MAX31856.h"
YSI 0:9aa539a3d0f8 50
YSI 0:9aa539a3d0f8 51 #define LOG(args...) printf(args)
YSI 0:9aa539a3d0f8 52
YSI 0:9aa539a3d0f8 53 //*****************************************************************************
YSI 0:9aa539a3d0f8 54 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) {
YSI 0:9aa539a3d0f8 55 spi.format(8,3); //configure the correct SPI mode to beable to program the registers intially correctly
YSI 0:9aa539a3d0f8 56 setThermocoupleType(_type);
YSI 0:9aa539a3d0f8 57 setEmiFilterFreq(_fltr);
YSI 0:9aa539a3d0f8 58 setNumSamplesAvg(_samples);
YSI 0:9aa539a3d0f8 59 setConversionMode(_conversion_mode);
YSI 0:9aa539a3d0f8 60 }
YSI 0:9aa539a3d0f8 61
YSI 0:9aa539a3d0f8 62
YSI 0:9aa539a3d0f8 63 //*****************************************************************************
YSI 0:9aa539a3d0f8 64 float MAX31856::readTC()
YSI 0:9aa539a3d0f8 65 {
YSI 0:9aa539a3d0f8 66 //Check and see if the MAX31856 is set to conversion mode ALWAYS ON
YSI 0:9aa539a3d0f8 67 if (conversion_mode==0) { //means that the conversion mode is normally off
YSI 0:9aa539a3d0f8 68 setOneShotMode(CR0_1_SHOT_MODE_ONE_CONVERSION); // turn on the one shot mode for singular conversion
YSI 0:9aa539a3d0f8 69 thermocouple_conversion_count=0; //reset the conversion count back to zero to make sure minimum conversion time reflects one shot mode requirements
YSI 0:9aa539a3d0f8 70 }
YSI 0:9aa539a3d0f8 71
YSI 0:9aa539a3d0f8 72 //calculate minimum wait time for conversions
YSI 0:9aa539a3d0f8 73 calculateDelayTime();
YSI 0:9aa539a3d0f8 74
YSI 0:9aa539a3d0f8 75 //initialize other info for the read functionality
YSI 0:9aa539a3d0f8 76 uint32_t buf_read[3], buf_write[3] = {ADDRESS_LTCBH_READ, ADDRESS_LTCBM_READ, ADDRESS_LTCBL_READ};
YSI 0:9aa539a3d0f8 77
YSI 0:9aa539a3d0f8 78 //bool read_thermocouple_temp = checkFaultsThermocoupleConnection(); //check and see if there are any faults that prohibit a normal read of the register
YSI 0:9aa539a3d0f8 79
YSI 0:9aa539a3d0f8 80 //if(read_thermocouple_temp) { //no faults with connection are present so continue on with normal read of temperature
YSI 0:9aa539a3d0f8 81 uint32_t time = us_ticker_read();
YSI 0:9aa539a3d0f8 82 uint32_t duration = time - lastReadTime;
YSI 0:9aa539a3d0f8 83 if (duration > conversion_time)
YSI 0:9aa539a3d0f8 84 {
YSI 0:9aa539a3d0f8 85 for(int i=0; i<3; i++) buf_read[i] = registerReadByte(buf_write[i]);
YSI 0:9aa539a3d0f8 86
YSI 0:9aa539a3d0f8 87 //Convert the registers contents into the correct value
YSI 0:9aa539a3d0f8 88 int32_t temp = ((buf_read[0] & 0xFF) << 0x18) + ((buf_read[1] & 0xFF) << 0x10) + ((buf_read[2] & 0xFF) << 0x08); // LTCBH + LTCBM + LTCBL
YSI 0:9aa539a3d0f8 89
YSI 0:9aa539a3d0f8 90 return prev_TC = (temp >> 0x0D) * 0.0078125;
YSI 0:9aa539a3d0f8 91 }
YSI 0:9aa539a3d0f8 92 //}
YSI 0:9aa539a3d0f8 93 thermocouple_conversion_count++; //iterate the conversion count to speed up time in between future converions in always on mode
YSI 0:9aa539a3d0f8 94
YSI 0:9aa539a3d0f8 95 //checkFaultsThermocoupleThresholds(); //print any faults to the terminal
YSI 0:9aa539a3d0f8 96
YSI 0:9aa539a3d0f8 97 return prev_TC;
YSI 0:9aa539a3d0f8 98 }
YSI 0:9aa539a3d0f8 99
YSI 0:9aa539a3d0f8 100
YSI 0:9aa539a3d0f8 101 //*****************************************************************************
YSI 0:9aa539a3d0f8 102 float MAX31856::readCJ()
YSI 0:9aa539a3d0f8 103 {
YSI 0:9aa539a3d0f8 104 uint16_t buf_read[3];
YSI 0:9aa539a3d0f8 105
YSI 0:9aa539a3d0f8 106 spiEnable();
YSI 0:9aa539a3d0f8 107 for(int i=0; i<3; i++) buf_read[i]=spi.write(ADDRESS_CJTH_READ);
YSI 0:9aa539a3d0f8 108 spiDisable();
YSI 0:9aa539a3d0f8 109
YSI 0:9aa539a3d0f8 110 //Convert the registers contents into the correct value
YSI 0:9aa539a3d0f8 111 int16_t temp = ((buf_read[1] & 0xFF) << 8) + (buf_read[2] & 0xFF); // CJTH + CJTL
YSI 0:9aa539a3d0f8 112
YSI 0:9aa539a3d0f8 113 //checkFaultsColdJunctionThresholds(); //print any faults to the terminal
YSI 0:9aa539a3d0f8 114
YSI 0:9aa539a3d0f8 115 return ((temp==0)?NAN:(temp/256.0));
YSI 0:9aa539a3d0f8 116 }
YSI 0:9aa539a3d0f8 117
YSI 0:9aa539a3d0f8 118 //*****************************************************************************
YSI 0:9aa539a3d0f8 119 uint8_t MAX31856::checkFaultsThermocoupleThresholds()
YSI 0:9aa539a3d0f8 120 {
YSI 0:9aa539a3d0f8 121 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 122 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 123 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 124 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 125
YSI 0:9aa539a3d0f8 126 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 127 if ((fault_byte&0x4C)==0) //means no fault is detected for thermocouple thresholds
YSI 0:9aa539a3d0f8 128 return_int=0;
YSI 0:9aa539a3d0f8 129 else {
YSI 0:9aa539a3d0f8 130 if ((fault_byte&0x40)==0) { //check if normal operation of thermocouple is true
YSI 0:9aa539a3d0f8 131 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 132 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 133 return_int=1;
YSI 0:9aa539a3d0f8 134 }
YSI 0:9aa539a3d0f8 135 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 136 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 137 return_int=2;
YSI 0:9aa539a3d0f8 138 }
YSI 0:9aa539a3d0f8 139 }
YSI 0:9aa539a3d0f8 140 else { //Thermocouples is operating outside of normal range
YSI 0:9aa539a3d0f8 141 LOG("FAULT! Thermocouple temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 142 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 143 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 144 return_int=4;
YSI 0:9aa539a3d0f8 145 }
YSI 0:9aa539a3d0f8 146 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 147 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 148 return_int=5;
YSI 0:9aa539a3d0f8 149 }
YSI 0:9aa539a3d0f8 150 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 151 return_int=3;
YSI 0:9aa539a3d0f8 152 }
YSI 0:9aa539a3d0f8 153 }
YSI 0:9aa539a3d0f8 154 return return_int;
YSI 0:9aa539a3d0f8 155 }
YSI 0:9aa539a3d0f8 156
YSI 0:9aa539a3d0f8 157 //*****************************************************************************
YSI 0:9aa539a3d0f8 158 uint8_t MAX31856::checkFaultsColdJunctionThresholds()
YSI 0:9aa539a3d0f8 159 {
YSI 0:9aa539a3d0f8 160 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 161 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 162 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 163 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 164
YSI 0:9aa539a3d0f8 165 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 166 if ((fault_byte&0xB0)==0) //means no fault is detected for cold junction thresholds
YSI 0:9aa539a3d0f8 167 return_int=0;
YSI 0:9aa539a3d0f8 168 else {
YSI 0:9aa539a3d0f8 169 if ((fault_byte&0x80)==0) { //check if normal operation of cold junction is true
YSI 0:9aa539a3d0f8 170 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 171 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 172 return_int=1;
YSI 0:9aa539a3d0f8 173 }
YSI 0:9aa539a3d0f8 174 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 175 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 176 return_int=2;
YSI 0:9aa539a3d0f8 177 }
YSI 0:9aa539a3d0f8 178 }
YSI 0:9aa539a3d0f8 179 else { //Cold Junction is operating outside of normal range
YSI 0:9aa539a3d0f8 180 LOG("FAULT! Cold Junction temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 181 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 182 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 183 return_int=4;
YSI 0:9aa539a3d0f8 184 }
YSI 0:9aa539a3d0f8 185 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 186 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 187 return_int=5;
YSI 0:9aa539a3d0f8 188 }
YSI 0:9aa539a3d0f8 189 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 190 return_int=3;
YSI 0:9aa539a3d0f8 191 }
YSI 0:9aa539a3d0f8 192 }
YSI 0:9aa539a3d0f8 193 return return_int;
YSI 0:9aa539a3d0f8 194 }
YSI 0:9aa539a3d0f8 195
YSI 0:9aa539a3d0f8 196 //*****************************************************************************
YSI 0:9aa539a3d0f8 197 bool MAX31856::checkFaultsThermocoupleConnection()
YSI 0:9aa539a3d0f8 198 {
YSI 0:9aa539a3d0f8 199 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 200 uint8_t temp[2];
YSI 0:9aa539a3d0f8 201 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 202 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 203
YSI 0:9aa539a3d0f8 204 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 205 if (fault_byte==0) //means no fault is detected
YSI 0:9aa539a3d0f8 206 return_val=1;
YSI 0:9aa539a3d0f8 207 else{
YSI 0:9aa539a3d0f8 208 if (temp[0]&0x02) {
YSI 0:9aa539a3d0f8 209 LOG("Overvotage/Undervoltage Fault triggered! Input voltage is negative or the voltage is greater than Vdd! Please check thermocouple connection!\r\n");
YSI 0:9aa539a3d0f8 210 return_val=0;
YSI 0:9aa539a3d0f8 211 }
YSI 0:9aa539a3d0f8 212 if (temp[1]&0x01) {
YSI 0:9aa539a3d0f8 213 LOG("Open circuit fault detected! Please check thermocouple connection!\r\n");
YSI 0:9aa539a3d0f8 214 return_val=0;
YSI 0:9aa539a3d0f8 215 }
YSI 0:9aa539a3d0f8 216 }
YSI 0:9aa539a3d0f8 217 return return_val;
YSI 0:9aa539a3d0f8 218 }
YSI 0:9aa539a3d0f8 219
YSI 0:9aa539a3d0f8 220
YSI 0:9aa539a3d0f8 221 //Register:CR0 Bits: 7
YSI 0:9aa539a3d0f8 222 //*****************************************************************************
YSI 0:9aa539a3d0f8 223 bool MAX31856::setConversionMode(uint8_t val)
YSI 0:9aa539a3d0f8 224 {
YSI 0:9aa539a3d0f8 225 if (val==CR0_CONV_MODE_NORMALLY_OFF) {
YSI 0:9aa539a3d0f8 226 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
YSI 0:9aa539a3d0f8 227 conversion_mode=0;
YSI 0:9aa539a3d0f8 228 //LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_OFF\r\n");
YSI 0:9aa539a3d0f8 229 }
YSI 0:9aa539a3d0f8 230 else if (val==CR0_CONV_MODE_NORMALLY_ON) {
YSI 0:9aa539a3d0f8 231 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
YSI 0:9aa539a3d0f8 232 conversion_mode=1;
YSI 0:9aa539a3d0f8 233 //LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_ON\r\n");
YSI 0:9aa539a3d0f8 234 }
YSI 0:9aa539a3d0f8 235 else {
YSI 0:9aa539a3d0f8 236 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");
YSI 0:9aa539a3d0f8 237 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 238 }
YSI 0:9aa539a3d0f8 239 return return_val;
YSI 0:9aa539a3d0f8 240 }
YSI 0:9aa539a3d0f8 241
YSI 0:9aa539a3d0f8 242
YSI 0:9aa539a3d0f8 243 //Register:CR0 Bits: 6
YSI 0:9aa539a3d0f8 244 //*****************************************************************************
YSI 0:9aa539a3d0f8 245 bool MAX31856::setOneShotMode(uint8_t val)
YSI 0:9aa539a3d0f8 246 {
YSI 0:9aa539a3d0f8 247 if (val==CR0_1_SHOT_MODE_NO_CONVERSION)
YSI 0:9aa539a3d0f8 248 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
YSI 0:9aa539a3d0f8 249 else if (val==CR0_1_SHOT_MODE_ONE_CONVERSION)
YSI 0:9aa539a3d0f8 250 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
YSI 0:9aa539a3d0f8 251 else {
YSI 0:9aa539a3d0f8 252 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");
YSI 0:9aa539a3d0f8 253 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 254 }
YSI 0:9aa539a3d0f8 255 return return_val;
YSI 0:9aa539a3d0f8 256 }
YSI 0:9aa539a3d0f8 257
YSI 0:9aa539a3d0f8 258
YSI 0:9aa539a3d0f8 259 //Register:CR0 Bits: 5:4
YSI 0:9aa539a3d0f8 260 //*****************************************************************************
YSI 0:9aa539a3d0f8 261 bool MAX31856::setOpenCircuitFaultDetection(uint8_t val)
YSI 0:9aa539a3d0f8 262 {
YSI 0:9aa539a3d0f8 263 if (val==CR0_OC_DETECT_DISABLED) {
YSI 0:9aa539a3d0f8 264 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 265 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_DISABLED\r\n");
YSI 0:9aa539a3d0f8 266 }
YSI 0:9aa539a3d0f8 267 else if (val==CR0_OC_DETECT_ENABLED_R_LESS_5k) {
YSI 0:9aa539a3d0f8 268 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 269 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_R_LESS_5k\r\n");
YSI 0:9aa539a3d0f8 270 }
YSI 0:9aa539a3d0f8 271 else if (val==CR0_OC_DETECT_ENABLED_TC_LESS_2ms) {
YSI 0:9aa539a3d0f8 272 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 273 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_LESS_2ms\r\n");
YSI 0:9aa539a3d0f8 274 }
YSI 0:9aa539a3d0f8 275 else if (val==CR0_OC_DETECT_ENABLED_TC_MORE_2ms) {
YSI 0:9aa539a3d0f8 276 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 277 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_MORE_2ms\r\n");
YSI 0:9aa539a3d0f8 278 }
YSI 0:9aa539a3d0f8 279 else {
YSI 0:9aa539a3d0f8 280 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");
YSI 0:9aa539a3d0f8 281 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 282 }
YSI 0:9aa539a3d0f8 283 return return_val;
YSI 0:9aa539a3d0f8 284 }
YSI 0:9aa539a3d0f8 285
YSI 0:9aa539a3d0f8 286
YSI 0:9aa539a3d0f8 287 //Register:CR0 Bits: 3
YSI 0:9aa539a3d0f8 288 //*****************************************************************************
YSI 0:9aa539a3d0f8 289 bool MAX31856::setColdJunctionDisable(uint8_t val)
YSI 0:9aa539a3d0f8 290 {
YSI 0:9aa539a3d0f8 291 if (val==CR0_COLD_JUNC_ENABLE) {
YSI 0:9aa539a3d0f8 292 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 293 cold_junction_enabled=1;
YSI 0:9aa539a3d0f8 294 LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_ENABLE\r\n");
YSI 0:9aa539a3d0f8 295 }
YSI 0:9aa539a3d0f8 296 else if (val==CR0_COLD_JUNC_DISABLE) {
YSI 0:9aa539a3d0f8 297 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 298 cold_junction_enabled=0;
YSI 0:9aa539a3d0f8 299 LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_DISABLE\r\n");
YSI 0:9aa539a3d0f8 300 }
YSI 0:9aa539a3d0f8 301 else {
YSI 0:9aa539a3d0f8 302 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");
YSI 0:9aa539a3d0f8 303 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 304 }
YSI 0:9aa539a3d0f8 305 return return_val;
YSI 0:9aa539a3d0f8 306 }
YSI 0:9aa539a3d0f8 307
YSI 0:9aa539a3d0f8 308
YSI 0:9aa539a3d0f8 309 //Register:CR0 Bits: 2
YSI 0:9aa539a3d0f8 310 //*****************************************************************************
YSI 0:9aa539a3d0f8 311 bool MAX31856::setFaultMode(uint8_t val)
YSI 0:9aa539a3d0f8 312 {
YSI 0:9aa539a3d0f8 313 if (val==CR0_FAULT_MODE_COMPARATOR) {
YSI 0:9aa539a3d0f8 314 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 315 LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_COMPARATOR\r\n");
YSI 0:9aa539a3d0f8 316 }
YSI 0:9aa539a3d0f8 317 else if (val==CR0_FAULT_MODE_INTERUPT) {
YSI 0:9aa539a3d0f8 318 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 319 LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_INTERUPT\r\n");
YSI 0:9aa539a3d0f8 320 }
YSI 0:9aa539a3d0f8 321 else {
YSI 0:9aa539a3d0f8 322 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");
YSI 0:9aa539a3d0f8 323 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 324 }
YSI 0:9aa539a3d0f8 325 return return_val;
YSI 0:9aa539a3d0f8 326 }
YSI 0:9aa539a3d0f8 327
YSI 0:9aa539a3d0f8 328
YSI 0:9aa539a3d0f8 329 //Register:CR0 Bits: 1
YSI 0:9aa539a3d0f8 330 //*****************************************************************************
YSI 0:9aa539a3d0f8 331 bool MAX31856::setFaultStatusClear(uint8_t val)
YSI 0:9aa539a3d0f8 332 {
YSI 0:9aa539a3d0f8 333 if (val==CR0_FAULTCLR_DEFAULT_VAL) {
YSI 0:9aa539a3d0f8 334 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 335 LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_DEFAULT_VAL\r\n");
YSI 0:9aa539a3d0f8 336 }
YSI 0:9aa539a3d0f8 337 else if (val==CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO) {
YSI 0:9aa539a3d0f8 338 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 339 LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_RETURN_FAULTS_TO_ZERO\r\n");
YSI 0:9aa539a3d0f8 340 }
YSI 0:9aa539a3d0f8 341 else {
YSI 0:9aa539a3d0f8 342 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");
YSI 0:9aa539a3d0f8 343 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 344 }
YSI 0:9aa539a3d0f8 345 return return_val;
YSI 0:9aa539a3d0f8 346 }
YSI 0:9aa539a3d0f8 347
YSI 0:9aa539a3d0f8 348
YSI 0:9aa539a3d0f8 349 //Register:CR0 Bits: 0
YSI 0:9aa539a3d0f8 350 //*****************************************************************************
YSI 0:9aa539a3d0f8 351 bool MAX31856::setEmiFilterFreq(uint8_t val)
YSI 0:9aa539a3d0f8 352 {
YSI 0:9aa539a3d0f8 353 if (val==CR0_FILTER_OUT_60Hz) {
YSI 0:9aa539a3d0f8 354 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 355 filter_mode=0;
YSI 0:9aa539a3d0f8 356 //LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_60Hz\r\n");
YSI 0:9aa539a3d0f8 357 }
YSI 0:9aa539a3d0f8 358 else if (val==CR0_FILTER_OUT_50Hz) {
YSI 0:9aa539a3d0f8 359 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 360 filter_mode=1;
YSI 0:9aa539a3d0f8 361 //LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_50Hz\r\n");
YSI 0:9aa539a3d0f8 362 }
YSI 0:9aa539a3d0f8 363 else {
YSI 0:9aa539a3d0f8 364 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");
YSI 0:9aa539a3d0f8 365 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 366 }
YSI 0:9aa539a3d0f8 367 return return_val;
YSI 0:9aa539a3d0f8 368 }
YSI 0:9aa539a3d0f8 369
YSI 0:9aa539a3d0f8 370
YSI 0:9aa539a3d0f8 371 //Register:CR1 Bits: 6:4
YSI 0:9aa539a3d0f8 372 //*****************************************************************************
YSI 0:9aa539a3d0f8 373 bool MAX31856::setNumSamplesAvg(uint8_t val)
YSI 0:9aa539a3d0f8 374 {
YSI 0:9aa539a3d0f8 375 if (val==CR1_AVG_TC_SAMPLES_1) {
YSI 0:9aa539a3d0f8 376 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 377 samples=1;
YSI 0:9aa539a3d0f8 378 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_1\r\n");
YSI 0:9aa539a3d0f8 379 }
YSI 0:9aa539a3d0f8 380 else if (val==CR1_AVG_TC_SAMPLES_2) {
YSI 0:9aa539a3d0f8 381 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 382 samples=2;
YSI 0:9aa539a3d0f8 383 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_2\r\n");
YSI 0:9aa539a3d0f8 384 }
YSI 0:9aa539a3d0f8 385 else if (val==CR1_AVG_TC_SAMPLES_4) {
YSI 0:9aa539a3d0f8 386 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 387 samples=4;
YSI 0:9aa539a3d0f8 388 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_4\r\n");
YSI 0:9aa539a3d0f8 389 }
YSI 0:9aa539a3d0f8 390 else if (val==CR1_AVG_TC_SAMPLES_8) {
YSI 0:9aa539a3d0f8 391 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 392 samples=8;
YSI 0:9aa539a3d0f8 393 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_8\r\n");
YSI 0:9aa539a3d0f8 394 }
YSI 0:9aa539a3d0f8 395 else if (val==CR1_AVG_TC_SAMPLES_16) {
YSI 0:9aa539a3d0f8 396 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 397 samples=16;
YSI 0:9aa539a3d0f8 398 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_16\r\n");
YSI 0:9aa539a3d0f8 399 }
YSI 0:9aa539a3d0f8 400 else {
YSI 0:9aa539a3d0f8 401 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");
YSI 0:9aa539a3d0f8 402 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 403 }
YSI 0:9aa539a3d0f8 404 return return_val;
YSI 0:9aa539a3d0f8 405 }
YSI 0:9aa539a3d0f8 406
YSI 0:9aa539a3d0f8 407
YSI 0:9aa539a3d0f8 408 //Register:CR1 Bits: 3:0
YSI 0:9aa539a3d0f8 409 //*****************************************************************************
YSI 0:9aa539a3d0f8 410 bool MAX31856::setThermocoupleType(uint8_t val)
YSI 0:9aa539a3d0f8 411 {
YSI 0:9aa539a3d0f8 412 if (val==CR1_TC_TYPE_B) {
YSI 0:9aa539a3d0f8 413 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 414 voltage_mode=false;
YSI 0:9aa539a3d0f8 415 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_B\r\n");
YSI 0:9aa539a3d0f8 416 }
YSI 0:9aa539a3d0f8 417 else if (val==CR1_TC_TYPE_E) {
YSI 0:9aa539a3d0f8 418 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 419 voltage_mode=false;
YSI 0:9aa539a3d0f8 420 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_E\r\n");
YSI 0:9aa539a3d0f8 421 }
YSI 0:9aa539a3d0f8 422 else if (val==CR1_TC_TYPE_J) {
YSI 0:9aa539a3d0f8 423 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 424 voltage_mode=false;
YSI 0:9aa539a3d0f8 425 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_J\r\n");
YSI 0:9aa539a3d0f8 426 }
YSI 0:9aa539a3d0f8 427 else if (val==CR1_TC_TYPE_K) {
YSI 0:9aa539a3d0f8 428 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 429 voltage_mode=false;
YSI 0:9aa539a3d0f8 430 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_K\r\n");
YSI 0:9aa539a3d0f8 431 }
YSI 0:9aa539a3d0f8 432 else if (val==CR1_TC_TYPE_N) {
YSI 0:9aa539a3d0f8 433 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 434 voltage_mode=false;
YSI 0:9aa539a3d0f8 435 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_N\r\n");
YSI 0:9aa539a3d0f8 436 }
YSI 0:9aa539a3d0f8 437 else if (val==CR1_TC_TYPE_R) {
YSI 0:9aa539a3d0f8 438 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 439 voltage_mode=false;
YSI 0:9aa539a3d0f8 440 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_R\r\n");
YSI 0:9aa539a3d0f8 441 }
YSI 0:9aa539a3d0f8 442 else if (val==CR1_TC_TYPE_S) {
YSI 0:9aa539a3d0f8 443 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 444 voltage_mode=false;
YSI 0:9aa539a3d0f8 445 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_S\r\n");
YSI 0:9aa539a3d0f8 446 }
YSI 0:9aa539a3d0f8 447 else if (val==CR1_TC_TYPE_T) {
YSI 0:9aa539a3d0f8 448 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 449 voltage_mode=false;
YSI 0:9aa539a3d0f8 450 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_T\r\n");
YSI 0:9aa539a3d0f8 451 }
YSI 0:9aa539a3d0f8 452 else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_8) {
YSI 0:9aa539a3d0f8 453 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 454 voltage_mode=true;
YSI 0:9aa539a3d0f8 455 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_8\r\n");
YSI 0:9aa539a3d0f8 456 }
YSI 0:9aa539a3d0f8 457 else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_32) {
YSI 0:9aa539a3d0f8 458 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 459 voltage_mode=true;
YSI 0:9aa539a3d0f8 460 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_32\r\n");
YSI 0:9aa539a3d0f8 461 }
YSI 0:9aa539a3d0f8 462 else {
YSI 0:9aa539a3d0f8 463 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");
YSI 0:9aa539a3d0f8 464 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 465 }
YSI 0:9aa539a3d0f8 466 return return_val;
YSI 0:9aa539a3d0f8 467 }
YSI 0:9aa539a3d0f8 468
YSI 0:9aa539a3d0f8 469
YSI 0:9aa539a3d0f8 470 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 471 //*****************************************************************************
YSI 0:9aa539a3d0f8 472 bool MAX31856::setFaultMasks(uint8_t val, bool enable)
YSI 0:9aa539a3d0f8 473 {
YSI 0:9aa539a3d0f8 474 if(enable)
YSI 0:9aa539a3d0f8 475 val=0;
YSI 0:9aa539a3d0f8 476 if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
YSI 0:9aa539a3d0f8 477 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_5, val);
YSI 0:9aa539a3d0f8 478 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_HIGH\r\n");
YSI 0:9aa539a3d0f8 479 }
YSI 0:9aa539a3d0f8 480 else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 481 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_4, val);
YSI 0:9aa539a3d0f8 482 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_LOW\r\n");
YSI 0:9aa539a3d0f8 483 }
YSI 0:9aa539a3d0f8 484 else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
YSI 0:9aa539a3d0f8 485 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 486 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_HIGH\r\n");
YSI 0:9aa539a3d0f8 487 }
YSI 0:9aa539a3d0f8 488 else if (val==MASK_TC_FAULT_THRESHOLD_LOW) { //Thermocouple Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 489 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 490 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_LOW\r\n");
YSI 0:9aa539a3d0f8 491 }
YSI 0:9aa539a3d0f8 492 else if (val==MASK_OVER_UNDER_VOLT_FAULT) { //Over-Voltage/Under-Voltage Input Fault Mask
YSI 0:9aa539a3d0f8 493 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 494 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OVER_UNDER_VOLT_FAULT\r\n");
YSI 0:9aa539a3d0f8 495 }
YSI 0:9aa539a3d0f8 496 else if (val==MASK_OPEN_CIRCUIT_FAULT) { //Thermocouple Open-Circuit Fault Mask
YSI 0:9aa539a3d0f8 497 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 498 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OPEN_CIRCUIT_FAULT\r\n");
YSI 0:9aa539a3d0f8 499 }
YSI 0:9aa539a3d0f8 500 else {
YSI 0:9aa539a3d0f8 501 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");
YSI 0:9aa539a3d0f8 502 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 503 }
YSI 0:9aa539a3d0f8 504 return return_val;
YSI 0:9aa539a3d0f8 505 }
YSI 0:9aa539a3d0f8 506
YSI 0:9aa539a3d0f8 507
YSI 0:9aa539a3d0f8 508 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 509 //******************************************************************************
YSI 0:9aa539a3d0f8 510 bool MAX31856::setFaultThresholds(uint8_t val, float temperature)
YSI 0:9aa539a3d0f8 511 {
YSI 0:9aa539a3d0f8 512 if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
YSI 0:9aa539a3d0f8 513 int8_t temperature_byte=temperature;
YSI 0:9aa539a3d0f8 514 return_val=registerWriteByte(ADDRESS_CJHF_WRITE, temperature_byte);
YSI 0:9aa539a3d0f8 515 }
YSI 0:9aa539a3d0f8 516 else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 517 int8_t temperature_byte=temperature;
YSI 0:9aa539a3d0f8 518 return_val=registerWriteByte(ADDRESS_CJLF_WRITE, temperature_byte);
YSI 0:9aa539a3d0f8 519 }
YSI 0:9aa539a3d0f8 520 else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
YSI 0:9aa539a3d0f8 521 int8_t temperature_byte[2];
YSI 0:9aa539a3d0f8 522 int16_t temperature_multi_byte =temperature*4.0;
YSI 0:9aa539a3d0f8 523 //now split up the 16bit int into two bytes to program the registers with
YSI 0:9aa539a3d0f8 524 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 0:9aa539a3d0f8 525 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 0:9aa539a3d0f8 526
YSI 0:9aa539a3d0f8 527 return_val=registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]);
YSI 0:9aa539a3d0f8 528 return_val=registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);
YSI 0:9aa539a3d0f8 529 }
YSI 0:9aa539a3d0f8 530 else if (val==MASK_TC_FAULT_THRESHOLD_LOW) { //Thermocouple LOW Threshold Fault Mask
YSI 0:9aa539a3d0f8 531 int8_t temperature_byte[2];
YSI 0:9aa539a3d0f8 532 int16_t temperature_multi_byte =temperature*4.0;
YSI 0:9aa539a3d0f8 533 //now split up the 16bit int into two bytes to program the registers with
YSI 0:9aa539a3d0f8 534 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 0:9aa539a3d0f8 535 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 0:9aa539a3d0f8 536
YSI 0:9aa539a3d0f8 537 return_val=registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]);
YSI 0:9aa539a3d0f8 538 return_val=registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);
YSI 0:9aa539a3d0f8 539 }
YSI 0:9aa539a3d0f8 540 else
YSI 0:9aa539a3d0f8 541 LOG("Please select correct threshold register to program with the correct value!\r\n");
YSI 0:9aa539a3d0f8 542 return return_val;
YSI 0:9aa539a3d0f8 543 }
YSI 0:9aa539a3d0f8 544
YSI 0:9aa539a3d0f8 545 //******************************************************************************
YSI 0:9aa539a3d0f8 546 bool MAX31856::coldJunctionOffset(float temperature)
YSI 0:9aa539a3d0f8 547 {
YSI 0:9aa539a3d0f8 548 if (temperature > 7.9375 || temperature < -8.0) {
YSI 0:9aa539a3d0f8 549 LOG("Input value to offest the cold junction point is non valid. enter in value in range -8 to +7.9375\r\n");
YSI 0:9aa539a3d0f8 550 return_val = 0;
YSI 0:9aa539a3d0f8 551 }
YSI 0:9aa539a3d0f8 552 int8_t temp_val=temperature*16.0f; //normalize the value to get rid of decimal and shorten it to size of register
YSI 0:9aa539a3d0f8 553 return_val=registerWriteByte(ADDRESS_CJTO_WRITE, temp_val); //write the byte to cold junction offset register
YSI 0:9aa539a3d0f8 554 return return_val;
YSI 0:9aa539a3d0f8 555 }
YSI 0:9aa539a3d0f8 556
YSI 0:9aa539a3d0f8 557
YSI 0:9aa539a3d0f8 558 //The following functions are for internal library use only
YSI 0:9aa539a3d0f8 559 //******************************************************************************
YSI 0:9aa539a3d0f8 560 void MAX31856::spiEnable()
YSI 0:9aa539a3d0f8 561 {
YSI 0:9aa539a3d0f8 562 ncs=0; //Set CS low to start transmission (interrupts conversion)
YSI 0:9aa539a3d0f8 563 return;
YSI 0:9aa539a3d0f8 564 }
YSI 0:9aa539a3d0f8 565
YSI 0:9aa539a3d0f8 566
YSI 0:9aa539a3d0f8 567 //******************************************************************************
YSI 0:9aa539a3d0f8 568 void MAX31856::spiDisable()
YSI 0:9aa539a3d0f8 569 {
YSI 0:9aa539a3d0f8 570 ncs=1; //Set CS high to stop transmission (restarts conversion)
YSI 0:9aa539a3d0f8 571 return;
YSI 0:9aa539a3d0f8 572 }
YSI 0:9aa539a3d0f8 573
YSI 0:9aa539a3d0f8 574
YSI 0:9aa539a3d0f8 575 //******************************************************************************
YSI 0:9aa539a3d0f8 576 bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, int clear_bits, uint8_t val)
YSI 0:9aa539a3d0f8 577 {
YSI 0:9aa539a3d0f8 578 uint8_t buf_read[2];
YSI 0:9aa539a3d0f8 579
YSI 0:9aa539a3d0f8 580 //Read the current contents of a register
YSI 0:9aa539a3d0f8 581 spiEnable();
YSI 0:9aa539a3d0f8 582 for(int i=0; i<2; i++) {
YSI 0:9aa539a3d0f8 583 buf_read[i]=spi.write(read_address);
YSI 0:9aa539a3d0f8 584 }
YSI 0:9aa539a3d0f8 585 spiDisable();
YSI 0:9aa539a3d0f8 586
YSI 0:9aa539a3d0f8 587 //Modify contents pulled from the register
YSI 0:9aa539a3d0f8 588 buf_read[1]&=clear_bits; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
YSI 0:9aa539a3d0f8 589 buf_read[1]|=val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
YSI 0:9aa539a3d0f8 590 val=buf_read[1];
YSI 0:9aa539a3d0f8 591
YSI 0:9aa539a3d0f8 592 //Write the updated byte to the register
YSI 0:9aa539a3d0f8 593 spiEnable();
YSI 0:9aa539a3d0f8 594 buf_read[0]=spi.write(write_address);
YSI 0:9aa539a3d0f8 595 buf_read[1]=spi.write(val);
YSI 0:9aa539a3d0f8 596 spiDisable();
YSI 0:9aa539a3d0f8 597 return 1;
YSI 0:9aa539a3d0f8 598 }
YSI 0:9aa539a3d0f8 599
YSI 0:9aa539a3d0f8 600
YSI 0:9aa539a3d0f8 601 //******************************************************************************
YSI 0:9aa539a3d0f8 602 bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val)
YSI 0:9aa539a3d0f8 603 {
YSI 0:9aa539a3d0f8 604 //Write the updated byte to the register
YSI 0:9aa539a3d0f8 605 spiEnable();
YSI 0:9aa539a3d0f8 606 spi.write(write_address);
YSI 0:9aa539a3d0f8 607 spi.write(val);
YSI 0:9aa539a3d0f8 608 spiDisable();
YSI 0:9aa539a3d0f8 609 return true;
YSI 0:9aa539a3d0f8 610 }
YSI 0:9aa539a3d0f8 611
YSI 0:9aa539a3d0f8 612 //******************************************************************************
YSI 0:9aa539a3d0f8 613 uint8_t MAX31856::registerReadByte(uint8_t read_address)
YSI 0:9aa539a3d0f8 614 {
YSI 0:9aa539a3d0f8 615 spiEnable();
YSI 0:9aa539a3d0f8 616 spi.write(read_address);
YSI 0:9aa539a3d0f8 617 uint8_t buf_read=spi.write(0);
YSI 0:9aa539a3d0f8 618 spiDisable();
YSI 0:9aa539a3d0f8 619 return buf_read;
YSI 0:9aa539a3d0f8 620 }
YSI 0:9aa539a3d0f8 621
YSI 0:9aa539a3d0f8 622 //******************************************************************************
YSI 0:9aa539a3d0f8 623 void MAX31856::calculateDelayTime() {
YSI 0:9aa539a3d0f8 624 uint32_t temp_int;
YSI 0:9aa539a3d0f8 625
YSI 0:9aa539a3d0f8 626 if (conversion_mode==0 || thermocouple_conversion_count==0) {
YSI 0:9aa539a3d0f8 627 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 628 temp_int=82+(samples-1)*33.33f;
YSI 0:9aa539a3d0f8 629 else //50Hz
YSI 0:9aa539a3d0f8 630 temp_int=98+(samples-1)*40.00f;
YSI 0:9aa539a3d0f8 631 }
YSI 0:9aa539a3d0f8 632 else {
YSI 0:9aa539a3d0f8 633 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 634 temp_int=82+(samples-1)*16.67f;
YSI 0:9aa539a3d0f8 635 else //50Hz
YSI 0:9aa539a3d0f8 636 temp_int=98+(samples-1)*20.00f;
YSI 0:9aa539a3d0f8 637 }
YSI 0:9aa539a3d0f8 638
YSI 0:9aa539a3d0f8 639 if (cold_junction_enabled==0) //cold junction is disabled enabling 25 millisecond faster conversion times
YSI 0:9aa539a3d0f8 640 temp_int=temp_int-25;
YSI 0:9aa539a3d0f8 641 conversion_time=1000*temp_int; //set private member conversion time to calculated minimum wait time in microseconds
YSI 0:9aa539a3d0f8 642 return;
YSI 0:9aa539a3d0f8 643 }
YSI 0:9aa539a3d0f8 644
YSI 0:9aa539a3d0f8 645 //*****************************************************************************
YSI 0:9aa539a3d0f8 646 MAX31856::~MAX31856(void)
YSI 0:9aa539a3d0f8 647 {
YSI 0:9aa539a3d0f8 648 //empty block
YSI 0:9aa539a3d0f8 649 }