C++ library, driver software code for the low-power small WLP package MAX31875 temperature sensor. Code supports one-shot, shut-down/standby, hysteresis, alarm limits.

Dependents:   MAX31875_Temperature_Sensor_Small_Package

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers max31875.h Source File

max31875.h

00001 /*******************************************************************************
00002 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************
00032 * @file MAX31875.h
00033 *******************************************************************************
00034 */
00035 #ifndef MAX31875_H
00036 #define MAX31875_H
00037 #include "mbed.h"
00038 
00039 #define MAX31875_NO_ERROR   0
00040 #define MAX31875_ERROR      -1
00041 
00042 #define MAX31875_REG_TEMPERATURE   0X00
00043 #define MAX31875_REG_CONFIGURATION 0X01
00044 #define MAX31875_REG_THYST         0X02
00045 #define MAX31875_REG_TOS           0X03
00046 #define MAX31875_REG_MAX           0X03
00047 
00048 #define MAX31875_CFG_ONE_SHOT_START   (1) /* Start one-shot measurement */
00049 
00050 #define MAX31875_CFG_CONV_RATE_0_25   (0x00 << 1) /* 0.25 conversions/sec */
00051 #define MAX31875_CFG_CONV_RATE_1      (0x01 << 1) /* 1.0 conversions/sec */
00052 #define MAX31875_CFG_CONV_RATE_4      (0x02 << 1) /* 4.0 conversions/sec */
00053 #define MAX31875_CFG_CONV_RATE_8      (0x03 << 1) /* 8.0 conversions/sec */
00054 #define MAX31875_WAIT_CONV_RATE_0_25 (4.0)
00055 #define MAX31875_WAIT_CONV_RATE_1    (1.0)
00056 #define MAX31875_WAIT_CONV_RATE_4    (0.25)
00057 #define MAX31875_WAIT_CONV_RATE_8    (0.125)
00058 
00059 #define MAX31875_CFG_RESOLUTION_9BIT  (0x00 << 5)
00060 #define MAX31875_CFG_RESOLUTION_10BIT (0x01 << 5)
00061 #define MAX31875_CFG_RESOLUTION_11BIT (0x02 << 5)
00062 #define MAX31875_CFG_RESOLUTION_12BIT (0x03 << 5)
00063 
00064 #define MAX31875_CFG_NORMAL_FORMAT    (0X00 << 7)
00065 #define MAX31875_CFG_EXTENDED_FORMAT  (0X01 << 7)
00066 
00067 #define MAX31875_CFG_CONTINUOUS       (0X00 << 8)
00068 #define MAX31875_CFG_SHUTDOWN         (0X01 << 8)
00069 
00070 #define MAX31875_CFG_COMPARATOR_MODE  (0X00 << 9)
00071 #define MAX31875_CFG_INTERRUPT_MODE   (0X01 << 9)
00072 
00073 
00074 #define MAX31875_CFG_FAULT_FILTER_1   (0x00 << 11)
00075 #define MAX31875_CFG_FAULT_FILTER_2   (0x01 << 11)
00076 #define MAX31875_CFG_FAULT_FILTER_4   (0x02 << 11)
00077 #define MAX31875_CFG_FAULT_FILTER_6   (0x03 << 11)
00078 
00079 #define MAX31875_CFG_OVER_TEMP_MASK   (0x80)
00080 
00081 
00082 #define MAX31875_I2C_SLAVE_ADR_R0 (0x90 >> 1) // code uses the 7 bit address
00083 #define MAX31875_I2C_SLAVE_ADR_R1 (0x92 >> 1)
00084 #define MAX31875_I2C_SLAVE_ADR_R2 (0x94 >> 1)
00085 #define MAX31875_I2C_SLAVE_ADR_R3 (0x96 >> 1)
00086 #define MAX31875_I2C_SLAVE_ADR_R4 (0x98 >> 1)
00087 #define MAX31875_I2C_SLAVE_ADR_R5 (0x9A >> 1)
00088 #define MAX31875_I2C_SLAVE_ADR_R6 (0x9C >> 1)
00089 #define MAX31875_I2C_SLAVE_ADR_R7 (0x9E >> 1)
00090 
00091 #define MAX31875_CF_NORMAL_FORMAT (0.00390625F)
00092 #define MAX31875_CF_EXTENDED_FORMAT (0.0078125F)
00093 
00094 /**
00095  * @brief Extremely small low-power temperature sensor.
00096  * @version 1.0000.0000
00097  *
00098  * @details The MAX31875 is a small WLP package temperature sensor.
00099  * It supports high, low triggers stored in EEPROM for hystersis
00100  * or limit alarms using comparator or interrupt mode.
00101  * The MAX31875 can operate in shutdown and one-shot mode for
00102  * extremely low power applications.
00103  * Tiny size of 0.84 x 0.84 x 0.35 mm.
00104  * Accuracy is +-1.5°C from +10°C to +45°C  (±0.5°C Typical),
00105  *             +-2.0°C from -10°C to +100°C (±0.6°C Typical),
00106  *             +-3.0°C from -20°C to +125°C (±1.0°C Typical),
00107  * I2C data rate of up to 1 MHz.
00108  *
00109  * @code 
00110  * #include "mbed.h"
00111  * #include "max32630fthr.h"
00112  * #include "max31875.h"
00113  * #include "USBSerial.h"
00114  * MAX32630FTHR pegasus(MAX32630FTHR::VIO_1V8); 
00115  * I2C i2cBus(P3_4, P3_5);
00116  * int main()
00117  * {
00118  *     uint16_t raw;
00119  *     float temperature;
00120  *     DigitalOut rLED(LED1, LED_OFF);
00121  *     DigitalOut gLED(LED2, LED_OFF);
00122  *     DigitalOut bLED(LED3, LED_OFF);
00123  *     gLED = LED_ON;
00124  *     MAX31875 temp_sensor(i2cBus, MAX31875_I2C_SLAVE_ADR_R0);
00125  *    i2cBus.frequency(1000000);
00126  *    temp_sensor.write_reg(uint16_t(MAX31875_CFG_CONV_RATE_8 | 
00127  *        MAX31875_CFG_RESOLUTION_12BIT), MAX31875_REG_CONFIGURATION);
00128  *    wait(MAX31875_WAIT_CONV_RATE_8);
00129  *     temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE);
00130  *     printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", 
00131  *             temperature, temp_sensor.celsius_to_fahrenheit(temperature));
00132  * }
00133  * @endcode
00134  */
00135 
00136 class MAX31875
00137 {
00138     public:
00139 
00140     /**********************************************************//**
00141      * @brief Constructor for MAX31875 Class.  
00142      * 
00143      * @details Allows user to use existing I2C object
00144      *
00145      * On Entry:
00146      *     @param[in] i2c_bus - pointer to existing I2C object
00147      *     @param[in] i2c_adrs - 7-bit slave address of MAX31875
00148      *
00149      * On Exit:
00150      *
00151      * @return None
00152      **************************************************************/
00153     MAX31875(I2C &i2c_bus, uint8_t slaveAddress);
00154  
00155     /**********************************************************//**
00156      * @brief Default destructor for MAX31875 Class.  
00157      *
00158      * @details Destroys I2C object if owner 
00159      *
00160      * On Entry:
00161      *
00162      * On Exit:
00163      *
00164      * @return None
00165      **************************************************************/
00166     ~MAX31875();
00167 
00168     /**
00169      * @brief  Read register of device at slave address
00170      * @param[out] value - Read data on success
00171      * @param reg - Register address
00172      * @return 0 on success, negative number on failure
00173      */
00174     int read_reg(uint16_t *value, char reg);
00175 
00176     /** 
00177      * @brief Reads the temperature registers
00178      * @param reg - the address of the temperature register
00179      * @return temprature in degrees Celsius
00180      */
00181     float read_reg_as_temperature(uint8_t reg);
00182 
00183     /** 
00184      * @brief Writes to the configuration register
00185      * @param cfg - configurate word
00186      * @return 0 on success, negative number on failure
00187      */
00188      int write_cfg(uint16_t cfg);
00189 
00190     /** 
00191      * @brief Writes to the THYST register
00192      * @param temperature - the temperature in Celsius degrees
00193      * @return 0 on success, negative number on failure
00194      */
00195      int write_trip_low(float temperature);
00196 
00197     /** 
00198      * @brief Writes to the TOS register
00199      * @param temperature - the temperature in Celsius degrees
00200      * @return 0 on success, negative number on failure
00201      */
00202      int write_trip_high(float temperature);
00203 
00204     /** 
00205      * @brief Converts Celsius degrees to Fahrenheit
00206      * @param temp_c - the temperature in Celsius degrees
00207      * @return temperature in Celsius degrees
00208      */
00209      float celsius_to_fahrenheit(float temp_c);
00210 
00211 protected: 
00212     /** @union max31875_raw_data
00213      * @brief union data structure for byte word manipulations
00214      */
00215     union max31875_raw_data {
00216         struct {
00217             uint8_t lsb;
00218             uint8_t msb;
00219         };
00220         uint16_t uwrd;
00221         int16_t swrd;
00222     };
00223 
00224     /** 
00225      * @brief Write a value to a register
00226      * @param value - value to write to the register
00227      * @param reg - register address
00228      * @return 0 on success, negative number on failure
00229      */
00230     int write_reg(uint16_t value, char reg);
00231 
00232 private:
00233     /** @var m_i2c
00234      * @brief I2C object
00235      */
00236     I2C &m_i2c;
00237     /** @var m_writeAddress, m_readAddress
00238      * @brief I2C address
00239      */
00240     uint8_t m_writeAddress, m_readAddress;
00241     /** @var m_writeAddress, m_readAddress
00242      * @brief m_extended_format
00243      */
00244     uint32_t m_extended_format;
00245 };
00246  
00247 #endif/* MAX31875_H */