Y SI / lib_MAX31856

Dependents:   lib_MAX31856_example

Committer:
YSI
Date:
Tue Oct 13 11:57:18 2020 +0000
Revision:
2:c5e7f83a00ed
Parent:
1:916d3fb3b21d
Child:
3:9fb5fcf05ac3
add returns from MAX31856

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 2:c5e7f83a00ed 56 init_MAX31856 &= setThermocoupleType(_type);
YSI 2:c5e7f83a00ed 57 init_MAX31856 &= setEmiFilterFreq(_fltr);
YSI 2:c5e7f83a00ed 58 init_MAX31856 &= setNumSamplesAvg(_samples);
YSI 2:c5e7f83a00ed 59 init_MAX31856 &= 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 1:916d3fb3b21d 65 {
YSI 2:c5e7f83a00ed 66 if(!init_MAX31856) return NAN;
YSI 0:9aa539a3d0f8 67 //Check and see if the MAX31856 is set to conversion mode ALWAYS ON
YSI 0:9aa539a3d0f8 68 if (conversion_mode==0) { //means that the conversion mode is normally off
YSI 0:9aa539a3d0f8 69 setOneShotMode(CR0_1_SHOT_MODE_ONE_CONVERSION); // turn on the one shot mode for singular conversion
YSI 0:9aa539a3d0f8 70 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 71 }
YSI 0:9aa539a3d0f8 72
YSI 0:9aa539a3d0f8 73 //calculate minimum wait time for conversions
YSI 0:9aa539a3d0f8 74 calculateDelayTime();
YSI 0:9aa539a3d0f8 75
YSI 0:9aa539a3d0f8 76 //initialize other info for the read functionality
YSI 2:c5e7f83a00ed 77 uint32_t buf_read[3] = {0}, buf_write[3] = {ADDRESS_LTCBH_READ, ADDRESS_LTCBM_READ, ADDRESS_LTCBL_READ};
YSI 0:9aa539a3d0f8 78
YSI 0:9aa539a3d0f8 79 //bool read_thermocouple_temp = checkFaultsThermocoupleConnection(); //check and see if there are any faults that prohibit a normal read of the register
YSI 0:9aa539a3d0f8 80
YSI 0:9aa539a3d0f8 81 //if(read_thermocouple_temp) { //no faults with connection are present so continue on with normal read of temperature
YSI 0:9aa539a3d0f8 82 uint32_t time = us_ticker_read();
YSI 0:9aa539a3d0f8 83 uint32_t duration = time - lastReadTime;
YSI 0:9aa539a3d0f8 84 if (duration > conversion_time)
YSI 0:9aa539a3d0f8 85 {
YSI 0:9aa539a3d0f8 86 for(int i=0; i<3; i++) buf_read[i] = registerReadByte(buf_write[i]);
YSI 0:9aa539a3d0f8 87
YSI 0:9aa539a3d0f8 88 //Convert the registers contents into the correct value
YSI 2:c5e7f83a00ed 89 //int32_t temp = ((buf_read[0] & 0xFF) << 0x18) + ((buf_read[1] & 0xFF) << 0x10) + ((buf_read[2] & 0xFF) << 0x08); // LTCBH + LTCBM + LTCBL
YSI 2:c5e7f83a00ed 90 int32_t temp = ((buf_read[0] & 0xFF) << 11) + ((buf_read[1] & 0xFF) << 3) + ((buf_read[2] & 0xFF) >> 5); // LTCBH + LTCBM + LTCBL
YSI 0:9aa539a3d0f8 91
YSI 2:c5e7f83a00ed 92 //return prev_TC = (temp >> 0x0D) * 0.0078125;
YSI 2:c5e7f83a00ed 93 return prev_TC = temp * 0.0078125;
YSI 0:9aa539a3d0f8 94 }
YSI 0:9aa539a3d0f8 95 //}
YSI 0:9aa539a3d0f8 96 thermocouple_conversion_count++; //iterate the conversion count to speed up time in between future converions in always on mode
YSI 0:9aa539a3d0f8 97
YSI 0:9aa539a3d0f8 98 //checkFaultsThermocoupleThresholds(); //print any faults to the terminal
YSI 0:9aa539a3d0f8 99 return prev_TC;
YSI 0:9aa539a3d0f8 100 }
YSI 0:9aa539a3d0f8 101
YSI 0:9aa539a3d0f8 102
YSI 0:9aa539a3d0f8 103 //*****************************************************************************
YSI 0:9aa539a3d0f8 104 float MAX31856::readCJ()
YSI 0:9aa539a3d0f8 105 {
YSI 2:c5e7f83a00ed 106 if(!init_MAX31856) return NAN;
YSI 2:c5e7f83a00ed 107
YSI 2:c5e7f83a00ed 108 uint32_t buf_read[2] = {0}, buf_write[2] = {ADDRESS_CJTH_READ, ADDRESS_CJTL_READ};
YSI 2:c5e7f83a00ed 109
YSI 2:c5e7f83a00ed 110 for(int i=0; i<2; i++) buf_read[i] = registerReadByte(buf_write[i]);
YSI 2:c5e7f83a00ed 111
YSI 2:c5e7f83a00ed 112 //Convert the registers contents into the correct value
YSI 2:c5e7f83a00ed 113 int16_t temp = ((buf_read[0] & 0xFF) << 8) + (buf_read[1] & 0xFF); // CJTH + CJTL
YSI 2:c5e7f83a00ed 114
YSI 2:c5e7f83a00ed 115 //checkFaultsColdJunctionThresholds(); //print any faults to the terminal
YSI 2:c5e7f83a00ed 116 return temp/256.0;
YSI 2:c5e7f83a00ed 117 /*uint16_t buf_read[3];
YSI 0:9aa539a3d0f8 118
YSI 0:9aa539a3d0f8 119 spiEnable();
YSI 2:c5e7f83a00ed 120 for(int i=0; i<3; i++) buf_read[i] = spi.write(ADDRESS_CJTH_READ);
YSI 0:9aa539a3d0f8 121 spiDisable();
YSI 0:9aa539a3d0f8 122
YSI 0:9aa539a3d0f8 123 //Convert the registers contents into the correct value
YSI 0:9aa539a3d0f8 124 int16_t temp = ((buf_read[1] & 0xFF) << 8) + (buf_read[2] & 0xFF); // CJTH + CJTL
YSI 0:9aa539a3d0f8 125
YSI 0:9aa539a3d0f8 126 //checkFaultsColdJunctionThresholds(); //print any faults to the terminal
YSI 2:c5e7f83a00ed 127 printf("\r\nCJ %d", temp);
YSI 2:c5e7f83a00ed 128 return ((temp==0)?NAN:(temp/256.0));*/
YSI 0:9aa539a3d0f8 129 }
YSI 0:9aa539a3d0f8 130
YSI 0:9aa539a3d0f8 131 //*****************************************************************************
YSI 0:9aa539a3d0f8 132 uint8_t MAX31856::checkFaultsThermocoupleThresholds()
YSI 0:9aa539a3d0f8 133 {
YSI 0:9aa539a3d0f8 134 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 135 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 136 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 137 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 138
YSI 0:9aa539a3d0f8 139 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 140 if ((fault_byte&0x4C)==0) //means no fault is detected for thermocouple thresholds
YSI 0:9aa539a3d0f8 141 return_int=0;
YSI 0:9aa539a3d0f8 142 else {
YSI 0:9aa539a3d0f8 143 if ((fault_byte&0x40)==0) { //check if normal operation of thermocouple is true
YSI 0:9aa539a3d0f8 144 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 145 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 146 return_int=1;
YSI 0:9aa539a3d0f8 147 }
YSI 0:9aa539a3d0f8 148 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 149 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 150 return_int=2;
YSI 0:9aa539a3d0f8 151 }
YSI 0:9aa539a3d0f8 152 }
YSI 0:9aa539a3d0f8 153 else { //Thermocouples is operating outside of normal range
YSI 0:9aa539a3d0f8 154 LOG("FAULT! Thermocouple temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 155 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 156 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 157 return_int=4;
YSI 0:9aa539a3d0f8 158 }
YSI 0:9aa539a3d0f8 159 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 160 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 161 return_int=5;
YSI 0:9aa539a3d0f8 162 }
YSI 0:9aa539a3d0f8 163 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 164 return_int=3;
YSI 0:9aa539a3d0f8 165 }
YSI 0:9aa539a3d0f8 166 }
YSI 0:9aa539a3d0f8 167 return return_int;
YSI 0:9aa539a3d0f8 168 }
YSI 0:9aa539a3d0f8 169
YSI 0:9aa539a3d0f8 170 //*****************************************************************************
YSI 0:9aa539a3d0f8 171 uint8_t MAX31856::checkFaultsColdJunctionThresholds()
YSI 0:9aa539a3d0f8 172 {
YSI 0:9aa539a3d0f8 173 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 174 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 175 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 176 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 177
YSI 0:9aa539a3d0f8 178 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 179 if ((fault_byte&0xB0)==0) //means no fault is detected for cold junction thresholds
YSI 0:9aa539a3d0f8 180 return_int=0;
YSI 0:9aa539a3d0f8 181 else {
YSI 0:9aa539a3d0f8 182 if ((fault_byte&0x80)==0) { //check if normal operation of cold junction is true
YSI 0:9aa539a3d0f8 183 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 184 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 185 return_int=1;
YSI 0:9aa539a3d0f8 186 }
YSI 0:9aa539a3d0f8 187 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 188 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 189 return_int=2;
YSI 0:9aa539a3d0f8 190 }
YSI 0:9aa539a3d0f8 191 }
YSI 0:9aa539a3d0f8 192 else { //Cold Junction is operating outside of normal range
YSI 0:9aa539a3d0f8 193 LOG("FAULT! Cold Junction temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 194 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 195 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 196 return_int=4;
YSI 0:9aa539a3d0f8 197 }
YSI 0:9aa539a3d0f8 198 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 199 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 200 return_int=5;
YSI 0:9aa539a3d0f8 201 }
YSI 0:9aa539a3d0f8 202 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 203 return_int=3;
YSI 0:9aa539a3d0f8 204 }
YSI 0:9aa539a3d0f8 205 }
YSI 0:9aa539a3d0f8 206 return return_int;
YSI 0:9aa539a3d0f8 207 }
YSI 0:9aa539a3d0f8 208
YSI 0:9aa539a3d0f8 209 //*****************************************************************************
YSI 0:9aa539a3d0f8 210 bool MAX31856::checkFaultsThermocoupleConnection()
YSI 0:9aa539a3d0f8 211 {
YSI 0:9aa539a3d0f8 212 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 213 uint8_t temp[2];
YSI 0:9aa539a3d0f8 214 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 215 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 216
YSI 0:9aa539a3d0f8 217 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 218 if (fault_byte==0) //means no fault is detected
YSI 0:9aa539a3d0f8 219 return_val=1;
YSI 0:9aa539a3d0f8 220 else{
YSI 0:9aa539a3d0f8 221 if (temp[0]&0x02) {
YSI 0:9aa539a3d0f8 222 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 223 return_val=0;
YSI 0:9aa539a3d0f8 224 }
YSI 0:9aa539a3d0f8 225 if (temp[1]&0x01) {
YSI 0:9aa539a3d0f8 226 LOG("Open circuit fault detected! Please check thermocouple connection!\r\n");
YSI 0:9aa539a3d0f8 227 return_val=0;
YSI 0:9aa539a3d0f8 228 }
YSI 0:9aa539a3d0f8 229 }
YSI 0:9aa539a3d0f8 230 return return_val;
YSI 0:9aa539a3d0f8 231 }
YSI 0:9aa539a3d0f8 232
YSI 0:9aa539a3d0f8 233
YSI 0:9aa539a3d0f8 234 //Register:CR0 Bits: 7
YSI 0:9aa539a3d0f8 235 //*****************************************************************************
YSI 0:9aa539a3d0f8 236 bool MAX31856::setConversionMode(uint8_t val)
YSI 0:9aa539a3d0f8 237 {
YSI 0:9aa539a3d0f8 238 if (val==CR0_CONV_MODE_NORMALLY_OFF) {
YSI 0:9aa539a3d0f8 239 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
YSI 0:9aa539a3d0f8 240 conversion_mode=0;
YSI 0:9aa539a3d0f8 241 //LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_OFF\r\n");
YSI 0:9aa539a3d0f8 242 }
YSI 0:9aa539a3d0f8 243 else if (val==CR0_CONV_MODE_NORMALLY_ON) {
YSI 0:9aa539a3d0f8 244 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
YSI 0:9aa539a3d0f8 245 conversion_mode=1;
YSI 0:9aa539a3d0f8 246 //LOG("Register containing\t\tsetConversionMode\t\twas programmed with the parameter\t\tCR0_CONV_MODE_NORMALLY_ON\r\n");
YSI 0:9aa539a3d0f8 247 }
YSI 0:9aa539a3d0f8 248 else {
YSI 0:9aa539a3d0f8 249 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 250 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 251 }
YSI 0:9aa539a3d0f8 252 return return_val;
YSI 0:9aa539a3d0f8 253 }
YSI 0:9aa539a3d0f8 254
YSI 0:9aa539a3d0f8 255
YSI 0:9aa539a3d0f8 256 //Register:CR0 Bits: 6
YSI 0:9aa539a3d0f8 257 //*****************************************************************************
YSI 0:9aa539a3d0f8 258 bool MAX31856::setOneShotMode(uint8_t val)
YSI 0:9aa539a3d0f8 259 {
YSI 0:9aa539a3d0f8 260 if (val==CR0_1_SHOT_MODE_NO_CONVERSION)
YSI 0:9aa539a3d0f8 261 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
YSI 0:9aa539a3d0f8 262 else if (val==CR0_1_SHOT_MODE_ONE_CONVERSION)
YSI 0:9aa539a3d0f8 263 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
YSI 0:9aa539a3d0f8 264 else {
YSI 0:9aa539a3d0f8 265 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 266 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 267 }
YSI 0:9aa539a3d0f8 268 return return_val;
YSI 0:9aa539a3d0f8 269 }
YSI 0:9aa539a3d0f8 270
YSI 0:9aa539a3d0f8 271
YSI 0:9aa539a3d0f8 272 //Register:CR0 Bits: 5:4
YSI 0:9aa539a3d0f8 273 //*****************************************************************************
YSI 0:9aa539a3d0f8 274 bool MAX31856::setOpenCircuitFaultDetection(uint8_t val)
YSI 0:9aa539a3d0f8 275 {
YSI 0:9aa539a3d0f8 276 if (val==CR0_OC_DETECT_DISABLED) {
YSI 0:9aa539a3d0f8 277 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 278 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_DISABLED\r\n");
YSI 0:9aa539a3d0f8 279 }
YSI 0:9aa539a3d0f8 280 else if (val==CR0_OC_DETECT_ENABLED_R_LESS_5k) {
YSI 0:9aa539a3d0f8 281 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 282 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_R_LESS_5k\r\n");
YSI 0:9aa539a3d0f8 283 }
YSI 0:9aa539a3d0f8 284 else if (val==CR0_OC_DETECT_ENABLED_TC_LESS_2ms) {
YSI 0:9aa539a3d0f8 285 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 286 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_LESS_2ms\r\n");
YSI 0:9aa539a3d0f8 287 }
YSI 0:9aa539a3d0f8 288 else if (val==CR0_OC_DETECT_ENABLED_TC_MORE_2ms) {
YSI 0:9aa539a3d0f8 289 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 0:9aa539a3d0f8 290 LOG("Register containing\t\tsetOpenCircuitFaultDetection\t\twas programmed with the parameter\t\tCR0_OC_DETECT_ENABLED_TC_MORE_2ms\r\n");
YSI 0:9aa539a3d0f8 291 }
YSI 0:9aa539a3d0f8 292 else {
YSI 0:9aa539a3d0f8 293 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 294 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 295 }
YSI 0:9aa539a3d0f8 296 return return_val;
YSI 0:9aa539a3d0f8 297 }
YSI 0:9aa539a3d0f8 298
YSI 0:9aa539a3d0f8 299
YSI 0:9aa539a3d0f8 300 //Register:CR0 Bits: 3
YSI 0:9aa539a3d0f8 301 //*****************************************************************************
YSI 0:9aa539a3d0f8 302 bool MAX31856::setColdJunctionDisable(uint8_t val)
YSI 0:9aa539a3d0f8 303 {
YSI 0:9aa539a3d0f8 304 if (val==CR0_COLD_JUNC_ENABLE) {
YSI 0:9aa539a3d0f8 305 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 306 cold_junction_enabled=1;
YSI 0:9aa539a3d0f8 307 LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_ENABLE\r\n");
YSI 0:9aa539a3d0f8 308 }
YSI 0:9aa539a3d0f8 309 else if (val==CR0_COLD_JUNC_DISABLE) {
YSI 0:9aa539a3d0f8 310 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 311 cold_junction_enabled=0;
YSI 0:9aa539a3d0f8 312 LOG("Register containing\t\tsetColdJunctionDisable\t\twas programmed with the parameter\t\tCR0_COLD_JUNC_DISABLE\r\n");
YSI 0:9aa539a3d0f8 313 }
YSI 0:9aa539a3d0f8 314 else {
YSI 0:9aa539a3d0f8 315 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 316 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 317 }
YSI 0:9aa539a3d0f8 318 return return_val;
YSI 0:9aa539a3d0f8 319 }
YSI 0:9aa539a3d0f8 320
YSI 0:9aa539a3d0f8 321
YSI 0:9aa539a3d0f8 322 //Register:CR0 Bits: 2
YSI 0:9aa539a3d0f8 323 //*****************************************************************************
YSI 0:9aa539a3d0f8 324 bool MAX31856::setFaultMode(uint8_t val)
YSI 0:9aa539a3d0f8 325 {
YSI 0:9aa539a3d0f8 326 if (val==CR0_FAULT_MODE_COMPARATOR) {
YSI 0:9aa539a3d0f8 327 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 328 LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_COMPARATOR\r\n");
YSI 0:9aa539a3d0f8 329 }
YSI 0:9aa539a3d0f8 330 else if (val==CR0_FAULT_MODE_INTERUPT) {
YSI 0:9aa539a3d0f8 331 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 332 LOG("Register containing\t\tsetFaultMode\t\twas programmed with the parameter\t\tCR0_FAULT_MODE_INTERUPT\r\n");
YSI 0:9aa539a3d0f8 333 }
YSI 0:9aa539a3d0f8 334 else {
YSI 0:9aa539a3d0f8 335 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 336 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 337 }
YSI 0:9aa539a3d0f8 338 return return_val;
YSI 0:9aa539a3d0f8 339 }
YSI 0:9aa539a3d0f8 340
YSI 0:9aa539a3d0f8 341
YSI 0:9aa539a3d0f8 342 //Register:CR0 Bits: 1
YSI 0:9aa539a3d0f8 343 //*****************************************************************************
YSI 0:9aa539a3d0f8 344 bool MAX31856::setFaultStatusClear(uint8_t val)
YSI 0:9aa539a3d0f8 345 {
YSI 0:9aa539a3d0f8 346 if (val==CR0_FAULTCLR_DEFAULT_VAL) {
YSI 0:9aa539a3d0f8 347 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 348 LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_DEFAULT_VAL\r\n");
YSI 0:9aa539a3d0f8 349 }
YSI 0:9aa539a3d0f8 350 else if (val==CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO) {
YSI 0:9aa539a3d0f8 351 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 352 LOG("Register containing\t\tsetFaultStatusClear\t\twas programmed with the parameter\t\tCR0_FAULTCLR_RETURN_FAULTS_TO_ZERO\r\n");
YSI 0:9aa539a3d0f8 353 }
YSI 0:9aa539a3d0f8 354 else {
YSI 0:9aa539a3d0f8 355 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 356 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 357 }
YSI 0:9aa539a3d0f8 358 return return_val;
YSI 0:9aa539a3d0f8 359 }
YSI 0:9aa539a3d0f8 360
YSI 0:9aa539a3d0f8 361
YSI 0:9aa539a3d0f8 362 //Register:CR0 Bits: 0
YSI 0:9aa539a3d0f8 363 //*****************************************************************************
YSI 0:9aa539a3d0f8 364 bool MAX31856::setEmiFilterFreq(uint8_t val)
YSI 0:9aa539a3d0f8 365 {
YSI 0:9aa539a3d0f8 366 if (val==CR0_FILTER_OUT_60Hz) {
YSI 0:9aa539a3d0f8 367 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 368 filter_mode=0;
YSI 0:9aa539a3d0f8 369 //LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_60Hz\r\n");
YSI 0:9aa539a3d0f8 370 }
YSI 0:9aa539a3d0f8 371 else if (val==CR0_FILTER_OUT_50Hz) {
YSI 0:9aa539a3d0f8 372 return_val=registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 373 filter_mode=1;
YSI 0:9aa539a3d0f8 374 //LOG("Register containing\t\tsetEmiFilterFreq\t\twas programmed with the parameter\t\tCR0_FILTER_OUT_50Hz\r\n");
YSI 0:9aa539a3d0f8 375 }
YSI 0:9aa539a3d0f8 376 else {
YSI 0:9aa539a3d0f8 377 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 378 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 379 }
YSI 0:9aa539a3d0f8 380 return return_val;
YSI 0:9aa539a3d0f8 381 }
YSI 0:9aa539a3d0f8 382
YSI 0:9aa539a3d0f8 383
YSI 0:9aa539a3d0f8 384 //Register:CR1 Bits: 6:4
YSI 0:9aa539a3d0f8 385 //*****************************************************************************
YSI 0:9aa539a3d0f8 386 bool MAX31856::setNumSamplesAvg(uint8_t val)
YSI 0:9aa539a3d0f8 387 {
YSI 0:9aa539a3d0f8 388 if (val==CR1_AVG_TC_SAMPLES_1) {
YSI 0:9aa539a3d0f8 389 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 390 samples=1;
YSI 0:9aa539a3d0f8 391 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_1\r\n");
YSI 0:9aa539a3d0f8 392 }
YSI 0:9aa539a3d0f8 393 else if (val==CR1_AVG_TC_SAMPLES_2) {
YSI 0:9aa539a3d0f8 394 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 395 samples=2;
YSI 0:9aa539a3d0f8 396 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_2\r\n");
YSI 0:9aa539a3d0f8 397 }
YSI 0:9aa539a3d0f8 398 else if (val==CR1_AVG_TC_SAMPLES_4) {
YSI 0:9aa539a3d0f8 399 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 400 samples=4;
YSI 0:9aa539a3d0f8 401 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_4\r\n");
YSI 0:9aa539a3d0f8 402 }
YSI 0:9aa539a3d0f8 403 else if (val==CR1_AVG_TC_SAMPLES_8) {
YSI 0:9aa539a3d0f8 404 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 405 samples=8;
YSI 0:9aa539a3d0f8 406 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_8\r\n");
YSI 0:9aa539a3d0f8 407 }
YSI 0:9aa539a3d0f8 408 else if (val==CR1_AVG_TC_SAMPLES_16) {
YSI 0:9aa539a3d0f8 409 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 0:9aa539a3d0f8 410 samples=16;
YSI 0:9aa539a3d0f8 411 //LOG("Register containing\t\tsetNumSamplesAvg\t\twas programmed with the parameter\t\tCR1_AVG_TC_SAMPLES_16\r\n");
YSI 0:9aa539a3d0f8 412 }
YSI 0:9aa539a3d0f8 413 else {
YSI 0:9aa539a3d0f8 414 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 415 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 416 }
YSI 0:9aa539a3d0f8 417 return return_val;
YSI 0:9aa539a3d0f8 418 }
YSI 0:9aa539a3d0f8 419
YSI 0:9aa539a3d0f8 420
YSI 0:9aa539a3d0f8 421 //Register:CR1 Bits: 3:0
YSI 0:9aa539a3d0f8 422 //*****************************************************************************
YSI 0:9aa539a3d0f8 423 bool MAX31856::setThermocoupleType(uint8_t val)
YSI 0:9aa539a3d0f8 424 {
YSI 0:9aa539a3d0f8 425 if (val==CR1_TC_TYPE_B) {
YSI 0:9aa539a3d0f8 426 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 427 voltage_mode=false;
YSI 0:9aa539a3d0f8 428 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_B\r\n");
YSI 0:9aa539a3d0f8 429 }
YSI 0:9aa539a3d0f8 430 else if (val==CR1_TC_TYPE_E) {
YSI 0:9aa539a3d0f8 431 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 432 voltage_mode=false;
YSI 0:9aa539a3d0f8 433 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_E\r\n");
YSI 0:9aa539a3d0f8 434 }
YSI 0:9aa539a3d0f8 435 else if (val==CR1_TC_TYPE_J) {
YSI 0:9aa539a3d0f8 436 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 437 voltage_mode=false;
YSI 0:9aa539a3d0f8 438 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_J\r\n");
YSI 0:9aa539a3d0f8 439 }
YSI 0:9aa539a3d0f8 440 else if (val==CR1_TC_TYPE_K) {
YSI 0:9aa539a3d0f8 441 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 442 voltage_mode=false;
YSI 0:9aa539a3d0f8 443 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_K\r\n");
YSI 0:9aa539a3d0f8 444 }
YSI 0:9aa539a3d0f8 445 else if (val==CR1_TC_TYPE_N) {
YSI 0:9aa539a3d0f8 446 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 447 voltage_mode=false;
YSI 0:9aa539a3d0f8 448 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_N\r\n");
YSI 0:9aa539a3d0f8 449 }
YSI 0:9aa539a3d0f8 450 else if (val==CR1_TC_TYPE_R) {
YSI 0:9aa539a3d0f8 451 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 452 voltage_mode=false;
YSI 0:9aa539a3d0f8 453 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_R\r\n");
YSI 0:9aa539a3d0f8 454 }
YSI 0:9aa539a3d0f8 455 else if (val==CR1_TC_TYPE_S) {
YSI 0:9aa539a3d0f8 456 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 457 voltage_mode=false;
YSI 0:9aa539a3d0f8 458 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_S\r\n");
YSI 0:9aa539a3d0f8 459 }
YSI 0:9aa539a3d0f8 460 else if (val==CR1_TC_TYPE_T) {
YSI 0:9aa539a3d0f8 461 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 462 voltage_mode=false;
YSI 0:9aa539a3d0f8 463 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_T\r\n");
YSI 0:9aa539a3d0f8 464 }
YSI 0:9aa539a3d0f8 465 else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_8) {
YSI 0:9aa539a3d0f8 466 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 467 voltage_mode=true;
YSI 0:9aa539a3d0f8 468 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_8\r\n");
YSI 0:9aa539a3d0f8 469 }
YSI 0:9aa539a3d0f8 470 else if (val==CR1_TC_TYPE_VOLT_MODE_GAIN_32) {
YSI 0:9aa539a3d0f8 471 return_val=registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 0:9aa539a3d0f8 472 voltage_mode=true;
YSI 0:9aa539a3d0f8 473 //LOG("Register containing\t\tsetThermocoupleType\t\twas programmed with the parameter\t\tCR1_TC_TYPE_VOLT_MODE_GAIN_32\r\n");
YSI 0:9aa539a3d0f8 474 }
YSI 0:9aa539a3d0f8 475 else {
YSI 0:9aa539a3d0f8 476 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 477 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 478 }
YSI 0:9aa539a3d0f8 479 return return_val;
YSI 0:9aa539a3d0f8 480 }
YSI 0:9aa539a3d0f8 481
YSI 0:9aa539a3d0f8 482
YSI 0:9aa539a3d0f8 483 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 484 //*****************************************************************************
YSI 0:9aa539a3d0f8 485 bool MAX31856::setFaultMasks(uint8_t val, bool enable)
YSI 0:9aa539a3d0f8 486 {
YSI 0:9aa539a3d0f8 487 if(enable)
YSI 0:9aa539a3d0f8 488 val=0;
YSI 0:9aa539a3d0f8 489 if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
YSI 0:9aa539a3d0f8 490 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_5, val);
YSI 0:9aa539a3d0f8 491 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_HIGH\r\n");
YSI 0:9aa539a3d0f8 492 }
YSI 0:9aa539a3d0f8 493 else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 494 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_4, val);
YSI 0:9aa539a3d0f8 495 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_CJ_FAULT_THRESHOLD_LOW\r\n");
YSI 0:9aa539a3d0f8 496 }
YSI 0:9aa539a3d0f8 497 else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
YSI 0:9aa539a3d0f8 498 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_3, val);
YSI 0:9aa539a3d0f8 499 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_HIGH\r\n");
YSI 0:9aa539a3d0f8 500 }
YSI 0:9aa539a3d0f8 501 else if (val==MASK_TC_FAULT_THRESHOLD_LOW) { //Thermocouple Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 502 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_2, val);
YSI 0:9aa539a3d0f8 503 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_TC_FAULT_THRESHOLD_LOW\r\n");
YSI 0:9aa539a3d0f8 504 }
YSI 0:9aa539a3d0f8 505 else if (val==MASK_OVER_UNDER_VOLT_FAULT) { //Over-Voltage/Under-Voltage Input Fault Mask
YSI 0:9aa539a3d0f8 506 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_1, val);
YSI 0:9aa539a3d0f8 507 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OVER_UNDER_VOLT_FAULT\r\n");
YSI 0:9aa539a3d0f8 508 }
YSI 0:9aa539a3d0f8 509 else if (val==MASK_OPEN_CIRCUIT_FAULT) { //Thermocouple Open-Circuit Fault Mask
YSI 0:9aa539a3d0f8 510 return_val=registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_0, val);
YSI 0:9aa539a3d0f8 511 LOG("Register containing\t\tsetFaultMasks\t\twas programmed with the parameter\t\tMASK_OPEN_CIRCUIT_FAULT\r\n");
YSI 0:9aa539a3d0f8 512 }
YSI 0:9aa539a3d0f8 513 else {
YSI 0:9aa539a3d0f8 514 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 515 return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
YSI 0:9aa539a3d0f8 516 }
YSI 0:9aa539a3d0f8 517 return return_val;
YSI 0:9aa539a3d0f8 518 }
YSI 0:9aa539a3d0f8 519
YSI 0:9aa539a3d0f8 520
YSI 0:9aa539a3d0f8 521 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 522 //******************************************************************************
YSI 0:9aa539a3d0f8 523 bool MAX31856::setFaultThresholds(uint8_t val, float temperature)
YSI 0:9aa539a3d0f8 524 {
YSI 0:9aa539a3d0f8 525 if (val==MASK_CJ_FAULT_THRESHOLD_HIGH) { //Cold Junction High Threshold Fault Mask
YSI 0:9aa539a3d0f8 526 int8_t temperature_byte=temperature;
YSI 0:9aa539a3d0f8 527 return_val=registerWriteByte(ADDRESS_CJHF_WRITE, temperature_byte);
YSI 0:9aa539a3d0f8 528 }
YSI 0:9aa539a3d0f8 529 else if (val==MASK_CJ_FAULT_THRESHOLD_LOW) { //Cold Junction Low Threshold Fault Mask
YSI 0:9aa539a3d0f8 530 int8_t temperature_byte=temperature;
YSI 0:9aa539a3d0f8 531 return_val=registerWriteByte(ADDRESS_CJLF_WRITE, temperature_byte);
YSI 0:9aa539a3d0f8 532 }
YSI 0:9aa539a3d0f8 533 else if (val==MASK_TC_FAULT_THRESHOLD_HIGH) { //Thermocouple High Threshold Fault Mask
YSI 0:9aa539a3d0f8 534 int8_t temperature_byte[2];
YSI 0:9aa539a3d0f8 535 int16_t temperature_multi_byte =temperature*4.0;
YSI 0:9aa539a3d0f8 536 //now split up the 16bit int into two bytes to program the registers with
YSI 0:9aa539a3d0f8 537 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 0:9aa539a3d0f8 538 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 0:9aa539a3d0f8 539
YSI 0:9aa539a3d0f8 540 return_val=registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]);
YSI 0:9aa539a3d0f8 541 return_val=registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);
YSI 0:9aa539a3d0f8 542 }
YSI 0:9aa539a3d0f8 543 else if (val==MASK_TC_FAULT_THRESHOLD_LOW) { //Thermocouple LOW Threshold Fault Mask
YSI 0:9aa539a3d0f8 544 int8_t temperature_byte[2];
YSI 0:9aa539a3d0f8 545 int16_t temperature_multi_byte =temperature*4.0;
YSI 0:9aa539a3d0f8 546 //now split up the 16bit int into two bytes to program the registers with
YSI 0:9aa539a3d0f8 547 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 0:9aa539a3d0f8 548 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 0:9aa539a3d0f8 549
YSI 0:9aa539a3d0f8 550 return_val=registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]);
YSI 0:9aa539a3d0f8 551 return_val=registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);
YSI 0:9aa539a3d0f8 552 }
YSI 0:9aa539a3d0f8 553 else
YSI 0:9aa539a3d0f8 554 LOG("Please select correct threshold register to program with the correct value!\r\n");
YSI 0:9aa539a3d0f8 555 return return_val;
YSI 0:9aa539a3d0f8 556 }
YSI 0:9aa539a3d0f8 557
YSI 0:9aa539a3d0f8 558 //******************************************************************************
YSI 0:9aa539a3d0f8 559 bool MAX31856::coldJunctionOffset(float temperature)
YSI 0:9aa539a3d0f8 560 {
YSI 0:9aa539a3d0f8 561 if (temperature > 7.9375 || temperature < -8.0) {
YSI 0:9aa539a3d0f8 562 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 563 return_val = 0;
YSI 0:9aa539a3d0f8 564 }
YSI 0:9aa539a3d0f8 565 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 566 return_val=registerWriteByte(ADDRESS_CJTO_WRITE, temp_val); //write the byte to cold junction offset register
YSI 0:9aa539a3d0f8 567 return return_val;
YSI 0:9aa539a3d0f8 568 }
YSI 0:9aa539a3d0f8 569
YSI 0:9aa539a3d0f8 570
YSI 0:9aa539a3d0f8 571 //The following functions are for internal library use only
YSI 0:9aa539a3d0f8 572 //******************************************************************************
YSI 0:9aa539a3d0f8 573 void MAX31856::spiEnable()
YSI 0:9aa539a3d0f8 574 {
YSI 0:9aa539a3d0f8 575 ncs=0; //Set CS low to start transmission (interrupts conversion)
YSI 0:9aa539a3d0f8 576 return;
YSI 0:9aa539a3d0f8 577 }
YSI 0:9aa539a3d0f8 578
YSI 0:9aa539a3d0f8 579
YSI 0:9aa539a3d0f8 580 //******************************************************************************
YSI 0:9aa539a3d0f8 581 void MAX31856::spiDisable()
YSI 0:9aa539a3d0f8 582 {
YSI 0:9aa539a3d0f8 583 ncs=1; //Set CS high to stop transmission (restarts conversion)
YSI 0:9aa539a3d0f8 584 return;
YSI 0:9aa539a3d0f8 585 }
YSI 0:9aa539a3d0f8 586
YSI 0:9aa539a3d0f8 587
YSI 0:9aa539a3d0f8 588 //******************************************************************************
YSI 0:9aa539a3d0f8 589 bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, int clear_bits, uint8_t val)
YSI 0:9aa539a3d0f8 590 {
YSI 2:c5e7f83a00ed 591 /*uint8_t buf_read[2];
YSI 0:9aa539a3d0f8 592
YSI 0:9aa539a3d0f8 593 //Read the current contents of a register
YSI 0:9aa539a3d0f8 594 spiEnable();
YSI 2:c5e7f83a00ed 595 for(int i=0; i<2; i++) buf_read[i] = spi.write(read_address);
YSI 0:9aa539a3d0f8 596 spiDisable();
YSI 0:9aa539a3d0f8 597
YSI 0:9aa539a3d0f8 598 //Modify contents pulled from the register
YSI 2:c5e7f83a00ed 599 buf_read[1] &= clear_bits; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
YSI 2:c5e7f83a00ed 600 buf_read[1] |= val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
YSI 2:c5e7f83a00ed 601 val = buf_read[1];
YSI 0:9aa539a3d0f8 602
YSI 0:9aa539a3d0f8 603 //Write the updated byte to the register
YSI 0:9aa539a3d0f8 604 spiEnable();
YSI 2:c5e7f83a00ed 605 spi.write(write_address);
YSI 2:c5e7f83a00ed 606 spi.write(val);
YSI 2:c5e7f83a00ed 607 spiDisable();
YSI 2:c5e7f83a00ed 608 //return 1;
YSI 2:c5e7f83a00ed 609
YSI 2:c5e7f83a00ed 610 // ajout YSI pour verification
YSI 2:c5e7f83a00ed 611 spiEnable();
YSI 2:c5e7f83a00ed 612 for(int i=0; i<2; i++) buf_read[i] = spi.write(read_address);
YSI 0:9aa539a3d0f8 613 spiDisable();
YSI 2:c5e7f83a00ed 614 return buf_read[1] == val;*/
YSI 2:c5e7f83a00ed 615
YSI 2:c5e7f83a00ed 616 // Version YSI
YSI 2:c5e7f83a00ed 617 uint8_t buf_read;
YSI 2:c5e7f83a00ed 618
YSI 2:c5e7f83a00ed 619 //Read the current contents of a register
YSI 2:c5e7f83a00ed 620 buf_read = registerReadByte(read_address);
YSI 2:c5e7f83a00ed 621
YSI 2:c5e7f83a00ed 622 //Modify contents pulled from the register
YSI 2:c5e7f83a00ed 623 buf_read &= clear_bits; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
YSI 2:c5e7f83a00ed 624 buf_read |= val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
YSI 2:c5e7f83a00ed 625 val = buf_read;
YSI 2:c5e7f83a00ed 626
YSI 2:c5e7f83a00ed 627 //Write the updated byte to the register
YSI 2:c5e7f83a00ed 628 registerWriteByte(write_address, val);
YSI 2:c5e7f83a00ed 629
YSI 2:c5e7f83a00ed 630 //Read the current contents of a register
YSI 2:c5e7f83a00ed 631 buf_read = registerReadByte(read_address);
YSI 2:c5e7f83a00ed 632
YSI 2:c5e7f83a00ed 633 return buf_read == val;
YSI 0:9aa539a3d0f8 634 }
YSI 0:9aa539a3d0f8 635
YSI 0:9aa539a3d0f8 636
YSI 0:9aa539a3d0f8 637 //******************************************************************************
YSI 0:9aa539a3d0f8 638 bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val)
YSI 0:9aa539a3d0f8 639 {
YSI 0:9aa539a3d0f8 640 //Write the updated byte to the register
YSI 0:9aa539a3d0f8 641 spiEnable();
YSI 0:9aa539a3d0f8 642 spi.write(write_address);
YSI 0:9aa539a3d0f8 643 spi.write(val);
YSI 0:9aa539a3d0f8 644 spiDisable();
YSI 0:9aa539a3d0f8 645 return true;
YSI 0:9aa539a3d0f8 646 }
YSI 0:9aa539a3d0f8 647
YSI 0:9aa539a3d0f8 648 //******************************************************************************
YSI 0:9aa539a3d0f8 649 uint8_t MAX31856::registerReadByte(uint8_t read_address)
YSI 0:9aa539a3d0f8 650 {
YSI 0:9aa539a3d0f8 651 spiEnable();
YSI 0:9aa539a3d0f8 652 spi.write(read_address);
YSI 2:c5e7f83a00ed 653 uint8_t buf_read = spi.write(0);
YSI 0:9aa539a3d0f8 654 spiDisable();
YSI 0:9aa539a3d0f8 655 return buf_read;
YSI 0:9aa539a3d0f8 656 }
YSI 0:9aa539a3d0f8 657
YSI 0:9aa539a3d0f8 658 //******************************************************************************
YSI 0:9aa539a3d0f8 659 void MAX31856::calculateDelayTime() {
YSI 0:9aa539a3d0f8 660 uint32_t temp_int;
YSI 0:9aa539a3d0f8 661
YSI 0:9aa539a3d0f8 662 if (conversion_mode==0 || thermocouple_conversion_count==0) {
YSI 0:9aa539a3d0f8 663 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 664 temp_int=82+(samples-1)*33.33f;
YSI 0:9aa539a3d0f8 665 else //50Hz
YSI 0:9aa539a3d0f8 666 temp_int=98+(samples-1)*40.00f;
YSI 0:9aa539a3d0f8 667 }
YSI 0:9aa539a3d0f8 668 else {
YSI 0:9aa539a3d0f8 669 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 670 temp_int=82+(samples-1)*16.67f;
YSI 0:9aa539a3d0f8 671 else //50Hz
YSI 0:9aa539a3d0f8 672 temp_int=98+(samples-1)*20.00f;
YSI 0:9aa539a3d0f8 673 }
YSI 0:9aa539a3d0f8 674
YSI 0:9aa539a3d0f8 675 if (cold_junction_enabled==0) //cold junction is disabled enabling 25 millisecond faster conversion times
YSI 0:9aa539a3d0f8 676 temp_int=temp_int-25;
YSI 0:9aa539a3d0f8 677 conversion_time=1000*temp_int; //set private member conversion time to calculated minimum wait time in microseconds
YSI 0:9aa539a3d0f8 678 return;
YSI 0:9aa539a3d0f8 679 }
YSI 0:9aa539a3d0f8 680
YSI 0:9aa539a3d0f8 681 //*****************************************************************************
YSI 0:9aa539a3d0f8 682 MAX31856::~MAX31856(void)
YSI 0:9aa539a3d0f8 683 {
YSI 0:9aa539a3d0f8 684 //empty block
YSI 0:9aa539a3d0f8 685 }