Maxim Integrated / Mbed 2 deprecated MAX31856_example_program

Dependencies:   MAX31856 mbed

Fork of MAX31856_example_program by Central Applications - Mbed Code repo

MAX31856.h

Committer:
DevinAlexander
Date:
2017-07-31
Revision:
7:2e45068189b1
Parent:
6:e1200ae7d6a3
Child:
8:8723d0006097

File content as of revision 7:2e45068189b1:

/*******************************************************************************
 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *******************************************************************************
 */
#ifndef MAX31856_h
#define MAX31856_h
#include "mbed.h"


//Define all the addresses of the registers in the MAX31856
#define ADDRESS_CR0_READ                   0x00         //Factory Default 00h
#define ADDRESS_CR0_WRITE                  0x80
#define ADDRESS_CR1_READ                   0x01         //Factory Default 03h
#define ADDRESS_CR1_WRITE                  0x81
#define ADDRESS_MASK_READ                  0x02         //Factory Default FFh
#define ADDRESS_MASK_WRITE                 0x82
#define ADDRESS_CJHF_READ                  0x03         //Factory Default 7Fh
#define ADDRESS_CJHF_WRITE                 0x83
#define ADDRESS_CJLF_READ                  0x04         //Factory Default C0h
#define ADDRESS_CJLF_WRITE                 0x84
#define ADDRESS_LTHFTH_READ                0x05         //Factory Default 7Fh
#define ADDRESS_LTHFTH_WRITE               0x85
#define ADDRESS_LTHFTL_READ                0x06         //Factory Default FFh
#define ADDRESS_LTHFTL_WRITE               0x86
#define ADDRESS_LTLFTH_READ                0x07         //Factory Default 80h
#define ADDRESS_LTLFTH_WRITE               0x87
#define ADDRESS_LTLFTL_READ                0x08         //Factory Default 00h
#define ADDRESS_LTLFTL_WRITE               0x88
#define ADDRESS_CJTO_READ                  0x09         //Factory Default 00h
#define ADDRESS_CJTO_WRITE                 0x89
#define ADDRESS_CJTH_READ                  0x0A         //Factory Default 00h
#define ADDRESS_CJTH_WRITE                 0x8A
#define ADDRESS_CJTL_READ                  0x0B         //Factory Default 00h
#define ADDRESS_CJTL_WRITE                 0x8B
#define ADDRESS_LTCBH_READ                 0x0C
#define ADDRESS_LTCBM_READ                 0x0D
#define ADDRESS_LTCBL_READ                 0x0E
#define ADDRESS_SR_READ                    0x0F


//Define parameters for control register zero (CR0)
#define CR0_CONV_MODE_NORMALLY_OFF         0x00    //Power On Default value
#define CR0_CONV_MODE_NORMALLY_ON          0x80

#define CR0_1_SHOT_MODE_NO_CONVERSION      0x00    //defaults to this value
#define CR0_1_SHOT_MODE_ONE_CONVERSION     0x40    //^

#define CR0_OC_DETECT_DISABLED             0x00
#define CR0_OC_DETECT_ENABLED_R_LESS_5k    0x10
#define CR0_OC_DETECT_ENABLED_TC_LESS_2ms  0x20
#define CR0_OC_DETECT_ENABLED_TC_MORE_2ms  0x30

#define CR0_COLD_JUNC_ENABLE               0x00    //Power On Default value
#define CR0_COLD_JUNC_DISABLE              0x08    //speed of conversion is sped up by 25ms when this optionis selected (Disable the cold junc)

#define CR0_FAULT_MODE_COMPARATOR          0x00    //Power On Default value
#define CR0_FAULT_MODE_INTERUPT            0x04 

#define CR0_FAULTCLR_DEFAULT_VAL           0x00    //defaults to this value
#define CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO 0x02    //^

#define CR0_FILTER_OUT_60Hz                0x00         //Preset value
#define CR0_FILTER_OUT_50Hz                0x01         //^



//Define parameters for control register one (CR1)
/* Adding Samples increases the conversion time and reduces noise.
    Typical conversion times:
        1-shot or first conversion in Auto mode:
            = t_Conversion + (samples-1)*33.33mS (60Hz rejection)
            = t_Conversion + (samples-1)*40.00mS (50Hz rejection)
        2 thru n conversions in Auto mode:
            = t_Conversion + (samples-1)*16.67mS (60Hz rejection)
            = t_Conversion + (samples-1)*20.00mS (50Hz rejection)
*/
#define CR1_AVG_TC_SAMPLES_1               0x00    //Power on default value
#define CR1_AVG_TC_SAMPLES_2               0x10
#define CR1_AVG_TC_SAMPLES_4               0x20
#define CR1_AVG_TC_SAMPLES_8               0x30
#define CR1_AVG_TC_SAMPLES_16              0x40

