Y SI / lib_MAX31856

Dependents:   lib_MAX31856_example

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:9aa539a3d0f8 1 /******************************************************************//**
YSI 0:9aa539a3d0f8 2 * @file MAX31856.h
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 Header file for MAX31856 class
YSI 0:9aa539a3d0f8 13 *
YSI 0:9aa539a3d0f8 14 ***********************************************************************
YSI 0:9aa539a3d0f8 15 *
YSI 0:9aa539a3d0f8 16 * @copyright
YSI 0:9aa539a3d0f8 17 * Copyright (C) 2017 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
YSI 0:9aa539a3d0f8 49 #ifndef MAX31856_h
YSI 0:9aa539a3d0f8 50 #define MAX31856_h
YSI 0:9aa539a3d0f8 51 #include "mbed.h"
YSI 0:9aa539a3d0f8 52
YSI 0:9aa539a3d0f8 53 //*****************************************************************************
YSI 0:9aa539a3d0f8 54 //Define all the addresses of the registers in the MAX31856
YSI 0:9aa539a3d0f8 55 //*****************************************************************************
YSI 0:9aa539a3d0f8 56 #define ADDRESS_CR0_READ 0x00 //Factory Default 00h
YSI 0:9aa539a3d0f8 57 #define ADDRESS_CR0_WRITE 0x80
YSI 0:9aa539a3d0f8 58 #define ADDRESS_CR1_READ 0x01 //Factory Default 03h
YSI 0:9aa539a3d0f8 59 #define ADDRESS_CR1_WRITE 0x81
YSI 0:9aa539a3d0f8 60 #define ADDRESS_MASK_READ 0x02 //Factory Default FFh
YSI 0:9aa539a3d0f8 61 #define ADDRESS_MASK_WRITE 0x82
YSI 0:9aa539a3d0f8 62 #define ADDRESS_CJHF_READ 0x03 //Factory Default 7Fh
YSI 0:9aa539a3d0f8 63 #define ADDRESS_CJHF_WRITE 0x83
YSI 0:9aa539a3d0f8 64 #define ADDRESS_CJLF_READ 0x04 //Factory Default C0h
YSI 0:9aa539a3d0f8 65 #define ADDRESS_CJLF_WRITE 0x84
YSI 0:9aa539a3d0f8 66 #define ADDRESS_LTHFTH_READ 0x05 //Factory Default 7Fh
YSI 0:9aa539a3d0f8 67 #define ADDRESS_LTHFTH_WRITE 0x85
YSI 0:9aa539a3d0f8 68 #define ADDRESS_LTHFTL_READ 0x06 //Factory Default FFh
YSI 0:9aa539a3d0f8 69 #define ADDRESS_LTHFTL_WRITE 0x86
YSI 0:9aa539a3d0f8 70 #define ADDRESS_LTLFTH_READ 0x07 //Factory Default 80h
YSI 0:9aa539a3d0f8 71 #define ADDRESS_LTLFTH_WRITE 0x87
YSI 0:9aa539a3d0f8 72 #define ADDRESS_LTLFTL_READ 0x08 //Factory Default 00h
YSI 0:9aa539a3d0f8 73 #define ADDRESS_LTLFTL_WRITE 0x88
YSI 0:9aa539a3d0f8 74 #define ADDRESS_CJTO_READ 0x09 //Factory Default 00h
YSI 0:9aa539a3d0f8 75 #define ADDRESS_CJTO_WRITE 0x89
YSI 0:9aa539a3d0f8 76 #define ADDRESS_CJTH_READ 0x0A //Factory Default 00h
YSI 0:9aa539a3d0f8 77 #define ADDRESS_CJTH_WRITE 0x8A
YSI 0:9aa539a3d0f8 78 #define ADDRESS_CJTL_READ 0x0B //Factory Default 00h
YSI 0:9aa539a3d0f8 79 #define ADDRESS_CJTL_WRITE 0x8B
YSI 0:9aa539a3d0f8 80 #define ADDRESS_LTCBH_READ 0x0C
YSI 0:9aa539a3d0f8 81 #define ADDRESS_LTCBM_READ 0x0D
YSI 0:9aa539a3d0f8 82 #define ADDRESS_LTCBL_READ 0x0E
YSI 0:9aa539a3d0f8 83 #define ADDRESS_SR_READ 0x0F
YSI 0:9aa539a3d0f8 84
YSI 0:9aa539a3d0f8 85 //*****************************************************************************
YSI 0:9aa539a3d0f8 86 //Define parameters for control register zero (CR0)
YSI 0:9aa539a3d0f8 87 //*****************************************************************************
YSI 0:9aa539a3d0f8 88 #define CR0_CONV_MODE_NORMALLY_OFF 0x00 //Power On Default value
YSI 0:9aa539a3d0f8 89 #define CR0_CONV_MODE_NORMALLY_ON 0x80
YSI 0:9aa539a3d0f8 90
YSI 0:9aa539a3d0f8 91 #define CR0_1_SHOT_MODE_NO_CONVERSION 0x00 //defaults to this value
YSI 0:9aa539a3d0f8 92 #define CR0_1_SHOT_MODE_ONE_CONVERSION 0x40 //^
YSI 0:9aa539a3d0f8 93
YSI 0:9aa539a3d0f8 94 #define CR0_OC_DETECT_DISABLED 0x00
YSI 0:9aa539a3d0f8 95 #define CR0_OC_DETECT_ENABLED_R_LESS_5k 0x10
YSI 0:9aa539a3d0f8 96 #define CR0_OC_DETECT_ENABLED_TC_LESS_2ms 0x20
YSI 0:9aa539a3d0f8 97 #define CR0_OC_DETECT_ENABLED_TC_MORE_2ms 0x30
YSI 0:9aa539a3d0f8 98
YSI 0:9aa539a3d0f8 99 #define CR0_COLD_JUNC_ENABLE 0x00 //Power On Default value
YSI 0:9aa539a3d0f8 100 #define CR0_COLD_JUNC_DISABLE 0x08 //speed of conversion is sped up by 25ms when this optionis selected (Disable the cold junc)
YSI 0:9aa539a3d0f8 101
YSI 0:9aa539a3d0f8 102 #define CR0_FAULT_MODE_COMPARATOR 0x00 //Power On Default value
YSI 0:9aa539a3d0f8 103 #define CR0_FAULT_MODE_INTERUPT 0x04
YSI 0:9aa539a3d0f8 104
YSI 0:9aa539a3d0f8 105 #define CR0_FAULTCLR_DEFAULT_VAL 0x00 //defaults to this value
YSI 0:9aa539a3d0f8 106 #define CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO 0x02 //^
YSI 0:9aa539a3d0f8 107
YSI 0:9aa539a3d0f8 108 #define CR0_FILTER_OUT_60Hz 0x00 //Preset value
YSI 0:9aa539a3d0f8 109 #define CR0_FILTER_OUT_50Hz 0x01 //^
YSI 0:9aa539a3d0f8 110
YSI 0:9aa539a3d0f8 111
YSI 0:9aa539a3d0f8 112 //*****************************************************************************
YSI 0:9aa539a3d0f8 113 //Define parameters for control register one (CR1)
YSI 0:9aa539a3d0f8 114 //*****************************************************************************
YSI 0:9aa539a3d0f8 115 #define CR1_AVG_TC_SAMPLES_1 0x00 //Power on default value
YSI 0:9aa539a3d0f8 116 #define CR1_AVG_TC_SAMPLES_2 0x10
YSI 0:9aa539a3d0f8 117 #define CR1_AVG_TC_SAMPLES_4 0x20
YSI 0:9aa539a3d0f8 118 #define CR1_AVG_TC_SAMPLES_8 0x30
YSI 0:9aa539a3d0f8 119 #define CR1_AVG_TC_SAMPLES_16 0x40
YSI 0:9aa539a3d0f8 120
YSI 0:9aa539a3d0f8 121 // Define which type of thermocouple the MAX31856 is using. This is for lineariztion purposes
YSI 0:9aa539a3d0f8 122 #define CR1_TC_TYPE_B 0x00
YSI 0:9aa539a3d0f8 123 #define CR1_TC_TYPE_E 0x01
YSI 0:9aa539a3d0f8 124 #define CR1_TC_TYPE_J 0x02
YSI 0:9aa539a3d0f8 125 #define CR1_TC_TYPE_K 0x03 //Power on default value
YSI 0:9aa539a3d0f8 126 #define CR1_TC_TYPE_N 0x04
YSI 0:9aa539a3d0f8 127 #define CR1_TC_TYPE_R 0x05
YSI 0:9aa539a3d0f8 128 #define CR1_TC_TYPE_S 0x06
YSI 0:9aa539a3d0f8 129 #define CR1_TC_TYPE_T 0x07
YSI 0:9aa539a3d0f8 130 #define CR1_TC_TYPE_VOLT_MODE_GAIN_8 0x08
YSI 0:9aa539a3d0f8 131 #define CR1_TC_TYPE_VOLT_MODE_GAIN_32 0x0C
YSI 0:9aa539a3d0f8 132
YSI 0:9aa539a3d0f8 133 //*****************************************************************************
YSI 0:9aa539a3d0f8 134 //Define parameters for the mask register (MASK)
YSI 0:9aa539a3d0f8 135 //*****************************************************************************
YSI 0:9aa539a3d0f8 136 #define MASK_CJ_FAULT_THRESHOLD_HIGH 0x20
YSI 0:9aa539a3d0f8 137 #define MASK_CJ_FAULT_THRESHOLD_LOW 0x10
YSI 0:9aa539a3d0f8 138 #define MASK_TC_FAULT_THRESHOLD_HIGH 0x08
YSI 0:9aa539a3d0f8 139 #define MASK_TC_FAULT_THRESHOLD_LOW 0x04
YSI 0:9aa539a3d0f8 140 #define MASK_OVER_UNDER_VOLT_FAULT 0x02
YSI 0:9aa539a3d0f8 141 #define MASK_OPEN_CIRCUIT_FAULT 0x01
YSI 0:9aa539a3d0f8 142
YSI 0:9aa539a3d0f8 143 //*****************************************************************************
YSI 0:9aa539a3d0f8 144 //If these defined values are &= (bitwise ANDed) with the contents of a register, it will reset the bits pertaing to the specific bitfields to zero
YSI 0:9aa539a3d0f8 145 //*****************************************************************************
YSI 0:9aa539a3d0f8 146 #define CR0_CLEAR_BITS_7 ~(0x80)
YSI 0:9aa539a3d0f8 147 #define CR0_CLEAR_BITS_6 ~(0x40)
YSI 0:9aa539a3d0f8 148 #define CR0_CLEAR_BITS_5_4 ~(0x30)
YSI 0:9aa539a3d0f8 149 #define CR0_CLEAR_BITS_3 ~(0x08)
YSI 0:9aa539a3d0f8 150 #define CR0_CLEAR_BITS_2 ~(0x04)
YSI 0:9aa539a3d0f8 151 #define CR0_CLEAR_BITS_1 ~(0x02)
YSI 0:9aa539a3d0f8 152 #define CR0_CLEAR_BITS_0 ~(0x01)
YSI 0:9aa539a3d0f8 153
YSI 0:9aa539a3d0f8 154 #define CR1_CLEAR_BITS_6_4 ~(0x70)
YSI 0:9aa539a3d0f8 155 #define CR1_CLEAR_BITS_3_0 ~(0x0F)
YSI 0:9aa539a3d0f8 156
YSI 0:9aa539a3d0f8 157 #define MASK_CLEAR_BITS_5 ~(0x20)
YSI 0:9aa539a3d0f8 158 #define MASK_CLEAR_BITS_4 ~(0x10)
YSI 0:9aa539a3d0f8 159 #define MASK_CLEAR_BITS_3 ~(0x08)
YSI 0:9aa539a3d0f8 160 #define MASK_CLEAR_BITS_2 ~(0x04)
YSI 0:9aa539a3d0f8 161 #define MASK_CLEAR_BITS_1 ~(0x02)
YSI 0:9aa539a3d0f8 162 #define MASK_CLEAR_BITS_0 ~(0x01)
YSI 0:9aa539a3d0f8 163
YSI 0:9aa539a3d0f8 164 //*****************************************************************************
YSI 0:9aa539a3d0f8 165 ///Parameters that are used throughout the library
YSI 0:9aa539a3d0f8 166 //*****************************************************************************
YSI 0:9aa539a3d0f8 167 #define TC_MAX_VAL_FAULT 1800
YSI 0:9aa539a3d0f8 168 #define TC_MIN_VAL_FAULT -210
YSI 0:9aa539a3d0f8 169 #define CJ_MAX_VAL_FAULT 125
YSI 0:9aa539a3d0f8 170 #define CJ_MIN_VAL_FAULT -55
YSI 0:9aa539a3d0f8 171
YSI 0:9aa539a3d0f8 172
YSI 0:9aa539a3d0f8 173
YSI 0:9aa539a3d0f8 174 /**
YSI 0:9aa539a3d0f8 175 * @brief Library for the MAX31856\n
YSI 0:9aa539a3d0f8 176 * The MAX31856 thermocouple temperature sensor accurately measures temperature
YSI 0:9aa539a3d0f8 177 * and provides a vast amount of features such as:
YSI 0:9aa539a3d0f8 178 * //FEATURE
YSI 0:9aa539a3d0f8 179 * //FEATURE
YSI 0:9aa539a3d0f8 180 * //FEATURE
YSI 0:9aa539a3d0f8 181 * //FEATURE
YSI 0:9aa539a3d0f8 182 * //FEATURE
YSI 0:9aa539a3d0f8 183 * //FEATURE
YSI 0:9aa539a3d0f8 184 * Communication is through an SPI-compatible interface.
YSI 0:9aa539a3d0f8 185 *
YSI 0:9aa539a3d0f8 186 * @code
YSI 0:9aa539a3d0f8 187 * #include "mbed.h"
YSI 0:9aa539a3d0f8 188 * #include "MAX31856.h"
YSI 0:9aa539a3d0f8 189 *
YSI 0:9aa539a3d0f8 190 *
YSI 0:9aa539a3d0f8 191 * // Hardware serial port
YSI 0:9aa539a3d0f8 192 * Serial serial(USBTX, USBRX);
YSI 0:9aa539a3d0f8 193 *
YSI 0:9aa539a3d0f8 194 * //SPI communications
YSI 0:9aa539a3d0f8 195 * SPI spi(SPIO MOSI,SPIO MISO,SPIO SCK);
YSI 0:9aa539a3d0f8 196 *
YSI 0:9aa539a3d0f8 197 * //Thermocouples
YSI 0:9aa539a3d0f8 198 * MAX31856 Thermocouple1(spi, CHIPSELECT);
YSI 0:9aa539a3d0f8 199 *
YSI 0:9aa539a3d0f8 200 *
YSI 0:9aa539a3d0f8 201 * int main(void)
YSI 0:9aa539a3d0f8 202 * {
YSI 0:9aa539a3d0f8 203 * float temperature_TC_1, temperature_CJ_1;
YSI 0:9aa539a3d0f8 204 * while(true)
YSI 0:9aa539a3d0f8 205 * {
YSI 0:9aa539a3d0f8 206 * temperature_TC_1=Thermocouple1.readTC();
YSI 0:9aa539a3d0f8 207 * temperature_CJ_1=Thermocouple1.readCJ();
YSI 0:9aa539a3d0f8 208 * serial.printf("MAX31856 TC = %f Celsius MAX31856 CJ = %f Celsius \n\r",temperature_TC_1,temperature_CJ_1);
YSI 0:9aa539a3d0f8 209 * wait(1.0);
YSI 0:9aa539a3d0f8 210 * }
YSI 0:9aa539a3d0f8 211 * }
YSI 0:9aa539a3d0f8 212 * @endcode
YSI 0:9aa539a3d0f8 213 */
YSI 0:9aa539a3d0f8 214
YSI 0:9aa539a3d0f8 215
YSI 0:9aa539a3d0f8 216
YSI 0:9aa539a3d0f8 217 /** Please see pages 18-26 in the MAX31856 data sheet to see what register bit masks are needed to be set
YSI 0:9aa539a3d0f8 218 to achieve functionality desired. The data sheet can be found at
YSI 0:9aa539a3d0f8 219
YSI 0:9aa539a3d0f8 220 *** https://datasheets.maximintegrated.com/en/ds/MAX31856.pdf ***
YSI 0:9aa539a3d0f8 221 */
YSI 0:9aa539a3d0f8 222
YSI 0:9aa539a3d0f8 223
YSI 0:9aa539a3d0f8 224 /**
YSI 0:9aa539a3d0f8 225 * MAX31856 Class
YSI 0:9aa539a3d0f8 226 */
YSI 0:9aa539a3d0f8 227 class MAX31856
YSI 0:9aa539a3d0f8 228 {
YSI 0:9aa539a3d0f8 229
YSI 0:9aa539a3d0f8 230 public:
YSI 0:9aa539a3d0f8 231 //*****************************************************************************
YSI 0:9aa539a3d0f8 232 //Constructor and Destructor for the class
YSI 0:9aa539a3d0f8 233 //*****************************************************************************
YSI 0:9aa539a3d0f8 234 /**
YSI 0:9aa539a3d0f8 235 * @brief Constructor to create MAX31856 object with SPI information as well as preconfiguration parameter settings in configuration registers Zero and One
YSI 0:9aa539a3d0f8 236 * @param _spi - Reference to SPI object
YSI 0:9aa539a3d0f8 237 * @param _ncs - Chip Select for SPI comunications with the oject
YSI 0:9aa539a3d0f8 238 * @param _type - Type of thermocouple used
YSI 0:9aa539a3d0f8 239 * @param _fltr - Feature of the MAX31856 to filter out either 50Hz/60Hz from signal
YSI 0:9aa539a3d0f8 240 * @param _samples - How many samples are averaged for one conversion
YSI 0:9aa539a3d0f8 241 * @param _conversion_mode - Choose between always on and making conversions and off in between requests for a reading
YSI 0:9aa539a3d0f8 242 */
YSI 0:9aa539a3d0f8 243 MAX31856(SPI& _spi, PinName _ncs, uint8_t _type=CR1_TC_TYPE_K, uint8_t _fltr=CR0_FILTER_OUT_60Hz, uint8_t _samples=CR1_AVG_TC_SAMPLES_1, uint8_t _conversion_mode=CR0_CONV_MODE_NORMALLY_OFF);
YSI 0:9aa539a3d0f8 244
YSI 0:9aa539a3d0f8 245
YSI 0:9aa539a3d0f8 246 /** @brief Destructor */
YSI 0:9aa539a3d0f8 247 ~MAX31856(void);
YSI 0:9aa539a3d0f8 248
YSI 0:9aa539a3d0f8 249
YSI 0:9aa539a3d0f8 250 //*****************************************************************************
YSI 0:9aa539a3d0f8 251 //Temperature Functions
YSI 0:9aa539a3d0f8 252 //*****************************************************************************
YSI 0:9aa539a3d0f8 253 /**
YSI 0:9aa539a3d0f8 254 * @brief Requests read of the thermocouple temperature
YSI 0:9aa539a3d0f8 255 * @return float of the converted thermocouple reading based on current configurations
YSI 0:9aa539a3d0f8 256 */
YSI 0:9aa539a3d0f8 257 float readTC();
YSI 0:9aa539a3d0f8 258
YSI 0:9aa539a3d0f8 259
YSI 0:9aa539a3d0f8 260 /**
YSI 0:9aa539a3d0f8 261 * @brief Requests read of the cold junction temperature
YSI 0:9aa539a3d0f8 262 * @return float of the converted artificial cold junction reading based on current configurations
YSI 0:9aa539a3d0f8 263 */
YSI 0:9aa539a3d0f8 264 float readCJ();
YSI 0:9aa539a3d0f8 265
YSI 0:9aa539a3d0f8 266
YSI 0:9aa539a3d0f8 267 //*****************************************************************************
YSI 0:9aa539a3d0f8 268 //Functions for register CR0
YSI 0:9aa539a3d0f8 269 //*****************************************************************************
YSI 0:9aa539a3d0f8 270 /**
YSI 0:9aa539a3d0f8 271 * @brief Sets bits in the configuration register zero for setting the rate of conversions
YSI 0:9aa539a3d0f8 272 * @param val \li CR0_CONV_MODE_NORMALLY_OFF (Power On Default value)
YSI 0:9aa539a3d0f8 273 * \li CR0_CONV_MODE_NORMALLY_ON
YSI 0:9aa539a3d0f8 274 * @return \li 1 on success
YSI 0:9aa539a3d0f8 275 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 276 */
YSI 0:9aa539a3d0f8 277 bool setConversionMode(uint8_t val);
YSI 0:9aa539a3d0f8 278
YSI 0:9aa539a3d0f8 279
YSI 0:9aa539a3d0f8 280 /**
YSI 0:9aa539a3d0f8 281 * @brief Sets bits in the configuration register zero for enabling one conversion to take place
YSI 0:9aa539a3d0f8 282 * @param val \li CR0_1_SHOT_MODE_NO_CONVERSION (Power On Default value)
YSI 0:9aa539a3d0f8 283 * \li CR0_1_SHOT_MODE_ONE_CONVERSION (This bit self clears itself to default back to CR0_1_SHOT_MODE_NO_CONVERSION after singular conversion takes place)
YSI 0:9aa539a3d0f8 284 * @return \li 1 on success
YSI 0:9aa539a3d0f8 285 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 286 */
YSI 0:9aa539a3d0f8 287 bool setOneShotMode(uint8_t val);
YSI 0:9aa539a3d0f8 288
YSI 0:9aa539a3d0f8 289
YSI 0:9aa539a3d0f8 290 /**
YSI 0:9aa539a3d0f8 291 * @brief Sets bits in the configuration register zero for configuring open circuit fault detection
YSI 0:9aa539a3d0f8 292 * @param val \li CR0_OC_DETECT_DISABLED (Power On Default value)
YSI 0:9aa539a3d0f8 293 * \li CR0_OC_DETECT_ENABLED_R_LESS_5k
YSI 0:9aa539a3d0f8 294 * \li CR0_OC_DETECT_ENABLED_TC_LESS_2ms
YSI 0:9aa539a3d0f8 295 * \li CR0_OC_DETECT_ENABLED_TC_MORE_2ms
YSI 0:9aa539a3d0f8 296 * @return \li 1 on success
YSI 0:9aa539a3d0f8 297 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 298 */
YSI 0:9aa539a3d0f8 299 bool setOpenCircuitFaultDetection(uint8_t val);
YSI 0:9aa539a3d0f8 300
YSI 0:9aa539a3d0f8 301
YSI 0:9aa539a3d0f8 302 /**
YSI 0:9aa539a3d0f8 303 * @brief Sets bits in the configuration register zero for disabling or enabling the Cold Junction
YSI 0:9aa539a3d0f8 304 * @param val \li CR0_COLD_JUNC_ENABLE (Power On Default value)
YSI 0:9aa539a3d0f8 305 * \li CR0_COLD_JUNC_DISABLE
YSI 0:9aa539a3d0f8 306 * @return \li 1 on success
YSI 0:9aa539a3d0f8 307 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 308 */
YSI 0:9aa539a3d0f8 309 bool setColdJunctionDisable(uint8_t val);
YSI 0:9aa539a3d0f8 310
YSI 0:9aa539a3d0f8 311
YSI 0:9aa539a3d0f8 312 /**
YSI 0:9aa539a3d0f8 313 * @brief Sets bits in the configuration register zero for setting fault mode status
YSI 0:9aa539a3d0f8 314 * @param val \li CR0_FAULT_MODE_COMPARATOR (Power On Default value)
YSI 0:9aa539a3d0f8 315 * \li CR0_FAULT_MODE_INTERUPT
YSI 0:9aa539a3d0f8 316 * @return \li 1 on success
YSI 0:9aa539a3d0f8 317 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 318 */
YSI 0:9aa539a3d0f8 319 bool setFaultMode(uint8_t val);
YSI 0:9aa539a3d0f8 320
YSI 0:9aa539a3d0f8 321
YSI 0:9aa539a3d0f8 322 /**
YSI 0:9aa539a3d0f8 323 * @brief Sets bits in the configuration register zero for clearing fault status
YSI 0:9aa539a3d0f8 324 * @param val \li CR0_FAULTCLR_DEFAULT_VAL (Power On Default value)
YSI 0:9aa539a3d0f8 325 * \li CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO (This bit self clears itself to default back to CR0_FAULTCLR_DEFAULT_VAL after fault status is cleared)
YSI 0:9aa539a3d0f8 326 * @return \li 1 on success
YSI 0:9aa539a3d0f8 327 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 328 */
YSI 0:9aa539a3d0f8 329 bool setFaultStatusClear(uint8_t val);
YSI 0:9aa539a3d0f8 330
YSI 0:9aa539a3d0f8 331
YSI 0:9aa539a3d0f8 332 /**
YSI 0:9aa539a3d0f8 333 * @brief Sets bits in the configuration register zero for setting which of the two filter modes either 50Hz or 60Hz cancelation
YSI 0:9aa539a3d0f8 334 * @param val \li CR0_FILTER_OUT_60Hz (Power On Default value)
YSI 0:9aa539a3d0f8 335 * \li CR0_FILTER_OUT_50Hz
YSI 0:9aa539a3d0f8 336 * @return \li 1 on success
YSI 0:9aa539a3d0f8 337 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 338 */
YSI 0:9aa539a3d0f8 339 bool setEmiFilterFreq(uint8_t val);
YSI 0:9aa539a3d0f8 340
YSI 0:9aa539a3d0f8 341
YSI 0:9aa539a3d0f8 342 //*****************************************************************************
YSI 0:9aa539a3d0f8 343 //Functions for register CR1
YSI 0:9aa539a3d0f8 344 //*****************************************************************************
YSI 0:9aa539a3d0f8 345 /**
YSI 0:9aa539a3d0f8 346 * @brief Sets bits in the configuration register one for setting how many readings are taken
YSI 0:9aa539a3d0f8 347 * @param val \li CR1_AVG_TC_SAMPLES_1 (Power On Default value)
YSI 0:9aa539a3d0f8 348 * \li CR1_AVG_TC_SAMPLES_2
YSI 0:9aa539a3d0f8 349 * \li CR1_AVG_TC_SAMPLES_4
YSI 0:9aa539a3d0f8 350 * \li CR1_AVG_TC_SAMPLES_8
YSI 0:9aa539a3d0f8 351 * \li CR1_AVG_TC_SAMPLES_16
YSI 0:9aa539a3d0f8 352 * @return \li 1 on success
YSI 0:9aa539a3d0f8 353 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 354 */
YSI 0:9aa539a3d0f8 355 bool setNumSamplesAvg(uint8_t val);
YSI 0:9aa539a3d0f8 356
YSI 0:9aa539a3d0f8 357
YSI 0:9aa539a3d0f8 358 /**
YSI 0:9aa539a3d0f8 359 * @brief Sets bits in the configuration register one for setting which thermocouple type is going to be programmed into the MAX31856 for linearization of thermovoltage produced and temperature
YSI 0:9aa539a3d0f8 360 * @param val \li CR1_TC_TYPE_B
YSI 0:9aa539a3d0f8 361 * \li CR1_TC_TYPE_E
YSI 0:9aa539a3d0f8 362 * \li CR1_TC_TYPE_J
YSI 0:9aa539a3d0f8 363 * \li CR1_TC_TYPE_K (Power On Default value)
YSI 0:9aa539a3d0f8 364 * \li CR1_TC_TYPE_N
YSI 0:9aa539a3d0f8 365 * \li CR1_TC_TYPE_R
YSI 0:9aa539a3d0f8 366 * \li CR1_TC_TYPE_S
YSI 0:9aa539a3d0f8 367 * \li CR1_TC_TYPE_T
YSI 0:9aa539a3d0f8 368 * \li CR1_TC_TYPE_VOLT_MODE_GAIN_8
YSI 0:9aa539a3d0f8 369 * \li CR1_TC_TYPE_VOLT_MODE_GAIN_32
YSI 0:9aa539a3d0f8 370 * @return \li 1 on success
YSI 0:9aa539a3d0f8 371 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 372 */
YSI 0:9aa539a3d0f8 373 bool setThermocoupleType(uint8_t val);
YSI 0:9aa539a3d0f8 374
YSI 0:9aa539a3d0f8 375
YSI 0:9aa539a3d0f8 376 //*****************************************************************************
YSI 0:9aa539a3d0f8 377 //Functions for register MASK
YSI 0:9aa539a3d0f8 378 //*****************************************************************************
YSI 0:9aa539a3d0f8 379 /**
YSI 0:9aa539a3d0f8 380 * @brief Sets bits in the configuration register one for setting fault masks
YSI 0:9aa539a3d0f8 381 * @param val \li MASK_CJ_FAULT_THRESHOLD_HIGH
YSI 0:9aa539a3d0f8 382 * \li MASK_CJ_FAULT_THRESHOLD_LOW
YSI 0:9aa539a3d0f8 383 * \li MASK_TC_FAULT_THRESHOLD_HIGH
YSI 0:9aa539a3d0f8 384 * \li MASK_TC_FAULT_THRESHOLD_LOW
YSI 0:9aa539a3d0f8 385 * \li MASK_OVER_UNDER_VOLT_FAULT
YSI 0:9aa539a3d0f8 386 * \li MASK_OPEN_CIRCUIT_FAULT
YSI 0:9aa539a3d0f8 387 * @param enable \li 0 for disabling the mask in whichever option is selcted in parameter val
YSI 0:9aa539a3d0f8 388 * \li 1 for enabling the mask in whichever option is selcted in parameter val
YSI 0:9aa539a3d0f8 389 * @return \li 1 on success
YSI 0:9aa539a3d0f8 390 * \li 0 if there is an incorrect parameter that is passed in as parameter val
YSI 0:9aa539a3d0f8 391 */
YSI 0:9aa539a3d0f8 392 bool setFaultMasks(uint8_t val, bool enable);
YSI 0:9aa539a3d0f8 393
YSI 0:9aa539a3d0f8 394
YSI 0:9aa539a3d0f8 395 /**
YSI 0:9aa539a3d0f8 396 * @brief Sets bits in the configuration register one for setting thresholds that corespond to the fault mask settings
YSI 0:9aa539a3d0f8 397 * @param val \li MASK_CJ_FAULT_THRESHOLD_HIGH
YSI 0:9aa539a3d0f8 398 * \li MASK_CJ_FAULT_THRESHOLD_LOW
YSI 0:9aa539a3d0f8 399 * \li MASK_TC_FAULT_THRESHOLD_HIGH
YSI 0:9aa539a3d0f8 400 * \li MASK_TC_FAULT_THRESHOLD_LOW
YSI 0:9aa539a3d0f8 401 * @param temperature value that you want to program into a threshold register for temperatre
YSI 0:9aa539a3d0f8 402 * @return return value that was programmed into the threshold register
YSI 0:9aa539a3d0f8 403 */
YSI 0:9aa539a3d0f8 404 bool setFaultThresholds(uint8_t val, float temperature);
YSI 0:9aa539a3d0f8 405
YSI 0:9aa539a3d0f8 406
YSI 0:9aa539a3d0f8 407 //*****************************************************************************
YSI 0:9aa539a3d0f8 408 //Check Fault Status Functions
YSI 0:9aa539a3d0f8 409 //*****************************************************************************
YSI 0:9aa539a3d0f8 410 /**
YSI 0:9aa539a3d0f8 411 * @brief Check the fault stautus register to see if there is anything wrong with range of thermocouple temperature
YSI 0:9aa539a3d0f8 412 * whether outside opperating temperatures or if above/below thresholds that are set
YSI 0:9aa539a3d0f8 413 * @return \li 0 if no faults are present
YSI 0:9aa539a3d0f8 414 * \li 1 if Thermocouple temp is higher than the threshold
YSI 0:9aa539a3d0f8 415 * \li 2 if Thermocouple temp is lower than the threshold
YSI 0:9aa539a3d0f8 416 * \li 3 if Thermocouple temp is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 417 * \li 4 if Thermocouple temp is higher than the threshold && is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 418 * \li 5 if Thermocouple temp is lower than the threshold && is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 419 */
YSI 0:9aa539a3d0f8 420 uint8_t checkFaultsThermocoupleThresholds();
YSI 0:9aa539a3d0f8 421
YSI 0:9aa539a3d0f8 422
YSI 0:9aa539a3d0f8 423 /**
YSI 0:9aa539a3d0f8 424 * @brief Check the fault stautus register to see if there is anything wrong with range of cold junction temperature
YSI 0:9aa539a3d0f8 425 * whether outside opperating temperatures or if above/below thresholds that are set
YSI 0:9aa539a3d0f8 426 * @return \li 0 if no faults are present
YSI 0:9aa539a3d0f8 427 * \li 1 if Cold Junction temp is higher than the threshold
YSI 0:9aa539a3d0f8 428 * \li 2 if Cold Junction temp is lower than the threshold
YSI 0:9aa539a3d0f8 429 * \li 3 if Cold Junction temp is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 430 * \li 4 if Cold Junction temp is higher than the threshold && is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 431 * \li 5 if Cold Junction temp is lower than the threshold && is outside operating range of termocouple type
YSI 0:9aa539a3d0f8 432 */
YSI 0:9aa539a3d0f8 433 uint8_t checkFaultsColdJunctionThresholds();
YSI 0:9aa539a3d0f8 434
YSI 0:9aa539a3d0f8 435
YSI 0:9aa539a3d0f8 436 /**
YSI 0:9aa539a3d0f8 437 * @brief Check the fault stautus register to see if there is anything wrong with thermocouple connection to the MAX31856
YSI 0:9aa539a3d0f8 438 * @return \li 1 if no faults are present
YSI 0:9aa539a3d0f8 439 * \li 0 if there is a fault and there needs to be information printed to the console to help diagnose issues
YSI 0:9aa539a3d0f8 440 */
YSI 0:9aa539a3d0f8 441 bool checkFaultsThermocoupleConnection();
YSI 0:9aa539a3d0f8 442
YSI 0:9aa539a3d0f8 443
YSI 0:9aa539a3d0f8 444 //*****************************************************************************
YSI 0:9aa539a3d0f8 445 //General Functions
YSI 0:9aa539a3d0f8 446 //*****************************************************************************
YSI 0:9aa539a3d0f8 447 /**
YSI 0:9aa539a3d0f8 448 * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
YSI 0:9aa539a3d0f8 449 * \li Read the value of a register from contents of register matching the parameter read_address
YSI 0:9aa539a3d0f8 450 * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 8 bit parameter clear_bits
YSI 0:9aa539a3d0f8 451 * \li Set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter val
YSI 0:9aa539a3d0f8 452 * \li Rewrite to the register with the new 8 bit value to the register with the address with parameter write_address
YSI 0:9aa539a3d0f8 453 * @param read_address - Address of register to read the data before it's changed
YSI 0:9aa539a3d0f8 454 * @param write_address - Address of register to rewrite the changed data
YSI 0:9aa539a3d0f8 455 * @param clear_bits - Parameter that is
YSI 0:9aa539a3d0f8 456 * @param val - Bitfield that contains bits related to function specific settings
YSI 0:9aa539a3d0f8 457 * @return \li 1 on success
YSI 0:9aa539a3d0f8 458 */
YSI 0:9aa539a3d0f8 459 bool registerReadWriteByte(uint8_t read_address, uint8_t write_address, int clear_bits, uint8_t val);
YSI 0:9aa539a3d0f8 460
YSI 0:9aa539a3d0f8 461
YSI 0:9aa539a3d0f8 462 /**
YSI 0:9aa539a3d0f8 463 * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
YSI 0:9aa539a3d0f8 464 * \li Read the value of a register from contents of register matching the parameter read_address
YSI 0:9aa539a3d0f8 465 * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 8 bit parameter clear_bits
YSI 0:9aa539a3d0f8 466 * \li Set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter val
YSI 0:9aa539a3d0f8 467 * \li Write to the register with the new 8 bit value to the register with the address with parameter write_address
YSI 0:9aa539a3d0f8 468 * @param write_address - Address of register to rewrite the changed data
YSI 0:9aa539a3d0f8 469 * @param val - Byte of information that is going to be written to the regitser with the address that matches the parameter write_address
YSI 0:9aa539a3d0f8 470 * @return \li 1 on success
YSI 0:9aa539a3d0f8 471 */
YSI 0:9aa539a3d0f8 472 bool registerWriteByte(uint8_t write_address, uint8_t val);
YSI 0:9aa539a3d0f8 473
YSI 0:9aa539a3d0f8 474
YSI 0:9aa539a3d0f8 475 /**
YSI 0:9aa539a3d0f8 476 * @brief This function is to read current contents of register by passing in the address of the read address and return contents of the register
YSI 0:9aa539a3d0f8 477 * @param read_address - Address of register to read data from
YSI 0:9aa539a3d0f8 478 * @return \li byte contained in the address
YSI 0:9aa539a3d0f8 479 */
YSI 0:9aa539a3d0f8 480 uint8_t registerReadByte(uint8_t read_address);
YSI 0:9aa539a3d0f8 481
YSI 0:9aa539a3d0f8 482
YSI 0:9aa539a3d0f8 483 /**
YSI 0:9aa539a3d0f8 484 * @brief This function is to read current contents of register by passing in the address of the read address and return contents of the register
YSI 0:9aa539a3d0f8 485 * @param temperature - Float of value to offest the value of the cold junction offset by (must be between -8°C to +7.9375°C)
YSI 0:9aa539a3d0f8 486 * @return \li 1 on successfully updated coldjunction offset
YSI 0:9aa539a3d0f8 487 * \li 0 if parameter temperature does not fall between range -8°C to +7.9375°C
YSI 0:9aa539a3d0f8 488 */
YSI 0:9aa539a3d0f8 489 bool coldJunctionOffset(float temperature);
YSI 0:9aa539a3d0f8 490
YSI 0:9aa539a3d0f8 491
YSI 0:9aa539a3d0f8 492 private:
YSI 0:9aa539a3d0f8 493
YSI 0:9aa539a3d0f8 494 //*****************************************************************************
YSI 0:9aa539a3d0f8 495 //Private Functions
YSI 0:9aa539a3d0f8 496 //*****************************************************************************
YSI 0:9aa539a3d0f8 497 /** @brief Writes the chip seleect pin low to begin SPI communications */
YSI 0:9aa539a3d0f8 498 void spiEnable();
YSI 0:9aa539a3d0f8 499
YSI 0:9aa539a3d0f8 500
YSI 0:9aa539a3d0f8 501 /** @brief Writes the chip seleect pin high to end SPI communications */
YSI 0:9aa539a3d0f8 502 void spiDisable();
YSI 0:9aa539a3d0f8 503
YSI 0:9aa539a3d0f8 504 /** @brief Calculates minimum wait time for a conversion to take place */
YSI 0:9aa539a3d0f8 505 void calculateDelayTime();
YSI 0:9aa539a3d0f8 506
YSI 0:9aa539a3d0f8 507
YSI 0:9aa539a3d0f8 508 //*****************************************************************************
YSI 0:9aa539a3d0f8 509 //Private Members
YSI 0:9aa539a3d0f8 510 //*****************************************************************************
YSI 0:9aa539a3d0f8 511 /// SPI object
YSI 0:9aa539a3d0f8 512 SPI& spi;
YSI 0:9aa539a3d0f8 513
YSI 0:9aa539a3d0f8 514 /// Chip select pin for SPI communications
YSI 0:9aa539a3d0f8 515 DigitalOut ncs;
YSI 0:9aa539a3d0f8 516
YSI 0:9aa539a3d0f8 517 /// Number of samples the thermocouple is configured to average
YSI 0:9aa539a3d0f8 518 uint8_t samples;
YSI 0:9aa539a3d0f8 519
YSI 0:9aa539a3d0f8 520 /// 0=thermocouple is set to one of 8 thermocouple types and 1=Thermocouple is configured to report in voltage mode
YSI 0:9aa539a3d0f8 521 bool voltage_mode;
YSI 0:9aa539a3d0f8 522
YSI 0:9aa539a3d0f8 523 /// 0=60Hz and 1=50Hz
YSI 0:9aa539a3d0f8 524 bool filter_mode;
YSI 0:9aa539a3d0f8 525
YSI 0:9aa539a3d0f8 526 /// 0=MAX31856 is off, so no conversion is taking place currently and 1=Always On and converting
YSI 0:9aa539a3d0f8 527 bool conversion_mode;
YSI 0:9aa539a3d0f8 528
YSI 0:9aa539a3d0f8 529 /// 0=cold junction is disabled and 1=cold junction is enabled
YSI 0:9aa539a3d0f8 530 bool cold_junction_enabled;
YSI 0:9aa539a3d0f8 531
YSI 0:9aa539a3d0f8 532 ///Define a return val for all boolean functions
YSI 0:9aa539a3d0f8 533 bool return_val;
YSI 0:9aa539a3d0f8 534
YSI 0:9aa539a3d0f8 535 ///Used to figure out when a new conversion is ready to go
YSI 0:9aa539a3d0f8 536 uint32_t lastReadTime;
YSI 0:9aa539a3d0f8 537
YSI 0:9aa539a3d0f8 538 ///How many conversions have taken place since conversion mode was switched into auto mode
YSI 0:9aa539a3d0f8 539 ///Also this value should be 0 if the mode is in oneshot mode
YSI 0:9aa539a3d0f8 540 uint32_t thermocouple_conversion_count;
YSI 0:9aa539a3d0f8 541
YSI 0:9aa539a3d0f8 542 ///time in milliseconds that is needed minimum for a new conversion to take place
YSI 0:9aa539a3d0f8 543 uint32_t conversion_time;
YSI 0:9aa539a3d0f8 544
YSI 0:9aa539a3d0f8 545 float prev_TC;
YSI 0:9aa539a3d0f8 546 };
YSI 0:9aa539a3d0f8 547
YSI 0:9aa539a3d0f8 548 #endif /* __MAX31856_H_ */