C++ driver software code for Maxim Integrated MAX31723/MAX31722 device temperature sensor. The MAX31723 provides 9 to 12 bits of resolution.

Dependents:   MAX31723_Thermostat_Thermometer_Sensor

Committer:
phonemacro
Date:
Mon Jan 28 00:39:50 2019 +0000
Revision:
3:f39791139435
Parent:
2:7a20e65da621
Child:
4:ac75d4d62471
Use 1.8V for VDD. Updated the interface for reading registers.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:4a3d6ac5042d 1 /*******************************************************************************
phonemacro 0:4a3d6ac5042d 2 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:4a3d6ac5042d 3 *
phonemacro 0:4a3d6ac5042d 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:4a3d6ac5042d 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:4a3d6ac5042d 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:4a3d6ac5042d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:4a3d6ac5042d 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:4a3d6ac5042d 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:4a3d6ac5042d 10 *
phonemacro 0:4a3d6ac5042d 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:4a3d6ac5042d 12 * in all copies or substantial portions of the Software.
phonemacro 0:4a3d6ac5042d 13 *
phonemacro 0:4a3d6ac5042d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:4a3d6ac5042d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:4a3d6ac5042d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:4a3d6ac5042d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:4a3d6ac5042d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:4a3d6ac5042d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:4a3d6ac5042d 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:4a3d6ac5042d 21 *
phonemacro 0:4a3d6ac5042d 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:4a3d6ac5042d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:4a3d6ac5042d 24 * Products, Inc. Branding Policy.
phonemacro 0:4a3d6ac5042d 25 *
phonemacro 0:4a3d6ac5042d 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:4a3d6ac5042d 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:4a3d6ac5042d 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:4a3d6ac5042d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:4a3d6ac5042d 30 * ownership rights.
phonemacro 0:4a3d6ac5042d 31 *******************************************************************************
phonemacro 0:4a3d6ac5042d 32 * @file MAX31723.h
phonemacro 0:4a3d6ac5042d 33 *******************************************************************************
phonemacro 0:4a3d6ac5042d 34 */
phonemacro 0:4a3d6ac5042d 35 #ifndef MAX31723_H
phonemacro 0:4a3d6ac5042d 36 #define MAX31723_H
phonemacro 0:4a3d6ac5042d 37
phonemacro 0:4a3d6ac5042d 38 #include "mbed.h"
phonemacro 0:4a3d6ac5042d 39
phonemacro 0:4a3d6ac5042d 40 #define MAX31723_NO_ERROR 0
phonemacro 0:4a3d6ac5042d 41 #define MAX31723_ERROR -1
phonemacro 0:4a3d6ac5042d 42
phonemacro 0:4a3d6ac5042d 43 #define MAX31723_WRITE_MASK 0x80
phonemacro 0:4a3d6ac5042d 44
phonemacro 0:4a3d6ac5042d 45 #define MAX31723_REG_CFG 0x00
phonemacro 0:4a3d6ac5042d 46 #define MAX31723_REG_TEMP_LSB 0x01
phonemacro 0:4a3d6ac5042d 47 #define MAX31723_REG_TEMP_MSB 0x02
phonemacro 3:f39791139435 48 #define MAX31723_REG_TRIP_HI_LSB 0x03
phonemacro 3:f39791139435 49 #define MAX31723_REG_TRIP_HI_MSB 0x04
phonemacro 3:f39791139435 50 #define MAX31723_REG_TRIP_LO_LSB 0x05
phonemacro 3:f39791139435 51 #define MAX31723_REG_TRIP_LO_MSB 0x06
phonemacro 3:f39791139435 52 #define MAX31723_REG_MAX MAX31723_REG_TRIP_LO_MSB
phonemacro 0:4a3d6ac5042d 53
phonemacro 0:4a3d6ac5042d 54 /* Configuration, Status masks */
phonemacro 0:4a3d6ac5042d 55 #define MAX31723_CFG_CONTINUOUS 0x00
phonemacro 0:4a3d6ac5042d 56 #define MAX31723_CFG_STANDBY 0x01
phonemacro 0:4a3d6ac5042d 57
phonemacro 0:4a3d6ac5042d 58 #define MAX31723_CFG_RESOLUTION_9BIT 0x00
phonemacro 0:4a3d6ac5042d 59 #define MAX31723_CFG_RESOLUTION_10BIT 0x02
phonemacro 0:4a3d6ac5042d 60 #define MAX31723_CFG_RESOLUTION_11BIT 0x04
phonemacro 0:4a3d6ac5042d 61 #define MAX31723_CFG_RESOLUTION_12BIT 0x06
phonemacro 0:4a3d6ac5042d 62
phonemacro 0:4a3d6ac5042d 63 #define MAX31723_CFG_TM_MODE_INTERRUPT 0x08
phonemacro 0:4a3d6ac5042d 64 #define MAX31723_CFG_1SHOT 0x10
phonemacro 0:4a3d6ac5042d 65 #define MAX31723_CFG_NVB_NVRAM_BUSY 0x20
phonemacro 0:4a3d6ac5042d 66 #define MAX31723_CFG_MEMW_CFG_WR_EEPROM 0x40
phonemacro 0:4a3d6ac5042d 67
phonemacro 3:f39791139435 68 #define MAX31723_CONV_TIME_MSEC_9BIT 0.025
phonemacro 3:f39791139435 69 #define MAX31723_CONV_TIME_MSEC_10BIT 0.050
phonemacro 3:f39791139435 70 #define MAX31723_CONV_TIME_MSEC_11BIT 0.100
phonemacro 3:f39791139435 71 #define MAX31723_CONV_TIME_MSEC_12BIT 0.200
phonemacro 3:f39791139435 72
phonemacro 3:f39791139435 73 #define MAX31723_NVRAM_WRITE_TIME_MSEC 0.015
phonemacro 3:f39791139435 74
phonemacro 3:f39791139435 75 #define MAX31723_CF_LSB 0.0625f
phonemacro 3:f39791139435 76 #define MAX31723_UNUSED_BITS 4
phonemacro 0:4a3d6ac5042d 77
phonemacro 0:4a3d6ac5042d 78 /**
phonemacro 0:4a3d6ac5042d 79 * @brief 9-bit to 12bit device temperature sensor with digital-to-analog converters (DACs)
phonemacro 0:4a3d6ac5042d 80 * for the MAX31723, MAX5214.
phonemacro 3:f39791139435 81 * @version 1.0000.0002
phonemacro 0:4a3d6ac5042d 82 *
phonemacro 0:4a3d6ac5042d 83 * @details The MAX31722/MAX31723 provides device temperature readings
phonemacro 0:4a3d6ac5042d 84 * for thermostats and thermometers. Communications may be either SPI or
phonemacro 0:4a3d6ac5042d 85 * 3-wire. There are two operating modes available: comparator or interrupt.
phonemacro 0:4a3d6ac5042d 86 * Temperature limits may be stored in NVRAM so that the TOUT_N pin can be
phonemacro 0:4a3d6ac5042d 87 * triggered when the limits are exceeded while in the comparator mode.
phonemacro 0:4a3d6ac5042d 88 * Typically applications are for temperature sensitive systems.
phonemacro 0:4a3d6ac5042d 89 *
phonemacro 0:4a3d6ac5042d 90 */
phonemacro 0:4a3d6ac5042d 91
phonemacro 0:4a3d6ac5042d 92 class MAX31723{
phonemacro 0:4a3d6ac5042d 93 public:
phonemacro 0:4a3d6ac5042d 94
phonemacro 0:4a3d6ac5042d 95 /**************************************************************
phonemacro 0:4a3d6ac5042d 96 * @brief Constructor for MAX31723 Class.
phonemacro 0:4a3d6ac5042d 97 *
phonemacro 0:4a3d6ac5042d 98 * @details Requires an existing SPI object as well as a DigitalOut object.
phonemacro 0:4a3d6ac5042d 99 * The DigitalOut object is used for a chip enable signal
phonemacro 0:4a3d6ac5042d 100 *
phonemacro 0:4a3d6ac5042d 101 * On Entry:
phonemacro 0:4a3d6ac5042d 102 * @param[in] spi - pointer to existing SPI object
phonemacro 0:4a3d6ac5042d 103 * @param[in] ce_pin - pointer to a DigitalOut pin object
phonemacro 0:4a3d6ac5042d 104 * @param[in] ic_variant - which type of MAX521x is used
phonemacro 0:4a3d6ac5042d 105 *
phonemacro 0:4a3d6ac5042d 106 * On Exit:
phonemacro 0:4a3d6ac5042d 107 *
phonemacro 0:4a3d6ac5042d 108 * @return None
phonemacro 0:4a3d6ac5042d 109 ***************************************************************
phonemacro 0:4a3d6ac5042d 110 */
phonemacro 0:4a3d6ac5042d 111 MAX31723(SPI &spi, DigitalOut &ce_pin);
phonemacro 0:4a3d6ac5042d 112
phonemacro 0:4a3d6ac5042d 113 /**
phonemacro 0:4a3d6ac5042d 114 * @brief Write a value to a register
phonemacro 0:4a3d6ac5042d 115 * @param val - 8-bit value to write to the register
phonemacro 0:4a3d6ac5042d 116 * @param reg - register address
phonemacro 0:4a3d6ac5042d 117 * @return 0 on success, negative number on failure
phonemacro 0:4a3d6ac5042d 118 */
phonemacro 0:4a3d6ac5042d 119 int write_reg(uint8_t val, uint8_t reg);
phonemacro 0:4a3d6ac5042d 120
phonemacro 0:4a3d6ac5042d 121 /**
phonemacro 3:f39791139435 122 * @brief Reads the temperature registers
phonemacro 3:f39791139435 123 * @param reg - the low byte address of the temperature register
phonemacro 0:4a3d6ac5042d 124 * @return temprature in degrees celsius
phonemacro 0:4a3d6ac5042d 125 */
phonemacro 3:f39791139435 126 float read_reg_as_temperature(uint8_t reg);
phonemacro 0:4a3d6ac5042d 127
phonemacro 0:4a3d6ac5042d 128 /**
phonemacro 0:4a3d6ac5042d 129 * @brief Configures the device to perform a one-shot temperature reading
phonemacro 2:7a20e65da621 130 * and places the device in standby mode, interrupt mode.
phonemacro 0:4a3d6ac5042d 131 * @param resolution - the resolution of the calculation is set to this
phonemacro 0:4a3d6ac5042d 132 * @return 0 on success, negative number on failure
phonemacro 0:4a3d6ac5042d 133 */
phonemacro 2:7a20e65da621 134 int perform_one_shot_int(uint8_t resolution);
phonemacro 0:4a3d6ac5042d 135
phonemacro 0:4a3d6ac5042d 136 /**
phonemacro 3:f39791139435 137 * @brief Configures the device to perform a one-shot temperature reading
phonemacro 3:f39791139435 138 * and places the device in standby mode, comparator mode.
phonemacro 3:f39791139435 139 * @param resolution - the resolution of the calculation is set to this
phonemacro 3:f39791139435 140 * @return 0 on success, negative number on failure
phonemacro 3:f39791139435 141 */
phonemacro 3:f39791139435 142 int perform_one_shot_comparator(uint8_t resolution);
phonemacro 3:f39791139435 143
phonemacro 3:f39791139435 144 /**
phonemacro 0:4a3d6ac5042d 145 * @brief Converts Celcius degrees to Fahrenheit
phonemacro 0:4a3d6ac5042d 146 * @param temp_c - the temperature in Celsius degrees
phonemacro 0:4a3d6ac5042d 147 * @return 0 on success, negative number on failure
phonemacro 0:4a3d6ac5042d 148 */
phonemacro 0:4a3d6ac5042d 149 float celsius_to_fahrenheit(float temp_c);
phonemacro 0:4a3d6ac5042d 150
phonemacro 3:f39791139435 151 /**
phonemacro 3:f39791139435 152 * @brief Reads the configuration register
phonemacro 3:f39791139435 153 * @return value of the configuration register
phonemacro 3:f39791139435 154 */
phonemacro 3:f39791139435 155 uint8_t read_cfg();
phonemacro 3:f39791139435 156
phonemacro 0:4a3d6ac5042d 157
phonemacro 0:4a3d6ac5042d 158 /************************************************************
phonemacro 0:4a3d6ac5042d 159 * @brief Default destructor for MAX31723 Class.
phonemacro 0:4a3d6ac5042d 160 *
phonemacro 0:4a3d6ac5042d 161 * @details Destroys SPI object if owner
phonemacro 0:4a3d6ac5042d 162 *
phonemacro 0:4a3d6ac5042d 163 * On Entry:
phonemacro 0:4a3d6ac5042d 164 *
phonemacro 0:4a3d6ac5042d 165 * On Exit:
phonemacro 0:4a3d6ac5042d 166 *
phonemacro 0:4a3d6ac5042d 167 * @return None
phonemacro 0:4a3d6ac5042d 168 *************************************************************
phonemacro 0:4a3d6ac5042d 169 */
phonemacro 0:4a3d6ac5042d 170 ~MAX31723();
phonemacro 3:f39791139435 171 protected:
phonemacro 3:f39791139435 172 /**
phonemacro 3:f39791139435 173 * @brief Configures the device to perform a one-shot temperature reading
phonemacro 3:f39791139435 174 * and places the device in standby mode, interrupt mode.
phonemacro 3:f39791139435 175 * @param resolution - the resolution of the calculation is set to this
phonemacro 3:f39791139435 176 * @return 0 on success, negative number on failure
phonemacro 3:f39791139435 177 */
phonemacro 3:f39791139435 178 int perform_one_shot(uint8_t resolution, uint8_t interrupt_mode);
phonemacro 3:f39791139435 179
phonemacro 3:f39791139435 180
phonemacro 0:4a3d6ac5042d 181 private:
phonemacro 3:f39791139435 182 /**
phonemacro 3:f39791139435 183 * @brief Read a value from a register
phonemacro 3:f39791139435 184 * @param val - 8-bit value read from the register
phonemacro 3:f39791139435 185 * @param reg - register address
phonemacro 3:f39791139435 186 * @return 0 on success, negative number on failure
phonemacro 3:f39791139435 187 */
phonemacro 3:f39791139435 188 int read_reg(uint8_t &val, uint8_t reg);
phonemacro 3:f39791139435 189
phonemacro 3:f39791139435 190 /* SPI object */
phonemacro 0:4a3d6ac5042d 191 SPI &m_spi;
phonemacro 3:f39791139435 192 /* chip enable pin */
phonemacro 0:4a3d6ac5042d 193 DigitalOut &m_chip_enable;
phonemacro 0:4a3d6ac5042d 194 };
phonemacro 0:4a3d6ac5042d 195
phonemacro 0:4a3d6ac5042d 196 #endif