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-28
Revision:
3:a99a4367c909
Parent:
2:296485923589
Child:
4:589401418bf0

File content as of revision 3:a99a4367c909:

#ifndef MAX31856_h
#define MAX31856_h

#include "mbed.h"
/** 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




///Define all the addresses of the registers in the MAX31856
#define ADDRESS_CR0_READ                   0x00UL))         //Factory Default 00h
#define ADDRESS_CR0_WRITE                  0x80UL))
#define ADDRESS_CR1_READ                   0x01UL))         //Factory Default 03h
#define ADDRESS_CR1_WRITE                  0x81UL))
#define ADDRESS_MASK_READ                  ((uint8_t)(0x02UL))         //Factory Default FFh
#define ADDRESS_MASK_WRITE                 ((uint8_t)(0x82UL))
#define ADDRESS_CJHF_READ                  ((uint8_t)(0x03UL))         //Factory Default 7Fh
#define ADDRESS_CJHF_WRITE                 ((uint8_t)(0x83UL))
#define ADDRESS_CJLF_READ                  ((uint8_t)(0x04UL))         //Factory Default C0h
#define ADDRESS_CJLF_WRITE                 ((uint8_t)(0x84UL))
#define ADDRESS_LTHFTH_READ                ((uint8_t)(0x05UL))         //Factory Default 7Fh
#define ADDRESS_LTHFTH_WRITE               ((uint8_t)(0x85UL))
#define ADDRESS_LTHFTL_READ                ((uint8_t)(0x06UL))         //Factory Default FFh
#define ADDRESS_LTHFTL_WRITE               ((uint8_t)(0x86UL))
#define ADDRESS_LTLFTH_READ                ((uint8_t)(0x07UL))         //Factory Default 80h
#define ADDRESS_LTLFTH_WRITE               ((uint8_t)(0x87UL))
#define ADDRESS_LTLFTL_READ                ((uint8_t)(0x08UL))         //Factory Default 00h
#define ADDRESS_LTLFTL_WRITE               ((uint8_t)(0x88UL))
#define ADDRESS_CJTO_READ                  ((uint8_t)(0x09UL))         //Factory Default 00h
#define ADDRESS_CJTO_WRITE                 ((uint8_t)(0x89UL))
#define ADDRESS_CJTH_READ                  ((uint8_t)(0x0AUL))         //Factory Default 00h
#define ADDRESS_CJTH_WRITE                 ((uint8_t)(0x8AUL))
#define ADDRESS_CJTL_READ                  ((uint8_t)(0x0BUL))         //Factory Default 00h
#define ADDRESS_CJTL_WRITE                 ((uint8_t)(0x8BUL))
#define ADDRESS_LTCBH_READ                 ((uint8_t)(0x0CUL))
#define ADDRESS_LTCBM_READ                 ((uint8_t)(0x0DUL))
#define ADDRESS_LTCBL_READ                 ((uint8_t)(0x0EUL))
#define ADDRESS_SR_READ                    ((uint8_t)(0x0FUL))


///Define parameters for control register zero (CR0)

#define CR0_CONV_MODE_NORMALLY_OFF         ((uint8_t)(0x00UL << 7))    //Power On Default value
#define CR0_CONV_MODE_NORMALLY_ON          ((uint8_t)(0x01UL << 7))

#define CR0_1_SHOT_MODE_NO_CONVERSION      ((uint8_t)(0x00UL << 6))    //defaults to this value
#define CR0_1_SHOT_MODE_ONE_CONVERSION     ((uint8_t)(0x01UL << 6))    //^

#define CR0_OC_DETECT_DISABLED             ((uint8_t)(0x00UL << 4))    //
#define CR0_OC_DETECT_ENABLED_R_LESS_5k    ((uint8_t)(0x01UL << 4))
#define CR0_OC_DETECT_ENABLED_TC_LESS_2ms  ((uint8_t)(0x02UL << 4))
#define CR0_OC_DETECT_ENABLED_TC_MORE_2ms  ((uint8_t)(0x03UL << 4))

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

#define CR0_FAULT_MODE_COMPARATOR          ((uint8_t)(0x00UL << 2))    //Power On Default value
#define CR0_FAULT_MODE_INTERUPT            ((uint8_t)(0x01UL << 2)) 

#define CR0_FAULTCLR_DEFAULT_VAL           ((uint8_t)(0x00UL << 1))    //defaults to this value
#define CR0_FAULTCLR_RETURN_FAULTS_TO_ZERO ((uint8_t)(0x01UL << 1))    //^

#define CR0_FILTER_OUT_60Hz                ((uint8_t)(0x00UL))         //Preset value
#define CR0_FILTER_OUT_50Hz                ((uint8_t)(0x01UL))         //^



///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               ((uint8_t)(0x00UL << 4))    //Power on default value
#define CR1_AVG_TC_SAMPLES_2               ((uint8_t)(0x01UL << 4))
#define CR1_AVG_TC_SAMPLES_4               ((uint8_t)(0x02UL << 4))
#define CR1_AVG_TC_SAMPLES_8               ((uint8_t)(0x03UL << 4))
#define CR1_AVG_TC_SAMPLES_16              ((uint8_t)(0x04UL << 4))

/** Define which type of thermocouple the MAX31856 is using. This is for lineariztion purposes*/
#define CR1_TC_TYPE_B                      ((uint8_t)(0x00UL))
#define CR1_TC_TYPE_E                      ((uint8_t)(0x01UL))
#define CR1_TC_TYPE_J                      ((uint8_t)(0x02UL))
#define CR1_TC_TYPE_K                      ((uint8_t)(0x03UL))         //Power on default value
#define CR1_TC_TYPE_N                      ((uint8_t)(0x04UL))
#define CR1_TC_TYPE_R                      ((uint8_t)(0x05UL))
#define CR1_TC_TYPE_S                      ((uint8_t)(0x06UL))
#define CR1_TC_TYPE_T                      ((uint8_t)(0x07UL))
#define CR1_TC_TYPE_VOLT_MODE_GAIN_8       ((uint8_t)(0x08UL))
#define CR1_TC_TYPE_VOLT_MODE_GAIN_32      ((uint8_t)(0x0CUL))

