Maxim Integrated / Mbed 2 deprecated MAX31856_example_program

Dependencies:   MAX31856 mbed

Fork of MAX31856_example_program by Central Applications - Mbed Code repo

Committer:
DevinAlexander
Date:
Tue Aug 01 03:39:32 2017 +0000
Revision:
10:4b907f32b4d4
Parent:
9:2d284cc2f65c
All that is left to do is redefine the sample program source code to explain the example program better

Who changed what in which revision?

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