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

Committer:
phonemacro
Date:
Mon Feb 04 05:32:58 2019 +0000
Revision:
2:048eff228fd8
Parent:
1:98499c3dace6
updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:f51465145906 1 /*******************************************************************************
phonemacro 0:f51465145906 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:f51465145906 3 *
phonemacro 0:f51465145906 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:f51465145906 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:f51465145906 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:f51465145906 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:f51465145906 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:f51465145906 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:f51465145906 10 *
phonemacro 0:f51465145906 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:f51465145906 12 * in all copies or substantial portions of the Software.
phonemacro 0:f51465145906 13 *
phonemacro 0:f51465145906 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:f51465145906 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:f51465145906 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:f51465145906 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:f51465145906 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:f51465145906 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:f51465145906 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:f51465145906 21 *
phonemacro 0:f51465145906 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:f51465145906 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:f51465145906 24 * Products, Inc. Branding Policy.
phonemacro 0:f51465145906 25 *
phonemacro 0:f51465145906 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:f51465145906 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:f51465145906 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:f51465145906 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:f51465145906 30 * ownership rights.
phonemacro 0:f51465145906 31 *******************************************************************************
phonemacro 0:f51465145906 32 * @file MAX31875.h
phonemacro 0:f51465145906 33 *******************************************************************************
phonemacro 0:f51465145906 34 */
phonemacro 0:f51465145906 35 #ifndef MAX31875_H
phonemacro 0:f51465145906 36 #define MAX31875_H
phonemacro 0:f51465145906 37 #include "mbed.h"
phonemacro 0:f51465145906 38
phonemacro 0:f51465145906 39 #define MAX31875_NO_ERROR 0
phonemacro 0:f51465145906 40 #define MAX31875_ERROR -1
phonemacro 0:f51465145906 41
phonemacro 0:f51465145906 42 #define MAX31875_REG_TEMPERATURE 0X00
phonemacro 0:f51465145906 43 #define MAX31875_REG_CONFIGURATION 0X01
phonemacro 0:f51465145906 44 #define MAX31875_REG_THYST 0X02
phonemacro 0:f51465145906 45 #define MAX31875_REG_TOS 0X03
phonemacro 0:f51465145906 46 #define MAX31875_REG_MAX 0X03
phonemacro 0:f51465145906 47
phonemacro 0:f51465145906 48 #define MAX31875_CFG_ONE_SHOT_START (1) /* Start one-shot measurement */
phonemacro 0:f51465145906 49
phonemacro 0:f51465145906 50 #define MAX31875_CFG_CONV_RATE_0_25 (0x00 << 1) /* 0.25 conversions/sec */
phonemacro 0:f51465145906 51 #define MAX31875_CFG_CONV_RATE_1 (0x01 << 1) /* 1.0 conversions/sec */
phonemacro 0:f51465145906 52 #define MAX31875_CFG_CONV_RATE_4 (0x02 << 1) /* 4.0 conversions/sec */
phonemacro 0:f51465145906 53 #define MAX31875_CFG_CONV_RATE_8 (0x03 << 1) /* 8.0 conversions/sec */
phonemacro 0:f51465145906 54 #define MAX31875_WAIT_CONV_RATE_0_25 (4.0)
phonemacro 0:f51465145906 55 #define MAX31875_WAIT_CONV_RATE_1 (1.0)
phonemacro 0:f51465145906 56 #define MAX31875_WAIT_CONV_RATE_4 (0.25)
phonemacro 0:f51465145906 57 #define MAX31875_WAIT_CONV_RATE_8 (0.125)
phonemacro 0:f51465145906 58
phonemacro 0:f51465145906 59 #define MAX31875_CFG_RESOLUTION_9BIT (0x00 << 5)
phonemacro 0:f51465145906 60 #define MAX31875_CFG_RESOLUTION_10BIT (0x01 << 5)
phonemacro 0:f51465145906 61 #define MAX31875_CFG_RESOLUTION_11BIT (0x02 << 5)
phonemacro 0:f51465145906 62 #define MAX31875_CFG_RESOLUTION_12BIT (0x03 << 5)
phonemacro 0:f51465145906 63
phonemacro 0:f51465145906 64 #define MAX31875_CFG_NORMAL_FORMAT (0X00 << 7)
phonemacro 0:f51465145906 65 #define MAX31875_CFG_EXTENDED_FORMAT (0X01 << 7)
phonemacro 0:f51465145906 66
phonemacro 0:f51465145906 67 #define MAX31875_CFG_CONTINUOUS (0X00 << 8)
phonemacro 0:f51465145906 68 #define MAX31875_CFG_SHUTDOWN (0X01 << 8)
phonemacro 0:f51465145906 69
phonemacro 0:f51465145906 70 #define MAX31875_CFG_COMPARATOR_MODE (0X00 << 9)
phonemacro 0:f51465145906 71 #define MAX31875_CFG_INTERRUPT_MODE (0X01 << 9)
phonemacro 0:f51465145906 72
phonemacro 0:f51465145906 73
phonemacro 0:f51465145906 74 #define MAX31875_CFG_FAULT_FILTER_1 (0x00 << 11)
phonemacro 0:f51465145906 75 #define MAX31875_CFG_FAULT_FILTER_2 (0x01 << 11)
phonemacro 0:f51465145906 76 #define MAX31875_CFG_FAULT_FILTER_4 (0x02 << 11)
phonemacro 0:f51465145906 77 #define MAX31875_CFG_FAULT_FILTER_6 (0x03 << 11)
phonemacro 0:f51465145906 78
phonemacro 0:f51465145906 79 #define MAX31875_CFG_OVER_TEMP_MASK (0x80)
phonemacro 0:f51465145906 80
phonemacro 0:f51465145906 81
phonemacro 0:f51465145906 82 #define MAX31875_I2C_SLAVE_ADR_R0 (0x90 >> 1) // code uses the 7 bit address
phonemacro 0:f51465145906 83 #define MAX31875_I2C_SLAVE_ADR_R1 (0x92 >> 1)
phonemacro 0:f51465145906 84 #define MAX31875_I2C_SLAVE_ADR_R2 (0x94 >> 1)
phonemacro 0:f51465145906 85 #define MAX31875_I2C_SLAVE_ADR_R3 (0x96 >> 1)
phonemacro 0:f51465145906 86 #define MAX31875_I2C_SLAVE_ADR_R4 (0x98 >> 1)
phonemacro 0:f51465145906 87 #define MAX31875_I2C_SLAVE_ADR_R5 (0x9A >> 1)
phonemacro 0:f51465145906 88 #define MAX31875_I2C_SLAVE_ADR_R6 (0x9C >> 1)
phonemacro 0:f51465145906 89 #define MAX31875_I2C_SLAVE_ADR_R7 (0x9E >> 1)
phonemacro 0:f51465145906 90
phonemacro 0:f51465145906 91 #define MAX31875_CF_NORMAL_FORMAT (0.00390625F)
phonemacro 0:f51465145906 92 #define MAX31875_CF_EXTENDED_FORMAT (0.0078125F)
phonemacro 0:f51465145906 93
phonemacro 0:f51465145906 94 /**
phonemacro 0:f51465145906 95 * @brief Extremely small low-power temperature sensor.
phonemacro 0:f51465145906 96 * @version 1.0000.0000
phonemacro 0:f51465145906 97 *
phonemacro 0:f51465145906 98 * @details The MAX31875 is a small WLP package temperature sensor.
phonemacro 0:f51465145906 99 * It supports high, low triggers stored in EEPROM for hystersis
phonemacro 0:f51465145906 100 * or limit alarms using comparator or interrupt mode.
phonemacro 0:f51465145906 101 * The MAX31875 can operate in shutdown and one-shot mode for
phonemacro 0:f51465145906 102 * extremely low power applications.
phonemacro 0:f51465145906 103 * Tiny size of 0.84 x 0.84 x 0.35 mm.
phonemacro 0:f51465145906 104 * Accuracy is +-1.5°C from +10°C to +45°C (±0.5°C Typical),
phonemacro 0:f51465145906 105 * +-2.0°C from -10°C to +100°C (±0.6°C Typical),
phonemacro 0:f51465145906 106 * +-3.0°C from -20°C to +125°C (±1.0°C Typical),
phonemacro 0:f51465145906 107 * I2C data rate of up to 1 MHz.
phonemacro 0:f51465145906 108 *
phonemacro 0:f51465145906 109 * @code
phonemacro 0:f51465145906 110 * #include "mbed.h"
phonemacro 0:f51465145906 111 * #include "max32630fthr.h"
phonemacro 0:f51465145906 112 * #include "max31875.h"
phonemacro 0:f51465145906 113 * #include "USBSerial.h"
phonemacro 0:f51465145906 114 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_1V8);
phonemacro 0:f51465145906 115 * I2C i2cBus(P3_4, P3_5);
phonemacro 0:f51465145906 116 * int main()
phonemacro 0:f51465145906 117 * {
phonemacro 0:f51465145906 118 * uint16_t raw;
phonemacro 0:f51465145906 119 * float temperature;
phonemacro 0:f51465145906 120 * DigitalOut rLED(LED1, LED_OFF);
phonemacro 0:f51465145906 121 * DigitalOut gLED(LED2, LED_OFF);
phonemacro 0:f51465145906 122 * DigitalOut bLED(LED3, LED_OFF);
phonemacro 0:f51465145906 123 * gLED = LED_ON;
phonemacro 0:f51465145906 124 * MAX31875 temp_sensor(i2cBus, MAX31875_I2C_SLAVE_ADR_R0);
phonemacro 0:f51465145906 125 * i2cBus.frequency(1000000);
phonemacro 0:f51465145906 126 * temp_sensor.write_reg(uint16_t(MAX31875_CFG_CONV_RATE_8 |
phonemacro 0:f51465145906 127 * MAX31875_CFG_RESOLUTION_12BIT), MAX31875_REG_CONFIGURATION);
phonemacro 0:f51465145906 128 * wait(MAX31875_WAIT_CONV_RATE_8);
phonemacro 0:f51465145906 129 * temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE);
phonemacro 0:f51465145906 130 * printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 0:f51465145906 131 * temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 0:f51465145906 132 * }
phonemacro 0:f51465145906 133 * @endcode
phonemacro 0:f51465145906 134 */
phonemacro 0:f51465145906 135
phonemacro 0:f51465145906 136 class MAX31875
phonemacro 0:f51465145906 137 {
phonemacro 0:f51465145906 138 public:
phonemacro 0:f51465145906 139
phonemacro 0:f51465145906 140 /**********************************************************//**
phonemacro 0:f51465145906 141 * @brief Constructor for MAX31875 Class.
phonemacro 0:f51465145906 142 *
phonemacro 0:f51465145906 143 * @details Allows user to use existing I2C object
phonemacro 0:f51465145906 144 *
phonemacro 0:f51465145906 145 * On Entry:
phonemacro 0:f51465145906 146 * @param[in] i2c_bus - pointer to existing I2C object
phonemacro 0:f51465145906 147 * @param[in] i2c_adrs - 7-bit slave address of MAX31875
phonemacro 0:f51465145906 148 *
phonemacro 0:f51465145906 149 * On Exit:
phonemacro 0:f51465145906 150 *
phonemacro 0:f51465145906 151 * @return None
phonemacro 0:f51465145906 152 **************************************************************/
phonemacro 0:f51465145906 153 MAX31875(I2C &i2c_bus, uint8_t slaveAddress);
phonemacro 0:f51465145906 154
phonemacro 0:f51465145906 155 /**********************************************************//**
phonemacro 0:f51465145906 156 * @brief Default destructor for MAX31875 Class.
phonemacro 0:f51465145906 157 *
phonemacro 0:f51465145906 158 * @details Destroys I2C object if owner
phonemacro 0:f51465145906 159 *
phonemacro 0:f51465145906 160 * On Entry:
phonemacro 0:f51465145906 161 *
phonemacro 0:f51465145906 162 * On Exit:
phonemacro 0:f51465145906 163 *
phonemacro 0:f51465145906 164 * @return None
phonemacro 0:f51465145906 165 **************************************************************/
phonemacro 0:f51465145906 166 ~MAX31875();
phonemacro 0:f51465145906 167
phonemacro 0:f51465145906 168 /**
phonemacro 0:f51465145906 169 * @brief Read register of device at slave address
phonemacro 0:f51465145906 170 * @param[out] value - Read data on success
phonemacro 0:f51465145906 171 * @param reg - Register address
phonemacro 0:f51465145906 172 * @return 0 on success, negative number on failure
phonemacro 0:f51465145906 173 */
phonemacro 0:f51465145906 174 int read_reg(uint16_t *value, char reg);
phonemacro 0:f51465145906 175
phonemacro 0:f51465145906 176 /**
phonemacro 0:f51465145906 177 * @brief Reads the temperature registers
phonemacro 0:f51465145906 178 * @param reg - the address of the temperature register
phonemacro 0:f51465145906 179 * @return temprature in degrees Celsius
phonemacro 0:f51465145906 180 */
phonemacro 0:f51465145906 181 float read_reg_as_temperature(uint8_t reg);
phonemacro 0:f51465145906 182
phonemacro 0:f51465145906 183 /**
phonemacro 1:98499c3dace6 184 * @brief Writes to the configuration register
phonemacro 1:98499c3dace6 185 * @param cfg - configurate word
phonemacro 0:f51465145906 186 * @return 0 on success, negative number on failure
phonemacro 0:f51465145906 187 */
phonemacro 0:f51465145906 188 int write_cfg(uint16_t cfg);
phonemacro 0:f51465145906 189
phonemacro 0:f51465145906 190 /**
phonemacro 1:98499c3dace6 191 * @brief Writes to the THYST register
phonemacro 1:98499c3dace6 192 * @param temperature - the temperature in Celsius degrees
phonemacro 0:f51465145906 193 * @return 0 on success, negative number on failure
phonemacro 0:f51465145906 194 */
phonemacro 0:f51465145906 195 int write_trip_low(float temperature);
phonemacro 0:f51465145906 196
phonemacro 0:f51465145906 197 /**
phonemacro 1:98499c3dace6 198 * @brief Writes to the TOS register
phonemacro 1:98499c3dace6 199 * @param temperature - the temperature in Celsius degrees
phonemacro 0:f51465145906 200 * @return 0 on success, negative number on failure
phonemacro 0:f51465145906 201 */
phonemacro 0:f51465145906 202 int write_trip_high(float temperature);
phonemacro 0:f51465145906 203
phonemacro 0:f51465145906 204 /**
phonemacro 1:98499c3dace6 205 * @brief Converts Celsius degrees to Fahrenheit
phonemacro 0:f51465145906 206 * @param temp_c - the temperature in Celsius degrees
phonemacro 1:98499c3dace6 207 * @return temperature in Celsius degrees
phonemacro 0:f51465145906 208 */
phonemacro 0:f51465145906 209 float celsius_to_fahrenheit(float temp_c);
phonemacro 0:f51465145906 210
phonemacro 0:f51465145906 211 protected:
phonemacro 2:048eff228fd8 212 /** @union max31875_raw_data
phonemacro 2:048eff228fd8 213 * @brief union data structure for byte word manipulations
phonemacro 2:048eff228fd8 214 */
phonemacro 0:f51465145906 215 union max31875_raw_data {
phonemacro 0:f51465145906 216 struct {
phonemacro 0:f51465145906 217 uint8_t lsb;
phonemacro 0:f51465145906 218 uint8_t msb;
phonemacro 0:f51465145906 219 };
phonemacro 0:f51465145906 220 uint16_t uwrd;
phonemacro 0:f51465145906 221 int16_t swrd;
phonemacro 0:f51465145906 222 };
phonemacro 0:f51465145906 223
phonemacro 0:f51465145906 224 /**
phonemacro 0:f51465145906 225 * @brief Write a value to a register
phonemacro 0:f51465145906 226 * @param value - value to write to the register
phonemacro 0:f51465145906 227 * @param reg - register address
phonemacro 0:f51465145906 228 * @return 0 on success, negative number on failure
phonemacro 0:f51465145906 229 */
phonemacro 0:f51465145906 230 int write_reg(uint16_t value, char reg);
phonemacro 0:f51465145906 231
phonemacro 0:f51465145906 232 private:
phonemacro 2:048eff228fd8 233 /** @var m_i2c
phonemacro 2:048eff228fd8 234 * @brief I2C object
phonemacro 2:048eff228fd8 235 */
phonemacro 0:f51465145906 236 I2C &m_i2c;
phonemacro 2:048eff228fd8 237 /** @var m_writeAddress, m_readAddress
phonemacro 2:048eff228fd8 238 * @brief I2C address
phonemacro 2:048eff228fd8 239 */
phonemacro 0:f51465145906 240 uint8_t m_writeAddress, m_readAddress;
phonemacro 2:048eff228fd8 241 /** @var m_writeAddress, m_readAddress
phonemacro 2:048eff228fd8 242 * @brief m_extended_format
phonemacro 2:048eff228fd8 243 */
phonemacro 0:f51465145906 244 uint32_t m_extended_format;
phonemacro 0:f51465145906 245 };
phonemacro 0:f51465145906 246
phonemacro 0:f51465145906 247 #endif/* MAX31875_H */