///Define parameters for the mask register (MASK)
#define MASK_CJ_FAULT_THRESHOLD_HIGH       ((uint8_t)(0x01UL << 5))
#define MASK_CJ_FAULT_THRESHOLD_LOW        ((uint8_t)(0x01UL << 4))
#define MASK_TC_FAULT_THRESHOLD_HIGH       ((uint8_t)(0x01UL << 3))
#define MASK_TC_FAULT_THRESHOLD_LOW        ((uint8_t)(0x01UL << 2))
#define MASK_OVER_UNDER_VOLT_FAULT         ((uint8_t)(0x01UL << 1))
#define MASK_OPEN_CIRCUIT_FAULT            ((uint8_t)(0x01UL))


///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                   ~((uint8_t)(0x01UL << 7))
#define CR0_CLEAR_BITS_6                   ~((uint8_t)(0x01UL << 6))
#define CR0_CLEAR_BITS_5_4                 ~((uint8_t)(0x03UL << 4))
#define CR0_CLEAR_BITS_3                   ~((uint8_t)(0x01UL << 3))
#define CR0_CLEAR_BITS_2                   ~((uint8_t)(0x01UL << 2))
#define CR0_CLEAR_BITS_1                   ~((uint8_t)(0x01UL << 1))
#define CR0_CLEAR_BITS_0                   ~((uint8_t)(0x01UL))

#define CR1_CLEAR_BITS_6_4                 ~((uint8_t)(0x07UL << 4))
#define CR1_CLEAR_BITS_3_0                 ~((uint8_t)(0x0FUL))


#define MASK_CLEAR_BITS_5                      ~((uint8_t)(0x01UL << 5))
#define MASK_CLEAR_BITS_4                      ~((uint8_t)(0x01UL << 4))
#define MASK_CLEAR_BITS_3                      ~((uint8_t)(0x01UL << 3))
#define MASK_CLEAR_BITS_2                      ~((uint8_t)(0x01UL << 2))
#define MASK_CLEAR_BITS_1                      ~((uint8_t)(0x01UL << 1))
#define MASK_CLEAR_BITS_0                      ~((uint8_t)(0x01UL))
//The following are predefined times that the MAX31856 needs to wait in between 
#define MIN_TIME_BETWEEN_READINGS



class MAX31856
{
//    public:
//        struct time_adjusting_factors {
//            int samples;  // 1, 2, 4, 8, or 16 samples are the possible values
//            bool filter;  // 0=60Hz,   1=50Hz
//            
//        } conversion_time;

    public:
        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); 
//        float read(temperature_type_t type = THERMOCOUPLE_T);
        
        void spiEnable();
        void spiDisable();
        float readTC();
        float readCJ();
        
        //Functions for register CR0
        bool setConversionMode(uint8_t val);
        bool setOneShotMode(uint8_t val);
        bool setOpenCircuitFaultDetection(uint8_t val);
        bool setColdJunctionDisable(uint8_t val);
        bool setFaultMode(uint8_t val);
        bool setFaultStatusClear(uint8_t val);
        bool setEmiFilterFreq(uint8_t val);
        
        //Functions for register CR1
        bool setNumSamplesAvg(uint8_t val);
        bool setThermocoupleType(uint8_t val);
        
        //Functions for register MASK
        bool setFaultMasks(uint8_t val, bool enable);
        float setFaultThresholds(uint8_t val, bool enable_mask, float temperature);
        
        //General Functions
        bool registerReadWriteByte(uint8_t read_address, uint8_t write_address, uint8_t clear_bits, uint8_t val);
        bool registerWriteByte(uint8_t write_address, uint8_t val);
        int8_t twosComplimentToSigned8(int8_t temp);
        int16_t twosComplimentToSigned16(int16_t temp);
//        void printSetting(string register_bits, string register_info);
        
        //Define a return val for all boolean functions
        bool return_val;
        
  private:
    SPI& spi;
    DigitalOut ncs;
    uint8_t samples;
    bool voltage_mode;                  // used to determine what kind of read of the thermalcouple temperature will take place
    bool filter_mode;                   // 0=60Hz  and  1=50Hz
    bool conversion_mode;
    bool cold_junction_enabled;
    
    
    float thermocoupleT;
    float internalT;
    uint32_t lastReadTime;
};

#endif // __MAX31855_H__\