// Define which type of thermocouple the MAX31856 is using. This is for lineariztion purposes
#define CR1_TC_TYPE_B                      0x00
#define CR1_TC_TYPE_E                      0x01
#define CR1_TC_TYPE_J                      0x02
#define CR1_TC_TYPE_K                      0x03         //Power on default value
#define CR1_TC_TYPE_N                      0x04
#define CR1_TC_TYPE_R                      0x05
#define CR1_TC_TYPE_S                      0x06
#define CR1_TC_TYPE_T                      0x07
#define CR1_TC_TYPE_VOLT_MODE_GAIN_8       0x08
#define CR1_TC_TYPE_VOLT_MODE_GAIN_32      0x0C

//Define parameters for the mask register (MASK)
#define MASK_CJ_FAULT_THRESHOLD_HIGH       0x20
#define MASK_CJ_FAULT_THRESHOLD_LOW        0x10
#define MASK_TC_FAULT_THRESHOLD_HIGH       0x08
#define MASK_TC_FAULT_THRESHOLD_LOW        0x04
#define MASK_OVER_UNDER_VOLT_FAULT         0x02
#define MASK_OPEN_CIRCUIT_FAULT            0x01


//If these defined values are &= (ANDed) with the contents of a register, it will reset the bits pertaing to the naming convention to zero
#define CR0_CLEAR_BITS_7                   ~0x80
#define CR0_CLEAR_BITS_6                   ~0x40
#define CR0_CLEAR_BITS_5_4                 ~0x30
#define CR0_CLEAR_BITS_3                   ~0x08
#define CR0_CLEAR_BITS_2                   ~0x04
#define CR0_CLEAR_BITS_1                   ~0x02
#define CR0_CLEAR_BITS_0                   ~0x01

#define CR1_CLEAR_BITS_6_4                 ~0x70
#define CR1_CLEAR_BITS_3_0                 ~0x0F


#define MASK_CLEAR_BITS_5                  ~0x20
#define MASK_CLEAR_BITS_4                  ~0x10
#define MASK_CLEAR_BITS_3                  ~0x08
#define MASK_CLEAR_BITS_2                  ~0x04
#define MASK_CLEAR_BITS_1                  ~0x02
#define MASK_CLEAR_BITS_0                  ~0x01

//The following are predefined times that the MAX31856 needs to wait in between 
#define MIN_TIME_BETWEEN_READINGS

/**
 * @brief Library for the MAX31856\n
 * The MAX30205 temperature sensor accurately measures temperature and provide 
 * an overtemperature alarm/interrupt/shutdown output. This device converts the 
 * temperature measurements to digital form using a high-resolution, 
 * sigma-delta, analog-to-digital converter (ADC). Accuracy meets clinical 
 * thermometry specification of the ASTM E1112 when soldered on the final PCB. 
 * Communication is through an I2C-compatible 2-wire serial interface.
 *
 * @code
 * #include "mbed.h"
 * #include "MAX31856.h"
 * 
 *
 * //Get I2C instance
 * I2C i2cBus(I2C1_SDA, I2C1_SCL);
 *
 * //Get temp sensor instance
 * MAX30205 bodyTempSensor(i2cBus, 0x4D); //Constructor takes 7-bit slave adrs
 *
 * int main(void) 
 * {
 *     //use sensor
 * }
 * @endcode
 */



/** Please see pages 18-26 in the MAX31856 data sheet to see what register bit masks are needed to be set 
to achieve functionality desired. The data sheet can be found at 
            
           ***     https://datasheets.maximintegrated.com/en/ds/MAX31856.pdf      ***
*/

///Parameters that are used throughout the library
#define TC_MAX_VAL_FAULT                   1800
#define TC_MIN_VAL_FAULT                   -210
#define CJ_MAX_VAL_FAULT                   125
#define CJ_MIN_VAL_FAULT                   -55








class MAX31856
{

public:

    /**
    * @brief  Constructor using reference to I2C object
    * @param _spi - Reference to SPI object
    * @param _ncs - Chip Select for SPI comunications with the oject
    * @param _type - Type of thermocouple used
    * @param _fltr - Feature of the MAX31856 to filter out either 50Hz/60Hz from signal
    * @param _samples - How many samples are averaged for one conversion
    * @param _conversion_mode - Choose between always on and making conversions and off in between requests for a reading
    */
    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); 
    
    /** @brief Destructor */
    ~MAX31856(void);
    
//*****************************************************************************    
//Temperature Functions
//***************************************************************************** 
    /** 
    * @brief  Requests read of the thermocouple temperature
    * @return float of the converted thermocouple reading based on current configurations
    */
    float readTC();
    
    
    /** 
    * @brief  Requests read of the cold junction temperature
    * @return float of the converted artificial cold junction reading based on current configurations
    */
    float readCJ();

    
