MAX31856

Dependents:   lib_MAX31856_example

Committer:
YSI
Date:
Fri Jul 23 08:16:37 2021 +0000
Revision:
3:9fb5fcf05ac3
Parent:
2:c5e7f83a00ed
Correct bugs and make lot of changes in MAXIM origin lib

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 3:9fb5fcf05ac3 10 * Updated: Jully 2021 By Yannic Simon
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 #include "lib_MAX31856.h"
YSI 0:9aa539a3d0f8 48
YSI 0:9aa539a3d0f8 49 #define LOG(args...) printf(args)
YSI 0:9aa539a3d0f8 50
YSI 0:9aa539a3d0f8 51 //*****************************************************************************
YSI 3:9fb5fcf05ac3 52 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 3:9fb5fcf05ac3 53 {
YSI 0:9aa539a3d0f8 54 spi.format(8,3); //configure the correct SPI mode to beable to program the registers intially correctly
YSI 2:c5e7f83a00ed 55 init_MAX31856 &= setThermocoupleType(_type);
YSI 2:c5e7f83a00ed 56 init_MAX31856 &= setEmiFilterFreq(_fltr);
YSI 2:c5e7f83a00ed 57 init_MAX31856 &= setNumSamplesAvg(_samples);
YSI 2:c5e7f83a00ed 58 init_MAX31856 &= setConversionMode(_conversion_mode);
YSI 3:9fb5fcf05ac3 59 lastReadTime = time(NULL);
YSI 3:9fb5fcf05ac3 60 wait_us(1000000);
YSI 0:9aa539a3d0f8 61 }
YSI 0:9aa539a3d0f8 62
YSI 0:9aa539a3d0f8 63
YSI 0:9aa539a3d0f8 64 //*****************************************************************************
YSI 0:9aa539a3d0f8 65 float MAX31856::readTC()
YSI 1:916d3fb3b21d 66 {
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 3:9fb5fcf05ac3 69 init_MAX31856 &= 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 3:9fb5fcf05ac3 72 if(!init_MAX31856) return NAN;
YSI 0:9aa539a3d0f8 73 //calculate minimum wait time for conversions
YSI 0:9aa539a3d0f8 74 calculateDelayTime();
YSI 0:9aa539a3d0f8 75 //initialize other info for the read functionality
YSI 2:c5e7f83a00ed 76 uint32_t buf_read[3] = {0}, buf_write[3] = {ADDRESS_LTCBH_READ, ADDRESS_LTCBM_READ, ADDRESS_LTCBL_READ};
YSI 3:9fb5fcf05ac3 77 if(checkFaultsThermocoupleConnection()) //no faults with connection are present so continue on with normal read of temperature
YSI 3:9fb5fcf05ac3 78 {
YSI 3:9fb5fcf05ac3 79 uint32_t tm = time(NULL)*1000000;
YSI 3:9fb5fcf05ac3 80 uint32_t duration = tm - lastReadTime;
YSI 3:9fb5fcf05ac3 81 lastReadTime = tm;
YSI 0:9aa539a3d0f8 82 if (duration > conversion_time)
YSI 0:9aa539a3d0f8 83 {
YSI 0:9aa539a3d0f8 84 for(int i=0; i<3; i++) buf_read[i] = registerReadByte(buf_write[i]);
YSI 0:9aa539a3d0f8 85 //Convert the registers contents into the correct value
YSI 3:9fb5fcf05ac3 86 int32_t temp = ((buf_read[0] & 0xFF) << 0x18) + ((buf_read[1] & 0xFF) << 0x10) + ((buf_read[2] & 0xFF) << 0x08); // LTCBH + LTCBM + LTCBL
YSI 3:9fb5fcf05ac3 87 return prev_TC = (temp >> 0x0D) * 0.0078125;
YSI 0:9aa539a3d0f8 88 }
YSI 3:9fb5fcf05ac3 89 }
YSI 0:9aa539a3d0f8 90 thermocouple_conversion_count++; //iterate the conversion count to speed up time in between future converions in always on mode
YSI 3:9fb5fcf05ac3 91 checkFaultsThermocoupleThresholds(); //print any faults to the terminal
YSI 0:9aa539a3d0f8 92 return prev_TC;
YSI 0:9aa539a3d0f8 93 }
YSI 0:9aa539a3d0f8 94
YSI 0:9aa539a3d0f8 95
YSI 0:9aa539a3d0f8 96 //*****************************************************************************
YSI 0:9aa539a3d0f8 97 float MAX31856::readCJ()
YSI 0:9aa539a3d0f8 98 {
YSI 2:c5e7f83a00ed 99 if(!init_MAX31856) return NAN;
YSI 2:c5e7f83a00ed 100 uint32_t buf_read[2] = {0}, buf_write[2] = {ADDRESS_CJTH_READ, ADDRESS_CJTL_READ};
YSI 2:c5e7f83a00ed 101 for(int i=0; i<2; i++) buf_read[i] = registerReadByte(buf_write[i]);
YSI 2:c5e7f83a00ed 102 int16_t temp = ((buf_read[0] & 0xFF) << 8) + (buf_read[1] & 0xFF); // CJTH + CJTL
YSI 2:c5e7f83a00ed 103 return temp/256.0;
YSI 0:9aa539a3d0f8 104 }
YSI 0:9aa539a3d0f8 105
YSI 0:9aa539a3d0f8 106 //*****************************************************************************
YSI 0:9aa539a3d0f8 107 uint8_t MAX31856::checkFaultsThermocoupleThresholds()
YSI 0:9aa539a3d0f8 108 {
YSI 0:9aa539a3d0f8 109 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 110 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 111 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 112 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 113
YSI 0:9aa539a3d0f8 114 //Check if any of the faults for thermocouple connection are triggered
YSI 0:9aa539a3d0f8 115 if ((fault_byte&0x4C)==0) //means no fault is detected for thermocouple thresholds
YSI 0:9aa539a3d0f8 116 return_int=0;
YSI 0:9aa539a3d0f8 117 else {
YSI 0:9aa539a3d0f8 118 if ((fault_byte&0x40)==0) { //check if normal operation of thermocouple is true
YSI 0:9aa539a3d0f8 119 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 120 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 121 return_int=1;
YSI 0:9aa539a3d0f8 122 }
YSI 0:9aa539a3d0f8 123 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 124 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 125 return_int=2;
YSI 0:9aa539a3d0f8 126 }
YSI 0:9aa539a3d0f8 127 }
YSI 0:9aa539a3d0f8 128 else { //Thermocouples is operating outside of normal range
YSI 0:9aa539a3d0f8 129 LOG("FAULT! Thermocouple temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 130 if (temp[0]&0x08) {
YSI 0:9aa539a3d0f8 131 LOG("FAULT! Thermocouple temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 132 return_int=4;
YSI 0:9aa539a3d0f8 133 }
YSI 0:9aa539a3d0f8 134 else if (temp[1]&0x04) {
YSI 0:9aa539a3d0f8 135 LOG("FAULT! Thermocouple temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 136 return_int=5;
YSI 0:9aa539a3d0f8 137 }
YSI 0:9aa539a3d0f8 138 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 139 return_int=3;
YSI 0:9aa539a3d0f8 140 }
YSI 0:9aa539a3d0f8 141 }
YSI 0:9aa539a3d0f8 142 return return_int;
YSI 0:9aa539a3d0f8 143 }
YSI 0:9aa539a3d0f8 144
YSI 0:9aa539a3d0f8 145 //*****************************************************************************
YSI 0:9aa539a3d0f8 146 uint8_t MAX31856::checkFaultsColdJunctionThresholds()
YSI 0:9aa539a3d0f8 147 {
YSI 0:9aa539a3d0f8 148 uint8_t fault_byte=registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 149 uint8_t temp[2], return_int;
YSI 0:9aa539a3d0f8 150 for(int i=0; i<2; i++)
YSI 0:9aa539a3d0f8 151 temp[i]=fault_byte;
YSI 0:9aa539a3d0f8 152
YSI 0:9aa539a3d0f8 153 //Check if any of the faults for thermocouple connection are triggered
YSI 3:9fb5fcf05ac3 154 if ((fault_byte&0xB0)==0) //means no fault is detected for cold junction thresholds
YSI 0:9aa539a3d0f8 155 return_int=0;
YSI 0:9aa539a3d0f8 156 else {
YSI 0:9aa539a3d0f8 157 if ((fault_byte&0x80)==0) { //check if normal operation of cold junction is true
YSI 0:9aa539a3d0f8 158 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 159 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 160 return_int=1;
YSI 0:9aa539a3d0f8 161 }
YSI 0:9aa539a3d0f8 162 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 163 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 164 return_int=2;
YSI 0:9aa539a3d0f8 165 }
YSI 0:9aa539a3d0f8 166 }
YSI 0:9aa539a3d0f8 167 else { //Cold Junction is operating outside of normal range
YSI 0:9aa539a3d0f8 168 LOG("FAULT! Cold Junction temperature is out of range for specific type of thermocouple!\r\n");
YSI 0:9aa539a3d0f8 169 if (temp[0]&0x20) {
YSI 0:9aa539a3d0f8 170 LOG("FAULT! Cold Junction temp is higher than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 171 return_int=4;
YSI 0:9aa539a3d0f8 172 }
YSI 0:9aa539a3d0f8 173 else if (temp[1]&0x10) {
YSI 0:9aa539a3d0f8 174 LOG("FAULT! Cold Junction temp is lower than the threshold that is set!\r\n");
YSI 0:9aa539a3d0f8 175 return_int=5;
YSI 0:9aa539a3d0f8 176 }
YSI 0:9aa539a3d0f8 177 else //no other faults are flagged besides unnatural operation
YSI 0:9aa539a3d0f8 178 return_int=3;
YSI 0:9aa539a3d0f8 179 }
YSI 0:9aa539a3d0f8 180 }
YSI 0:9aa539a3d0f8 181 return return_int;
YSI 0:9aa539a3d0f8 182 }
YSI 0:9aa539a3d0f8 183
YSI 0:9aa539a3d0f8 184 //*****************************************************************************
YSI 0:9aa539a3d0f8 185 bool MAX31856::checkFaultsThermocoupleConnection()
YSI 0:9aa539a3d0f8 186 {
YSI 3:9fb5fcf05ac3 187 return !registerReadByte(ADDRESS_SR_READ); //Read contents of fault status register
YSI 0:9aa539a3d0f8 188 }
YSI 0:9aa539a3d0f8 189
YSI 0:9aa539a3d0f8 190
YSI 0:9aa539a3d0f8 191 //Register:CR0 Bits: 7
YSI 0:9aa539a3d0f8 192 //*****************************************************************************
YSI 0:9aa539a3d0f8 193 bool MAX31856::setConversionMode(uint8_t val)
YSI 0:9aa539a3d0f8 194 {
YSI 3:9fb5fcf05ac3 195 switch(val)
YSI 3:9fb5fcf05ac3 196 {
YSI 3:9fb5fcf05ac3 197 case CR0_CONV_MODE_NORMALLY_OFF: case CR0_CONV_MODE_NORMALLY_ON:
YSI 3:9fb5fcf05ac3 198 conversion_mode = (val == CR0_CONV_MODE_NORMALLY_ON)?1:0;
YSI 3:9fb5fcf05ac3 199 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_7, val);
YSI 3:9fb5fcf05ac3 200 break;
YSI 3:9fb5fcf05ac3 201 default:
YSI 3:9fb5fcf05ac3 202 //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 3:9fb5fcf05ac3 203 return false;
YSI 3:9fb5fcf05ac3 204 break;
YSI 0:9aa539a3d0f8 205 }
YSI 0:9aa539a3d0f8 206 }
YSI 0:9aa539a3d0f8 207
YSI 0:9aa539a3d0f8 208
YSI 0:9aa539a3d0f8 209 //Register:CR0 Bits: 6
YSI 0:9aa539a3d0f8 210 //*****************************************************************************
YSI 0:9aa539a3d0f8 211 bool MAX31856::setOneShotMode(uint8_t val)
YSI 0:9aa539a3d0f8 212 {
YSI 3:9fb5fcf05ac3 213 switch(val)
YSI 3:9fb5fcf05ac3 214 {
YSI 3:9fb5fcf05ac3 215 case CR0_1_SHOT_MODE_NO_CONVERSION: case CR0_1_SHOT_MODE_ONE_CONVERSION:
YSI 3:9fb5fcf05ac3 216 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_6, val);
YSI 3:9fb5fcf05ac3 217 break;
YSI 3:9fb5fcf05ac3 218 default:
YSI 3:9fb5fcf05ac3 219 //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 3:9fb5fcf05ac3 220 return false;
YSI 3:9fb5fcf05ac3 221 break;
YSI 0:9aa539a3d0f8 222 }
YSI 0:9aa539a3d0f8 223 }
YSI 0:9aa539a3d0f8 224
YSI 0:9aa539a3d0f8 225
YSI 0:9aa539a3d0f8 226 //Register:CR0 Bits: 5:4
YSI 0:9aa539a3d0f8 227 //*****************************************************************************
YSI 0:9aa539a3d0f8 228 bool MAX31856::setOpenCircuitFaultDetection(uint8_t val)
YSI 0:9aa539a3d0f8 229 {
YSI 3:9fb5fcf05ac3 230 switch(val)
YSI 3:9fb5fcf05ac3 231 {
YSI 3:9fb5fcf05ac3 232 case CR0_OC_DETECT_DISABLED: case CR0_OC_DETECT_ENABLED_R_LESS_5k: case CR0_OC_DETECT_ENABLED_TC_LESS_2ms: case CR0_OC_DETECT_ENABLED_TC_MORE_2ms:
YSI 3:9fb5fcf05ac3 233 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_5_4, val);
YSI 3:9fb5fcf05ac3 234 break;
YSI 3:9fb5fcf05ac3 235 default:
YSI 3:9fb5fcf05ac3 236 //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 3:9fb5fcf05ac3 237 return false;
YSI 3:9fb5fcf05ac3 238 break;
YSI 0:9aa539a3d0f8 239 }
YSI 0:9aa539a3d0f8 240 }
YSI 0:9aa539a3d0f8 241
YSI 0:9aa539a3d0f8 242
YSI 0:9aa539a3d0f8 243 //Register:CR0 Bits: 3
YSI 0:9aa539a3d0f8 244 //*****************************************************************************
YSI 0:9aa539a3d0f8 245 bool MAX31856::setColdJunctionDisable(uint8_t val)
YSI 0:9aa539a3d0f8 246 {
YSI 3:9fb5fcf05ac3 247 switch(val)
YSI 3:9fb5fcf05ac3 248 {
YSI 3:9fb5fcf05ac3 249 case CR0_COLD_JUNC_ENABLE: case CR0_COLD_JUNC_DISABLE:
YSI 3:9fb5fcf05ac3 250 cold_junction_enabled = (val==CR0_COLD_JUNC_ENABLE)?1:0;
YSI 3:9fb5fcf05ac3 251 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_3, val);
YSI 3:9fb5fcf05ac3 252 break;
YSI 3:9fb5fcf05ac3 253 default:
YSI 3:9fb5fcf05ac3 254 //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 3:9fb5fcf05ac3 255 return false;
YSI 3:9fb5fcf05ac3 256 break;
YSI 0:9aa539a3d0f8 257 }
YSI 0:9aa539a3d0f8 258 }
YSI 0:9aa539a3d0f8 259
YSI 0:9aa539a3d0f8 260
YSI 0:9aa539a3d0f8 261 //Register:CR0 Bits: 2
YSI 0:9aa539a3d0f8 262 //*****************************************************************************
YSI 0:9aa539a3d0f8 263 bool MAX31856::setFaultMode(uint8_t val)
YSI 0:9aa539a3d0f8 264 {
YSI 3:9fb5fcf05ac3 265 switch(val)
YSI 3:9fb5fcf05ac3 266 {
YSI 3:9fb5fcf05ac3 267 case CR0_FAULT_MODE_COMPARATOR: case CR0_FAULT_MODE_INTERUPT:
YSI 3:9fb5fcf05ac3 268 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_2, val);
YSI 3:9fb5fcf05ac3 269 break;
YSI 3:9fb5fcf05ac3 270 default:
YSI 3:9fb5fcf05ac3 271 //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 3:9fb5fcf05ac3 272 return false;
YSI 3:9fb5fcf05ac3 273 break;
YSI 0:9aa539a3d0f8 274 }
YSI 0:9aa539a3d0f8 275 }
YSI 0:9aa539a3d0f8 276
YSI 0:9aa539a3d0f8 277
YSI 0:9aa539a3d0f8 278 //Register:CR0 Bits: 1
YSI 0:9aa539a3d0f8 279 //*****************************************************************************
YSI 0:9aa539a3d0f8 280 bool MAX31856::setFaultStatusClear(uint8_t val)
YSI 0:9aa539a3d0f8 281 {
YSI 3:9fb5fcf05ac3 282 switch(val)
YSI 3:9fb5fcf05ac3 283 {
YSI 3:9fb5fcf05ac3 284 case CR0_FAULTCLR_DEFAULT_VAL: case CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO:
YSI 3:9fb5fcf05ac3 285 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_1, val);
YSI 3:9fb5fcf05ac3 286 break;
YSI 3:9fb5fcf05ac3 287 default:
YSI 3:9fb5fcf05ac3 288 //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 3:9fb5fcf05ac3 289 return false;
YSI 3:9fb5fcf05ac3 290 break;
YSI 0:9aa539a3d0f8 291 }
YSI 0:9aa539a3d0f8 292 }
YSI 0:9aa539a3d0f8 293
YSI 0:9aa539a3d0f8 294
YSI 0:9aa539a3d0f8 295 //Register:CR0 Bits: 0
YSI 0:9aa539a3d0f8 296 //*****************************************************************************
YSI 0:9aa539a3d0f8 297 bool MAX31856::setEmiFilterFreq(uint8_t val)
YSI 0:9aa539a3d0f8 298 {
YSI 3:9fb5fcf05ac3 299 switch(val)
YSI 3:9fb5fcf05ac3 300 {
YSI 3:9fb5fcf05ac3 301 case CR0_FILTER_OUT_60Hz: case CR0_FILTER_OUT_50Hz:
YSI 3:9fb5fcf05ac3 302 filter_mode = val;
YSI 3:9fb5fcf05ac3 303 return registerReadWriteByte(ADDRESS_CR0_READ, ADDRESS_CR0_WRITE, CR0_CLEAR_BITS_0, val);
YSI 3:9fb5fcf05ac3 304 break;
YSI 3:9fb5fcf05ac3 305 default:
YSI 3:9fb5fcf05ac3 306 //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 3:9fb5fcf05ac3 307 return false;
YSI 3:9fb5fcf05ac3 308 break;
YSI 0:9aa539a3d0f8 309 }
YSI 0:9aa539a3d0f8 310 }
YSI 0:9aa539a3d0f8 311
YSI 0:9aa539a3d0f8 312
YSI 0:9aa539a3d0f8 313 //Register:CR1 Bits: 6:4
YSI 0:9aa539a3d0f8 314 //*****************************************************************************
YSI 0:9aa539a3d0f8 315 bool MAX31856::setNumSamplesAvg(uint8_t val)
YSI 3:9fb5fcf05ac3 316 {
YSI 3:9fb5fcf05ac3 317 switch(val)
YSI 3:9fb5fcf05ac3 318 {
YSI 3:9fb5fcf05ac3 319 case CR1_AVG_TC_SAMPLES_1: case CR1_AVG_TC_SAMPLES_2: case CR1_AVG_TC_SAMPLES_4: case CR1_AVG_TC_SAMPLES_8: case CR1_AVG_TC_SAMPLES_16:
YSI 3:9fb5fcf05ac3 320 samples = 1 << (val >> 4);
YSI 3:9fb5fcf05ac3 321 return registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_6_4, val);
YSI 3:9fb5fcf05ac3 322 break;
YSI 3:9fb5fcf05ac3 323 default:
YSI 3:9fb5fcf05ac3 324 //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 3:9fb5fcf05ac3 325 return false;
YSI 3:9fb5fcf05ac3 326 break;
YSI 0:9aa539a3d0f8 327 }
YSI 0:9aa539a3d0f8 328 }
YSI 0:9aa539a3d0f8 329
YSI 0:9aa539a3d0f8 330
YSI 0:9aa539a3d0f8 331 //Register:CR1 Bits: 3:0
YSI 0:9aa539a3d0f8 332 //*****************************************************************************
YSI 0:9aa539a3d0f8 333 bool MAX31856::setThermocoupleType(uint8_t val)
YSI 3:9fb5fcf05ac3 334 {
YSI 3:9fb5fcf05ac3 335 switch(val)
YSI 3:9fb5fcf05ac3 336 {
YSI 3:9fb5fcf05ac3 337 case CR1_TC_TYPE_B: case CR1_TC_TYPE_E: case CR1_TC_TYPE_J: case CR1_TC_TYPE_K: case CR1_TC_TYPE_N: case CR1_TC_TYPE_R: case CR1_TC_TYPE_S: case CR1_TC_TYPE_T: case CR1_TC_TYPE_VOLT_MODE_GAIN_8: case CR1_TC_TYPE_VOLT_MODE_GAIN_32:
YSI 3:9fb5fcf05ac3 338 voltage_mode = ((val == CR1_TC_TYPE_VOLT_MODE_GAIN_8) || (val == CR1_TC_TYPE_VOLT_MODE_GAIN_32));
YSI 3:9fb5fcf05ac3 339 return registerReadWriteByte(ADDRESS_CR1_READ, ADDRESS_CR1_WRITE, CR1_CLEAR_BITS_3_0, val);
YSI 3:9fb5fcf05ac3 340 break;
YSI 3:9fb5fcf05ac3 341 default:
YSI 3:9fb5fcf05ac3 342 //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 3:9fb5fcf05ac3 343 return false;
YSI 3:9fb5fcf05ac3 344 break;
YSI 0:9aa539a3d0f8 345 }
YSI 0:9aa539a3d0f8 346 }
YSI 0:9aa539a3d0f8 347
YSI 0:9aa539a3d0f8 348
YSI 0:9aa539a3d0f8 349 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 350 //*****************************************************************************
YSI 0:9aa539a3d0f8 351 bool MAX31856::setFaultMasks(uint8_t val, bool enable)
YSI 0:9aa539a3d0f8 352 {
YSI 3:9fb5fcf05ac3 353 if(enable) val = 0;
YSI 3:9fb5fcf05ac3 354 switch(val)
YSI 3:9fb5fcf05ac3 355 {
YSI 3:9fb5fcf05ac3 356 case MASK_CJ_FAULT_THRESHOLD_HIGH:
YSI 3:9fb5fcf05ac3 357 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_5, val);
YSI 3:9fb5fcf05ac3 358 break;
YSI 3:9fb5fcf05ac3 359 case MASK_CJ_FAULT_THRESHOLD_LOW:
YSI 3:9fb5fcf05ac3 360 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_4, val);
YSI 3:9fb5fcf05ac3 361 break;
YSI 3:9fb5fcf05ac3 362 case MASK_TC_FAULT_THRESHOLD_HIGH:
YSI 3:9fb5fcf05ac3 363 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_3, val);
YSI 3:9fb5fcf05ac3 364 break;
YSI 3:9fb5fcf05ac3 365 case MASK_TC_FAULT_THRESHOLD_LOW:
YSI 3:9fb5fcf05ac3 366 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_2, val);
YSI 3:9fb5fcf05ac3 367 break;
YSI 3:9fb5fcf05ac3 368 case MASK_OVER_UNDER_VOLT_FAULT:
YSI 3:9fb5fcf05ac3 369 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_1, val);
YSI 3:9fb5fcf05ac3 370 break;
YSI 3:9fb5fcf05ac3 371 case MASK_OPEN_CIRCUIT_FAULT:
YSI 3:9fb5fcf05ac3 372 return registerReadWriteByte(ADDRESS_MASK_READ, ADDRESS_MASK_WRITE, MASK_CLEAR_BITS_0, val);
YSI 3:9fb5fcf05ac3 373 break;
YSI 3:9fb5fcf05ac3 374 default:
YSI 3:9fb5fcf05ac3 375 //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 3:9fb5fcf05ac3 376 return false;
YSI 3:9fb5fcf05ac3 377 break;
YSI 0:9aa539a3d0f8 378 }
YSI 0:9aa539a3d0f8 379 }
YSI 0:9aa539a3d0f8 380
YSI 0:9aa539a3d0f8 381
YSI 0:9aa539a3d0f8 382 //Register:MASK Bits: 5:0
YSI 0:9aa539a3d0f8 383 //******************************************************************************
YSI 0:9aa539a3d0f8 384 bool MAX31856::setFaultThresholds(uint8_t val, float temperature)
YSI 0:9aa539a3d0f8 385 {
YSI 3:9fb5fcf05ac3 386 switch(val)
YSI 3:9fb5fcf05ac3 387 {
YSI 3:9fb5fcf05ac3 388 case MASK_CJ_FAULT_THRESHOLD_HIGH:
YSI 3:9fb5fcf05ac3 389 return registerWriteByte(ADDRESS_CJHF_WRITE, temperature);
YSI 3:9fb5fcf05ac3 390 break;
YSI 3:9fb5fcf05ac3 391 case MASK_CJ_FAULT_THRESHOLD_LOW:
YSI 3:9fb5fcf05ac3 392 return registerWriteByte(ADDRESS_CJLF_WRITE, temperature);
YSI 3:9fb5fcf05ac3 393 break;
YSI 3:9fb5fcf05ac3 394 case MASK_TC_FAULT_THRESHOLD_HIGH:{
YSI 3:9fb5fcf05ac3 395 int8_t temperature_byte[2];
YSI 3:9fb5fcf05ac3 396 int16_t temperature_multi_byte =temperature*4.0;
YSI 3:9fb5fcf05ac3 397 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 3:9fb5fcf05ac3 398 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 3:9fb5fcf05ac3 399 return registerWriteByte(ADDRESS_LTHFTH_WRITE, temperature_byte[0]) && registerWriteByte(ADDRESS_LTHFTL_WRITE, temperature_byte[1]);}
YSI 3:9fb5fcf05ac3 400 break;
YSI 3:9fb5fcf05ac3 401 case MASK_TC_FAULT_THRESHOLD_LOW:{
YSI 3:9fb5fcf05ac3 402 int8_t temperature_byte[2];
YSI 3:9fb5fcf05ac3 403 int16_t temperature_multi_byte =temperature*4.0;
YSI 3:9fb5fcf05ac3 404 temperature_byte[0]=((uint8_t)((temperature_multi_byte)&(0xFF00) >> 8));
YSI 3:9fb5fcf05ac3 405 temperature_byte[1]=((uint8_t)((temperature_multi_byte)&(0x00FF)));
YSI 3:9fb5fcf05ac3 406 return registerWriteByte(ADDRESS_LTLFTH_WRITE, temperature_byte[0]) && registerWriteByte(ADDRESS_LTLFTL_WRITE, temperature_byte[1]);}
YSI 3:9fb5fcf05ac3 407 break;
YSI 3:9fb5fcf05ac3 408 default:
YSI 3:9fb5fcf05ac3 409 return false;
YSI 3:9fb5fcf05ac3 410 //LOG("Please select correct threshold register to program with the correct value!\r\n");
YSI 3:9fb5fcf05ac3 411 break;
YSI 0:9aa539a3d0f8 412 }
YSI 0:9aa539a3d0f8 413 }
YSI 0:9aa539a3d0f8 414
YSI 0:9aa539a3d0f8 415 //******************************************************************************
YSI 0:9aa539a3d0f8 416 bool MAX31856::coldJunctionOffset(float temperature)
YSI 0:9aa539a3d0f8 417 {
YSI 3:9fb5fcf05ac3 418 if (temperature > 7.9375 || temperature < -8.0)
YSI 3:9fb5fcf05ac3 419 {
YSI 3:9fb5fcf05ac3 420 //LOG("Input value to offest the cold junction point is non valid. enter in value in range -8 to +7.9375\r\n");
YSI 3:9fb5fcf05ac3 421 return false;
YSI 0:9aa539a3d0f8 422 }
YSI 0:9aa539a3d0f8 423 int8_t temp_val=temperature*16.0f; //normalize the value to get rid of decimal and shorten it to size of register
YSI 3:9fb5fcf05ac3 424 return registerWriteByte(ADDRESS_CJTO_WRITE, temp_val); //write the byte to cold junction offset register
YSI 0:9aa539a3d0f8 425 }
YSI 0:9aa539a3d0f8 426
YSI 0:9aa539a3d0f8 427
YSI 0:9aa539a3d0f8 428 //The following functions are for internal library use only
YSI 0:9aa539a3d0f8 429 //******************************************************************************
YSI 0:9aa539a3d0f8 430 void MAX31856::spiEnable()
YSI 0:9aa539a3d0f8 431 {
YSI 0:9aa539a3d0f8 432 ncs=0; //Set CS low to start transmission (interrupts conversion)
YSI 0:9aa539a3d0f8 433 return;
YSI 0:9aa539a3d0f8 434 }
YSI 0:9aa539a3d0f8 435
YSI 0:9aa539a3d0f8 436
YSI 0:9aa539a3d0f8 437 //******************************************************************************
YSI 0:9aa539a3d0f8 438 void MAX31856::spiDisable()
YSI 0:9aa539a3d0f8 439 {
YSI 0:9aa539a3d0f8 440 ncs=1; //Set CS high to stop transmission (restarts conversion)
YSI 0:9aa539a3d0f8 441 return;
YSI 0:9aa539a3d0f8 442 }
YSI 0:9aa539a3d0f8 443
YSI 0:9aa539a3d0f8 444
YSI 0:9aa539a3d0f8 445 //******************************************************************************
YSI 0:9aa539a3d0f8 446 bool MAX31856::registerReadWriteByte(uint8_t read_address, uint8_t write_address, int clear_bits, uint8_t val)
YSI 0:9aa539a3d0f8 447 {
YSI 0:9aa539a3d0f8 448 //Read the current contents of a register
YSI 3:9fb5fcf05ac3 449 uint8_t buf_read = registerReadByte(read_address);
YSI 0:9aa539a3d0f8 450
YSI 0:9aa539a3d0f8 451 //Modify contents pulled from the register
YSI 3:9fb5fcf05ac3 452 buf_read &= clear_bits; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
YSI 3:9fb5fcf05ac3 453 buf_read |= val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
YSI 2:c5e7f83a00ed 454 val = buf_read;
YSI 2:c5e7f83a00ed 455
YSI 2:c5e7f83a00ed 456 //Write the updated byte to the register
YSI 2:c5e7f83a00ed 457 registerWriteByte(write_address, val);
YSI 2:c5e7f83a00ed 458
YSI 2:c5e7f83a00ed 459 //Read the current contents of a register
YSI 2:c5e7f83a00ed 460 buf_read = registerReadByte(read_address);
YSI 2:c5e7f83a00ed 461
YSI 2:c5e7f83a00ed 462 return buf_read == val;
YSI 0:9aa539a3d0f8 463 }
YSI 0:9aa539a3d0f8 464
YSI 0:9aa539a3d0f8 465
YSI 0:9aa539a3d0f8 466 //******************************************************************************
YSI 0:9aa539a3d0f8 467 bool MAX31856::registerWriteByte(uint8_t write_address, uint8_t val)
YSI 0:9aa539a3d0f8 468 {
YSI 0:9aa539a3d0f8 469 //Write the updated byte to the register
YSI 0:9aa539a3d0f8 470 spiEnable();
YSI 0:9aa539a3d0f8 471 spi.write(write_address);
YSI 0:9aa539a3d0f8 472 spi.write(val);
YSI 0:9aa539a3d0f8 473 spiDisable();
YSI 0:9aa539a3d0f8 474 return true;
YSI 0:9aa539a3d0f8 475 }
YSI 0:9aa539a3d0f8 476
YSI 0:9aa539a3d0f8 477 //******************************************************************************
YSI 0:9aa539a3d0f8 478 uint8_t MAX31856::registerReadByte(uint8_t read_address)
YSI 0:9aa539a3d0f8 479 {
YSI 0:9aa539a3d0f8 480 spiEnable();
YSI 0:9aa539a3d0f8 481 spi.write(read_address);
YSI 2:c5e7f83a00ed 482 uint8_t buf_read = spi.write(0);
YSI 0:9aa539a3d0f8 483 spiDisable();
YSI 0:9aa539a3d0f8 484 return buf_read;
YSI 0:9aa539a3d0f8 485 }
YSI 0:9aa539a3d0f8 486
YSI 0:9aa539a3d0f8 487 //******************************************************************************
YSI 0:9aa539a3d0f8 488 void MAX31856::calculateDelayTime() {
YSI 0:9aa539a3d0f8 489 uint32_t temp_int;
YSI 0:9aa539a3d0f8 490
YSI 0:9aa539a3d0f8 491 if (conversion_mode==0 || thermocouple_conversion_count==0) {
YSI 0:9aa539a3d0f8 492 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 493 temp_int=82+(samples-1)*33.33f;
YSI 0:9aa539a3d0f8 494 else //50Hz
YSI 0:9aa539a3d0f8 495 temp_int=98+(samples-1)*40.00f;
YSI 0:9aa539a3d0f8 496 }
YSI 0:9aa539a3d0f8 497 else {
YSI 0:9aa539a3d0f8 498 if (filter_mode==0) //60Hz
YSI 0:9aa539a3d0f8 499 temp_int=82+(samples-1)*16.67f;
YSI 0:9aa539a3d0f8 500 else //50Hz
YSI 0:9aa539a3d0f8 501 temp_int=98+(samples-1)*20.00f;
YSI 0:9aa539a3d0f8 502 }
YSI 0:9aa539a3d0f8 503
YSI 0:9aa539a3d0f8 504 if (cold_junction_enabled==0) //cold junction is disabled enabling 25 millisecond faster conversion times
YSI 0:9aa539a3d0f8 505 temp_int=temp_int-25;
YSI 0:9aa539a3d0f8 506 conversion_time=1000*temp_int; //set private member conversion time to calculated minimum wait time in microseconds
YSI 0:9aa539a3d0f8 507 return;
YSI 0:9aa539a3d0f8 508 }
YSI 0:9aa539a3d0f8 509
YSI 0:9aa539a3d0f8 510 //*****************************************************************************
YSI 0:9aa539a3d0f8 511 MAX31856::~MAX31856(void)
YSI 0:9aa539a3d0f8 512 {
YSI 0:9aa539a3d0f8 513 //empty block
YSI 0:9aa539a3d0f8 514 }