Library for the MAX31856 Precision Thermocouple to Digital Converter with Linearization

Dependents:   MAX31856_example_program

Fork of MAX31856 by Central Applications - Mbed Code repo

Committer:
DevinAlexander
Date:
Thu Sep 14 17:56:47 2017 +0000
Revision:
0:b714c6a7c969
Child:
1:a1bbb5c254f2
Initial demo of the MAX31856;

Who changed what in which revision?

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