//*****************************************************************************    
//Functions for register CR0
//*****************************************************************************
    /** 
    * @brief  Sets bits in the configuration register zero for setting the rate of conversions
    * @param val -  CR0_CONV_MODE_NORMALLY_OFF
    *               CR0_CONV_MODE_NORMALLY_ON
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setConversionMode(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for enabling one conversion to take place
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setOneShotMode(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for configuring open circuit fault detection
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setOpenCircuitFaultDetection(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for disabling or enabling the Cold Junction
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setColdJunctionDisable(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for setting fault mode status
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setFaultMode(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for clearing fault status
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setFaultStatusClear(uint8_t val);
    
    
    /** 
    * @brief  Sets bits in the configuration register zero for setting which of the two filter modes either 50Hz or 60Hz cancelation
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setEmiFilterFreq(uint8_t val);
    
    
//*****************************************************************************    
//Functions for register CR1
//*****************************************************************************
    /** 
    * @brief  Sets bits in the configuration register one for setting how many readings are taken 
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setNumSamplesAvg(uint8_t val);
    
    
    /** 
    * @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
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setThermocoupleType(uint8_t val);
    

//*****************************************************************************    
//Functions for register MASK
//*****************************************************************************    
    /** 
    * @brief  Sets bits in the configuration register one for setting fault masks
    * @return 1 on success, and zero on failure or if there is an incorrect parameter is selected
    */
    bool setFaultMasks(uint8_t val, bool enable);
    
    
    /** 
    * @brief  Sets bits in the configuration register one for setting thresholds that corespond to the fault mask settings
    * @return return value that was programmed into the the threshold register t
    */
    float setFaultThresholds(uint8_t val, bool enable_mask, float temperature);
    
    
//*****************************************************************************    
//General Functions
//*****************************************************************************    
    /**
    * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
    * Read the value of a register from contents of register matching the parameter "read_address", clear the bits needed to be changed by bitwise ANDing the read value with the 8-bit parameter "clear_bits", set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter "val", then rewrite to the register with the new 8bit value to the register with the address with parameter "write_address"
    * @param read_address - Address of register to read the data before it's changed
    * @param write_address - Address of register to rewrite the changed data
    * @param clear_bits - Parameter that is 
    * @param val - Bitfield that contains bits related to function specific settings
    * @return 1 on success, and zero on failure
    */
    bool registerReadWriteByte(uint8_t read_address, uint8_t write_address, uint8_t clear_bits, uint8_t val);
    
    
    /**
    * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
    * Read the value of a register from contents of register matching the parameter "read_address", clear the bits needed to be changed by bitwise ANDing the read value with the 8-bit parameter "clear_bits", set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter "val", then rewrite to the register with the new 8bit value to the register with the address with parameter "write_address"
    * @param read_address - Address of register to read the data before it's changed
    * @param write_address - Address of register to rewrite the changed data
    * @return 1 on success, and zero on failure
    */
    bool registerWriteByte(uint8_t write_address, uint8_t val);
    
    
    /**
    * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
    * Read the value of a register from contents of register matching the parameter "read_address", clear the bits needed to be changed by bitwise ANDing the read value with the 8-bit parameter "clear_bits", set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter "val", then rewrite to the register with the new 8bit value to the register with the address with parameter "write_address"
    * @param temp - 
    * @return 1 on success, and zero on failure
    */
    int8_t twosComplimentToSigned8(int8_t temp);


    /**
    * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n
    * Read the value of a register from contents of register matching the parameter "read_address", clear the bits needed to be changed by bitwise ANDing the read value with the 8-bit parameter "clear_bits", set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter "val", then rewrite to the register with the new 8bit value to the register with the address with parameter "write_address"
    * @param temp - 
    * @return 1 on success, and zero on failure
    */
    int16_t twosComplimentToSigned16(int16_t temp);
//        void printSetting(string register_bits, string register_info);
    


private:

//*****************************************************************************    
//Private Functions
//*****************************************************************************    
    
    /** @brief  Writes the chip seleect pin low to begin SPI communications */
    void spiEnable();
    
    
    /** @brief  Writes the chip seleect pin high to end SPI communications */
    void spiDisable();
    
    
//*****************************************************************************    
//Private Members
//*****************************************************************************        
    /// SPI object
    SPI& spi;
    
    /// Chip select
    DigitalOut ncs;
    
    /// Number of samples the thermocouple is configured to average
    uint8_t samples;
    
    /// 0=thermocouple is set to one of 8 thermocouple types   and   1=Thermocouple is configured to report in voltage mode
    bool voltage_mode;
    
    /// 0=60Hz   and   1=50Hz
    bool filter_mode;
    
    /// 0=Always On and converting   and   1=MAX31856 is off, so no conversion is taking place currently
    bool conversion_mode;
    
    /// 0=cold junction is disabled   and   1=cold junction is enabled
    bool cold_junction_enabled;
    
    ///Define a return val for all boolean functions
    bool return_val;
    
    ///Used to figure out when a new conversion is ready to go
    uint32_t lastReadTime;
};

#endif  /* __MAX31856_H_ */