Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MAX31856_example_program by
Diff: MAX31856.h
- Revision:
- 8:8723d0006097
- Parent:
- 7:2e45068189b1
- Child:
- 9:2d284cc2f65c
--- a/MAX31856.h Mon Jul 31 18:46:49 2017 +0000 +++ b/MAX31856.h Tue Aug 01 03:29:15 2017 +0000 @@ -35,7 +35,19 @@ #include "mbed.h" +/* 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 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 @@ -65,8 +77,9 @@ #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 @@ -87,21 +100,13 @@ #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 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 @@ -120,7 +125,9 @@ #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 @@ -128,8 +135,9 @@ #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 +//***************************************************************************** +//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 +//***************************************************************************** #define CR0_CLEAR_BITS_7 ~0x80 #define CR0_CLEAR_BITS_6 ~0x40 #define CR0_CLEAR_BITS_5_4 ~0x30 @@ -141,7 +149,6 @@ #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 @@ -149,8 +156,15 @@ #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 +//***************************************************************************** +///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 + + /** * @brief Library for the MAX31856\n @@ -166,15 +180,27 @@ * #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 - * + * // Hardware serial port + * Serial serial(USBTX, USBRX); + * + * //SPI communications + * SPI spi(SPIO MOSI,SPIO MISO,SPIO SCK); + * + * //Thermocouples + * MAX31856 Thermocouple1(spi, CHIPSELECT); + * MAX31856 Thermocouple2(spi, CHIPSELECT); + * + * * int main(void) * { - * //use sensor + * float temperature_TC_1, temperature_CJ_1; + * while(true) + * { + * temperature_TC_1=Thermocouple1.readTC(); + * temperature_CJ_1=Thermocouple1.readCJ(); + * serial.printf("MAX31856 TC = %f Celsius MAX31856 CJ = %f Celsius \n\r",temperature_TC_1,temperature_CJ_1); + * wait(1.0); + * } * } * @endcode */ @@ -187,26 +213,19 @@ *** 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 - - - - - - - +/** +* MAX31856 Class +*/ class MAX31856 { public: - +//***************************************************************************** +//Constructor and Destructor for the class +//***************************************************************************** /** - * @brief Constructor using reference to I2C object + * @brief Constructor to create MAX31856 object with SPI information as well as preconfiguration parameter settings in configuration registers Zero and One * @param _spi - Reference to SPI object * @param _ncs - Chip Select for SPI comunications with the oject * @param _type - Type of thermocouple used @@ -216,9 +235,11 @@ */ 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 //***************************************************************************** @@ -241,51 +262,72 @@ //***************************************************************************** /** * @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 + * @param val \li CR0_CONV_MODE_NORMALLY_OFF (Power On Default value) + * \li CR0_CONV_MODE_NORMALLY_ON + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_1_SHOT_MODE_NO_CONVERSION (Power On Default value) + * \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) + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_OC_DETECT_DISABLED (Power On Default value) + * \li CR0_OC_DETECT_ENABLED_R_LESS_5k + * \li CR0_OC_DETECT_ENABLED_TC_LESS_2ms + * \li CR0_OC_DETECT_ENABLED_TC_MORE_2ms + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_COLD_JUNC_ENABLE (Power On Default value) + * \li CR0_COLD_JUNC_DISABLE + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_FAULT_MODE_COMPARATOR (Power On Default value) + * \li CR0_FAULT_MODE_INTERUPT + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_FAULTCLR_DEFAULT_VAL (Power On Default value) + * \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) + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR0_FILTER_OUT_60Hz (Power On Default value) + * \li CR0_FILTER_OUT_50Hz + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ bool setEmiFilterFreq(uint8_t val); @@ -295,14 +337,31 @@ //***************************************************************************** /** * @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 + * @param val \li CR1_AVG_TC_SAMPLES_1 (Power On Default value) + * \li CR1_AVG_TC_SAMPLES_2 + * \li CR1_AVG_TC_SAMPLES_4 + * \li CR1_AVG_TC_SAMPLES_8 + * \li CR1_AVG_TC_SAMPLES_16 + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li CR1_TC_TYPE_B + * \li CR1_TC_TYPE_E + * \li CR1_TC_TYPE_J + * \li CR1_TC_TYPE_K (Power On Default value) + * \li CR1_TC_TYPE_N + * \li CR1_TC_TYPE_R + * \li CR1_TC_TYPE_S + * \li CR1_TC_TYPE_T + * \li CR1_TC_TYPE_VOLT_MODE_GAIN_8 + * \li CR1_TC_TYPE_VOLT_MODE_GAIN_32 + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ bool setThermocoupleType(uint8_t val); @@ -312,16 +371,71 @@ //***************************************************************************** /** * @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 + * @param val \li MASK_CJ_FAULT_THRESHOLD_HIGH + * \li MASK_CJ_FAULT_THRESHOLD_LOW + * \li MASK_TC_FAULT_THRESHOLD_HIGH + * \li MASK_TC_FAULT_THRESHOLD_LOW + * \li MASK_OVER_UNDER_VOLT_FAULT + * \li MASK_OPEN_CIRCUIT_FAULT + * @param enable \li 0 for disabling the mask in whichever option is selcted in parameter val + * \li 1 for enabling the mask in whichever option is selcted in parameter val + * @return \li 1 on success + * \li 0 if there is an incorrect parameter that is passed in as parameter val */ 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 + * @param val \li MASK_CJ_FAULT_THRESHOLD_HIGH + * \li MASK_CJ_FAULT_THRESHOLD_LOW + * \li MASK_TC_FAULT_THRESHOLD_HIGH + * \li MASK_TC_FAULT_THRESHOLD_LOW + * \li MASK_OVER_UNDER_VOLT_FAULT + * \li MASK_OPEN_CIRCUIT_FAULT + * @param enable \li 0 for disabling the mask in whichever option is selcted in parameter val + * \li 1 for enabling the mask in whichever option is selcted in parameter val + * @param temperature value that you want to program into a threshold register for temperatre + * @return return value that was programmed into the threshold register */ - float setFaultThresholds(uint8_t val, bool enable_mask, float temperature); + float setFaultThresholds(uint8_t val, bool enable, float temperature); + + +//***************************************************************************** +//Check Fault Status Functions +//***************************************************************************** + /** + * @brief Check the fault stautus register to see if there is anything wrong with range of thermocouple temperature + * whether outside opperating temperatures or if above/below thresholds that are set + * @return \li 0 if no faults are present + * \li 1 if Thermocouple temp is higher than the threshold + * \li 2 if Thermocouple temp is lower than the threshold + * \li 3 if Thermocouple temp is outside operating range of termocouple type + * \li 4 if Thermocouple temp is higher than the threshold && is outside operating range of termocouple type + * \li 5 if Thermocouple temp is lower than the threshold && is outside operating range of termocouple type + */ + uint8_t checkFaultsThermocoupleThresholds(); + + + /** + * @brief Check the fault stautus register to see if there is anything wrong with range of cold junction temperature + * whether outside opperating temperatures or if above/below thresholds that are set + * @return \li 0 if no faults are present + * \li 1 if Cold Junction temp is higher than the threshold + * \li 2 if Cold Junction temp is lower than the threshold + * \li 3 if Cold Junction temp is outside operating range of termocouple type + * \li 4 if Cold Junction temp is higher than the threshold && is outside operating range of termocouple type + * \li 5 if Cold Junction temp is lower than the threshold && is outside operating range of termocouple type + */ + uint8_t checkFaultsColdJunctionThresholds(); + + + /** + * @brief Check the fault stautus register to see if there is anything wrong with thermocouple connection to the MAX31856 + * @return \li 1 if no faults are present + * \li 0 if there is a fault and there needs to be information printed to the console to help diagnose issues + */ + bool checkFaultsThermocoupleConnection(); //***************************************************************************** @@ -329,52 +443,54 @@ //***************************************************************************** /** * @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" + * \li Read the value of a register from contents of register matching the parameter read_address + * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 8 bit parameter clear_bits + * \li Set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter val + * \li Rewrite to the register with the new 8 bit 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 + * @return \li 1 on success */ 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 + * \li Read the value of a register from contents of register matching the parameter read_address + * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 8 bit parameter clear_bits + * \li Set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter val + * \li Write to the register with the new 8 bit value to the register with the address with parameter write_address * @param write_address - Address of register to rewrite the changed data - * @return 1 on success, and zero on failure + * @param val - Byte of information that is going to be written to the regitser with the address that matches the parameter write_address + * @return \li 1 on success */ 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 + * @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 + * @param read_address - Address of register to read data from + * @return \li byte contained in the address */ - int8_t twosComplimentToSigned8(int8_t temp); - - + uint8_t registerReadByte(uint8_t read_address); + + /** - * @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 + * @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 + * @param temperature - Float of value to offest the value of the cold junction offset by (must be between -8°C to +7.9375°C) + * @return \li 1 on successfully updated coldjunction offset + * \li 0 if parameter temperature does not fall between range -8°C to +7.9375°C */ - int16_t twosComplimentToSigned16(int16_t temp); -// void printSetting(string register_bits, string register_info); + bool coldJunctionOffset(float temperature); - private: //***************************************************************************** //Private Functions //***************************************************************************** - /** @brief Writes the chip seleect pin low to begin SPI communications */ void spiEnable(); @@ -382,6 +498,9 @@ /** @brief Writes the chip seleect pin high to end SPI communications */ void spiDisable(); + /** @brief Calculates minimum wait time for a conversion to take place */ + void calculateDelayTime(); + //***************************************************************************** //Private Members @@ -389,7 +508,7 @@ /// SPI object SPI& spi; - /// Chip select + /// Chip select pin for SPI communications DigitalOut ncs; /// Number of samples the thermocouple is configured to average @@ -401,7 +520,7 @@ /// 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 + /// 0=MAX31856 is off, so no conversion is taking place currently and 1=Always On and converting bool conversion_mode; /// 0=cold junction is disabled and 1=cold junction is enabled @@ -412,6 +531,48 @@ ///Used to figure out when a new conversion is ready to go uint32_t lastReadTime; + + ///How many conversions have taken place since conversion mode was switched into auto mode + ///Also this value should be 0 if the mode is in oneshot mode + uint32_t thermocouple_conversion_count; + + ///time in milliseconds that is needed minimum for a new conversion to take place + uint32_t conversion_time; }; -#endif /* __MAX31856_H_ */ \ No newline at end of file +#endif /* __MAX31856_H_ */ + + + +//***************************************************************************** +//EXTRA +//***************************************************************************** +// /** +// * @brief Check the fault stautus register to see if there is anything wrong with thermocouple connections or if there are any faults +// * @return \li 1 if no faults are present +// * \li 0 if there is a fault and there needs to be information printed to the console to help diagnose issues +// */ +// bool checkFaultsAll(); +// +// /** +// * @brief This function is to read current contents of register, manipulate the contents, then rewrite the specific register\n +// * \li Read the value of a register from contents of register matching the parameter read_address +// * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 8 bit parameter clear_bits +// * \li Set the bits of interest in the 8 bit value by bitwise ORing the value from step two with parameter val +// * \li Rewrite to the register with the new 8 bit value to the register with the address with parameter write_address +// * @param temp - value to be converted +// * @return \li converted 2's complement value to 8 bit signed integer +// */ +// 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 +// * \li Read the value of a register from contents of register matching the parameter read_address +// * \li Clear the bits needed to be changed by bitwise ANDing the read value with the 16 bit parameter clear_bits +// * \li Set the bits of interest in the 16 bit value by bitwise ORing the value from step two with parameter val +// * \li Rewrite to the register with the new 16 bit value to the register with the address with parameter write_address +// * @param temp - value to be converted +// * @return \li converted 2's complement value to 16 bit signed integer +// */ +// int16_t twosComplimentToSigned16(int16_t temp);