C code and C++ library, driver software 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_WLP Click-Sensor-MAX31875 NuMaker-mbed-Sensor-MAX31875

Committer:
phonemacro
Date:
Wed Feb 13 22:17:06 2019 +0000
Revision:
6:61f3693edd6c
Parent:
5:228e67722322
correct initialization in write_trip_low, high.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 3:3528e660168c 1 /*******************************************************************************
phonemacro 3:3528e660168c 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 3:3528e660168c 3 *
phonemacro 3:3528e660168c 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 3:3528e660168c 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 3:3528e660168c 6 * to deal in the Software without restriction, including without limitation
phonemacro 3:3528e660168c 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 3:3528e660168c 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 3:3528e660168c 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 3:3528e660168c 10 *
phonemacro 3:3528e660168c 11 * The above copyright notice and this permission notice shall be included
phonemacro 3:3528e660168c 12 * in all copies or substantial portions of the Software.
phonemacro 3:3528e660168c 13 *
phonemacro 3:3528e660168c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 3:3528e660168c 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 3:3528e660168c 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 3:3528e660168c 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 3:3528e660168c 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 3:3528e660168c 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 3:3528e660168c 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 3:3528e660168c 21 *
phonemacro 3:3528e660168c 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 3:3528e660168c 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 3:3528e660168c 24 * Products, Inc. Branding Policy.
phonemacro 3:3528e660168c 25 *
phonemacro 3:3528e660168c 26 * The mere transfer of this software does not imply any licenses
phonemacro 3:3528e660168c 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 3:3528e660168c 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 3:3528e660168c 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 3:3528e660168c 30 * ownership rights.
phonemacro 3:3528e660168c 31 *******************************************************************************
phonemacro 3:3528e660168c 32 * @file max31875_cpp.h
phonemacro 3:3528e660168c 33 *******************************************************************************
phonemacro 3:3528e660168c 34 */
phonemacro 3:3528e660168c 35 #ifndef MAX31875_CPP_H
phonemacro 3:3528e660168c 36 #define MAX31875_CPP_H
phonemacro 3:3528e660168c 37 #include "mbed.h"
phonemacro 3:3528e660168c 38
phonemacro 3:3528e660168c 39
phonemacro 3:3528e660168c 40 /**
phonemacro 3:3528e660168c 41 * @brief Extremely small low-power temperature sensor.
phonemacro 6:61f3693edd6c 42 * @version 1.0000.0003
phonemacro 3:3528e660168c 43 *
phonemacro 3:3528e660168c 44 * @details The MAX31875 is a small WLP package temperature sensor.
phonemacro 3:3528e660168c 45 * It supports high, low triggers stored in EEPROM for hystersis
phonemacro 3:3528e660168c 46 * or limit alarms using comparator or interrupt mode.
phonemacro 3:3528e660168c 47 * The MAX31875 can operate in shutdown and one-shot mode for
phonemacro 3:3528e660168c 48 * extremely low power applications.
phonemacro 3:3528e660168c 49 * Tiny size of 0.84 x 0.84 x 0.35 mm.
phonemacro 3:3528e660168c 50 * Accuracy is +-1.5°C from +10°C to +45°C (±0.5°C Typical),
phonemacro 3:3528e660168c 51 * +-2.0°C from -10°C to +100°C (±0.6°C Typical),
phonemacro 3:3528e660168c 52 * +-3.0°C from -20°C to +125°C (±1.0°C Typical),
phonemacro 3:3528e660168c 53 * I2C data rate of up to 1 MHz.
phonemacro 3:3528e660168c 54 *
phonemacro 3:3528e660168c 55 * @code
phonemacro 3:3528e660168c 56 * #include "mbed.h"
phonemacro 3:3528e660168c 57 * #include "max32630fthr.h"
phonemacro 3:3528e660168c 58 * #include "max31875.h"
phonemacro 3:3528e660168c 59 * #include "USBSerial.h"
phonemacro 3:3528e660168c 60 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_1V8);
phonemacro 3:3528e660168c 61 * I2C i2cBus(P3_4, P3_5);
phonemacro 3:3528e660168c 62 * int main()
phonemacro 3:3528e660168c 63 * {
phonemacro 3:3528e660168c 64 * uint16_t raw;
phonemacro 3:3528e660168c 65 * float temperature;
phonemacro 3:3528e660168c 66 * DigitalOut rLED(LED1, LED_OFF);
phonemacro 3:3528e660168c 67 * DigitalOut gLED(LED2, LED_OFF);
phonemacro 3:3528e660168c 68 * DigitalOut bLED(LED3, LED_OFF);
phonemacro 3:3528e660168c 69 * gLED = LED_ON;
phonemacro 3:3528e660168c 70 * MAX31875 temp_sensor(i2cBus, MAX31875_I2C_SLAVE_ADR_R0);
phonemacro 3:3528e660168c 71 * i2cBus.frequency(1000000);
phonemacro 3:3528e660168c 72 * temp_sensor.write_reg(uint16_t(MAX31875_CFG_CONV_RATE_8 |
phonemacro 3:3528e660168c 73 * MAX31875_CFG_RESOLUTION_12BIT), MAX31875_REG_CONFIGURATION);
phonemacro 3:3528e660168c 74 * wait(MAX31875_WAIT_CONV_RATE_8);
phonemacro 3:3528e660168c 75 * temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE);
phonemacro 3:3528e660168c 76 * printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n",
phonemacro 3:3528e660168c 77 * temperature, temp_sensor.celsius_to_fahrenheit(temperature));
phonemacro 3:3528e660168c 78 * }
phonemacro 3:3528e660168c 79 * @endcode
phonemacro 3:3528e660168c 80 */
phonemacro 3:3528e660168c 81
phonemacro 3:3528e660168c 82 class MAX31875
phonemacro 3:3528e660168c 83 {
phonemacro 3:3528e660168c 84 public:
phonemacro 3:3528e660168c 85
phonemacro 3:3528e660168c 86 /**********************************************************//**
phonemacro 3:3528e660168c 87 * @brief Constructor for MAX31875 Class.
phonemacro 3:3528e660168c 88 *
phonemacro 3:3528e660168c 89 * @details Allows user to use existing I2C object
phonemacro 3:3528e660168c 90 *
phonemacro 3:3528e660168c 91 * On Entry:
phonemacro 3:3528e660168c 92 * @param[in] i2c_bus - pointer to existing I2C object
phonemacro 3:3528e660168c 93 * @param[in] i2c_adrs - 7-bit slave address of MAX31875
phonemacro 3:3528e660168c 94 *
phonemacro 3:3528e660168c 95 * On Exit:
phonemacro 3:3528e660168c 96 *
phonemacro 3:3528e660168c 97 * @return None
phonemacro 3:3528e660168c 98 **************************************************************/
phonemacro 3:3528e660168c 99 MAX31875(I2C &i2c_bus, uint8_t slave_address);
phonemacro 3:3528e660168c 100
phonemacro 3:3528e660168c 101 /**********************************************************//**
phonemacro 3:3528e660168c 102 * @brief Default destructor for MAX31875 Class.
phonemacro 3:3528e660168c 103 *
phonemacro 3:3528e660168c 104 * @details Destroys I2C object if owner
phonemacro 3:3528e660168c 105 *
phonemacro 3:3528e660168c 106 * On Entry:
phonemacro 3:3528e660168c 107 *
phonemacro 3:3528e660168c 108 * On Exit:
phonemacro 3:3528e660168c 109 *
phonemacro 3:3528e660168c 110 * @return None
phonemacro 3:3528e660168c 111 **************************************************************/
phonemacro 3:3528e660168c 112 ~MAX31875();
phonemacro 3:3528e660168c 113
phonemacro 3:3528e660168c 114 /**
phonemacro 3:3528e660168c 115 * @brief Read register of device at slave address
phonemacro 3:3528e660168c 116 * @param[out] value - Read data on success
phonemacro 3:3528e660168c 117 * @param reg - Register address
phonemacro 3:3528e660168c 118 * @return 0 on success, negative number on failure
phonemacro 3:3528e660168c 119 */
phonemacro 3:3528e660168c 120 int read_reg(uint16_t *value, char reg);
phonemacro 3:3528e660168c 121
phonemacro 3:3528e660168c 122 /**
phonemacro 3:3528e660168c 123 * @brief Reads the temperature registers
phonemacro 3:3528e660168c 124 * @param reg - the address of the temperature register
phonemacro 5:228e67722322 125 * @return temperature in degrees Celsius
phonemacro 3:3528e660168c 126 */
phonemacro 3:3528e660168c 127 float read_reg_as_temperature(uint8_t reg);
phonemacro 3:3528e660168c 128
phonemacro 3:3528e660168c 129 /**
phonemacro 3:3528e660168c 130 * @brief Writes to the configuration register
phonemacro 3:3528e660168c 131 * @param cfg - configurate word
phonemacro 3:3528e660168c 132 * @return 0 on success, negative number on failure
phonemacro 3:3528e660168c 133 */
phonemacro 3:3528e660168c 134 int write_cfg(uint16_t cfg);
phonemacro 3:3528e660168c 135
phonemacro 3:3528e660168c 136 /**
phonemacro 3:3528e660168c 137 * @brief Writes to the THYST register
phonemacro 3:3528e660168c 138 * @param temperature - the temperature in Celsius degrees
phonemacro 3:3528e660168c 139 * @return 0 on success, negative number on failure
phonemacro 3:3528e660168c 140 */
phonemacro 3:3528e660168c 141 int write_trip_low(float temperature);
phonemacro 3:3528e660168c 142
phonemacro 3:3528e660168c 143 /**
phonemacro 3:3528e660168c 144 * @brief Writes to the TOS register
phonemacro 3:3528e660168c 145 * @param temperature - the temperature in Celsius degrees
phonemacro 3:3528e660168c 146 * @return 0 on success, negative number on failure
phonemacro 3:3528e660168c 147 */
phonemacro 3:3528e660168c 148 int write_trip_high(float temperature);
phonemacro 3:3528e660168c 149
phonemacro 3:3528e660168c 150 /**
phonemacro 3:3528e660168c 151 * @brief Converts Celsius degrees to Fahrenheit
phonemacro 3:3528e660168c 152 * @param temp_c - the temperature in Celsius degrees
phonemacro 3:3528e660168c 153 * @return temperature in Celsius degrees
phonemacro 3:3528e660168c 154 */
phonemacro 3:3528e660168c 155 float celsius_to_fahrenheit(float temp_c);
phonemacro 3:3528e660168c 156
phonemacro 3:3528e660168c 157 protected:
phonemacro 3:3528e660168c 158 /**
phonemacro 3:3528e660168c 159 * @brief Write a value to a register
phonemacro 3:3528e660168c 160 * @param value - value to write to the register
phonemacro 3:3528e660168c 161 * @param reg - register address
phonemacro 3:3528e660168c 162 * @return 0 on success, negative number on failure
phonemacro 3:3528e660168c 163 */
phonemacro 3:3528e660168c 164 int write_reg(uint16_t value, char reg);
phonemacro 3:3528e660168c 165
phonemacro 3:3528e660168c 166 private:
phonemacro 3:3528e660168c 167 /** @var m_i2c
phonemacro 3:3528e660168c 168 * @brief I2C object
phonemacro 3:3528e660168c 169 */
phonemacro 3:3528e660168c 170 I2C &m_i2c;
phonemacro 3:3528e660168c 171 /** @var m_write_address, m_read_address
phonemacro 3:3528e660168c 172 * @brief I2C address
phonemacro 3:3528e660168c 173 */
phonemacro 3:3528e660168c 174 uint8_t m_write_address, m_read_address;
phonemacro 3:3528e660168c 175 /** @var m_write_address, m_read_address
phonemacro 3:3528e660168c 176 * @brief m_extended_format
phonemacro 3:3528e660168c 177 */
phonemacro 3:3528e660168c 178 uint32_t m_extended_format;
phonemacro 3:3528e660168c 179 };
phonemacro 3:3528e660168c 180
phonemacro 3:3528e660168c 181 #endif/* MAX31875_CPP_H */