C, C++ source code driver software for the Maxim Integrated MAX31725, MAX31726 thermostat : The MAX31725, MAX31726 temperature sensors provides temperature measurements with an accuracy of up to +-0.5°C. Extended format allows for high temperature readings up to 150°C. The MAX31725 can operate in a low powered mode by utilizing the shutdown and one-shot mode. MAX31725  supports two-wire serial, I2C bus communications in an 8-pin TQDFN 3x3 mm package.

Dependents:   MAX31725_High_Temperature_Accurate_IC

Committer:
phonemacro
Date:
Wed Apr 17 21:28:29 2019 +0000
Revision:
24:b4fdbbe79036
Parent:
22:692515ac2bb7
update comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 20:c9805b01e814 1 /*******************************************************************************
phonemacro 20:c9805b01e814 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 20:c9805b01e814 3 *
phonemacro 20:c9805b01e814 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 20:c9805b01e814 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 20:c9805b01e814 6 * to deal in the Software without restriction, including without limitation
phonemacro 20:c9805b01e814 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 20:c9805b01e814 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 20:c9805b01e814 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 20:c9805b01e814 10 *
phonemacro 20:c9805b01e814 11 * The above copyright notice and this permission notice shall be included
phonemacro 20:c9805b01e814 12 * in all copies or substantial portions of the Software.
phonemacro 20:c9805b01e814 13 *
phonemacro 20:c9805b01e814 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 20:c9805b01e814 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 20:c9805b01e814 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 20:c9805b01e814 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 20:c9805b01e814 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 20:c9805b01e814 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 20:c9805b01e814 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 20:c9805b01e814 21 *
phonemacro 20:c9805b01e814 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 20:c9805b01e814 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 20:c9805b01e814 24 * Products, Inc. Branding Policy.
phonemacro 20:c9805b01e814 25 *
phonemacro 20:c9805b01e814 26 * The mere transfer of this software does not imply any licenses
phonemacro 20:c9805b01e814 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 20:c9805b01e814 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 20:c9805b01e814 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 20:c9805b01e814 30 * ownership rights.
phonemacro 20:c9805b01e814 31 *******************************************************************************
phonemacro 20:c9805b01e814 32 */
phonemacro 20:c9805b01e814 33 #include "max31725.h"
phonemacro 20:c9805b01e814 34 #include "max31725_cpp.h"
phonemacro 20:c9805b01e814 35 #include "mbed.h"
phonemacro 20:c9805b01e814 36 // #include "USBSerial.h"
phonemacro 20:c9805b01e814 37
phonemacro 20:c9805b01e814 38 /******************************************************************************
phonemacro 20:c9805b01e814 39 * C++ version for MAX31725 driver *
phonemacro 20:c9805b01e814 40 ******************************************************************************
phonemacro 20:c9805b01e814 41 */
phonemacro 20:c9805b01e814 42
phonemacro 20:c9805b01e814 43 /******************************************************************************/
phonemacro 20:c9805b01e814 44 MAX31725::MAX31725(I2C &i2c_bus, uint8_t slave_address):
phonemacro 20:c9805b01e814 45 m_i2c(i2c_bus),
phonemacro 20:c9805b01e814 46 m_write_address(slave_address <<1),
phonemacro 20:c9805b01e814 47 m_read_address ((slave_address << 1) | 1)
phonemacro 20:c9805b01e814 48 {
phonemacro 20:c9805b01e814 49 max31725_extended_format = 0;
phonemacro 20:c9805b01e814 50 }
phonemacro 20:c9805b01e814 51
phonemacro 20:c9805b01e814 52 /******************************************************************************/
phonemacro 20:c9805b01e814 53 MAX31725::~MAX31725(void)
phonemacro 20:c9805b01e814 54 {
phonemacro 20:c9805b01e814 55 /** empty block */
phonemacro 20:c9805b01e814 56 }
phonemacro 20:c9805b01e814 57
phonemacro 20:c9805b01e814 58 /******************************************************************************/
phonemacro 20:c9805b01e814 59 int MAX31725::read_cfg_reg(uint8_t *value)
phonemacro 20:c9805b01e814 60 {
phonemacro 20:c9805b01e814 61 int32_t ret;
phonemacro 20:c9805b01e814 62 char data[1] = {0};
phonemacro 20:c9805b01e814 63 char reg = MAX31725_REG_CONFIGURATION;
phonemacro 20:c9805b01e814 64
phonemacro 20:c9805b01e814 65 /* write to the Register Select, true is for repeated start */
phonemacro 20:c9805b01e814 66 ret = m_i2c.write(m_write_address, &reg, 1, true);
phonemacro 20:c9805b01e814 67 if (ret == 0) {
phonemacro 20:c9805b01e814 68 ret = m_i2c.read(m_read_address, data, 1, false);
phonemacro 20:c9805b01e814 69 if (ret == 0) {
phonemacro 20:c9805b01e814 70 *value = data[0];
phonemacro 20:c9805b01e814 71 return MAX31725_NO_ERROR;
phonemacro 20:c9805b01e814 72 } else {
phonemacro 20:c9805b01e814 73 printf(
phonemacro 20:c9805b01e814 74 "%s: failed to read data: ret: %d\r\n", __func__, ret);
phonemacro 20:c9805b01e814 75 }
phonemacro 20:c9805b01e814 76 } else {
phonemacro 20:c9805b01e814 77 printf("%s: failed to write to Register Select: ret: %d\r\n",
phonemacro 20:c9805b01e814 78 __func__, ret);
phonemacro 20:c9805b01e814 79 }
phonemacro 20:c9805b01e814 80 return MAX31725_ERROR;
phonemacro 20:c9805b01e814 81 }
phonemacro 20:c9805b01e814 82
phonemacro 20:c9805b01e814 83 /******************************************************************************/
phonemacro 20:c9805b01e814 84 int MAX31725::read_reg16(int16_t *value, char reg)
phonemacro 20:c9805b01e814 85 {
phonemacro 20:c9805b01e814 86 int32_t ret;
phonemacro 20:c9805b01e814 87 char data[2] = {0, 0};
phonemacro 20:c9805b01e814 88 max31725_raw_data tmp;
phonemacro 20:c9805b01e814 89
phonemacro 20:c9805b01e814 90 if (reg == MAX31725_REG_TEMPERATURE ||
phonemacro 20:c9805b01e814 91 reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) {
phonemacro 20:c9805b01e814 92 /* write to the Register Select, true is for repeated start */
phonemacro 20:c9805b01e814 93 ret = m_i2c.write(m_write_address, &reg, 1, true);
phonemacro 20:c9805b01e814 94 /* read the two bytes of data */
phonemacro 20:c9805b01e814 95 if (ret == 0) {
phonemacro 20:c9805b01e814 96 ret = m_i2c.read(m_read_address, data, 2, false);
phonemacro 20:c9805b01e814 97 if (ret == 0) {
phonemacro 20:c9805b01e814 98 tmp.msb = data[0];
phonemacro 20:c9805b01e814 99 tmp.lsb = data[1];
phonemacro 20:c9805b01e814 100 *value = tmp.swrd;
phonemacro 20:c9805b01e814 101 return MAX31725_NO_ERROR;
phonemacro 20:c9805b01e814 102 } else {
phonemacro 20:c9805b01e814 103 printf(
phonemacro 20:c9805b01e814 104 "%s: failed to read data: ret: %d\r\n", __func__, ret);
phonemacro 20:c9805b01e814 105 }
phonemacro 20:c9805b01e814 106 } else {
phonemacro 20:c9805b01e814 107 printf("%s: failed to write to Register Select: ret: %d\r\n",
phonemacro 20:c9805b01e814 108 __func__, ret);
phonemacro 20:c9805b01e814 109 }
phonemacro 20:c9805b01e814 110 } else {
phonemacro 20:c9805b01e814 111 printf("%s: register address is not correct: register: %d\r\n",
phonemacro 20:c9805b01e814 112 __func__, reg);
phonemacro 20:c9805b01e814 113 }
phonemacro 20:c9805b01e814 114 return MAX31725_ERROR;
phonemacro 20:c9805b01e814 115 }
phonemacro 20:c9805b01e814 116
phonemacro 20:c9805b01e814 117 /******************************************************************************/
phonemacro 20:c9805b01e814 118 float MAX31725::read_reg_as_temperature(uint8_t reg)
phonemacro 20:c9805b01e814 119 {
phonemacro 20:c9805b01e814 120 max31725_raw_data tmp;
phonemacro 20:c9805b01e814 121 float temperature;
phonemacro 20:c9805b01e814 122 if (reg == MAX31725_REG_TEMPERATURE ||
phonemacro 20:c9805b01e814 123 reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) {
phonemacro 20:c9805b01e814 124 read_reg16(&tmp.swrd, reg);
phonemacro 20:c9805b01e814 125 temperature = (float)tmp.swrd; /* values are 2's complement */
phonemacro 20:c9805b01e814 126 temperature *= MAX31725_CF_LSB;
phonemacro 20:c9805b01e814 127 if (reg == MAX31725_REG_TEMPERATURE && max31725_extended_format)
phonemacro 20:c9805b01e814 128 temperature += MAX31725_EXTENDED_FORMAT_OFFSET;
phonemacro 20:c9805b01e814 129
phonemacro 20:c9805b01e814 130 return temperature;
phonemacro 20:c9805b01e814 131 } else {
phonemacro 20:c9805b01e814 132 printf("%s: register is invalid, %d r\n", __func__, reg);
phonemacro 20:c9805b01e814 133 return 0;
phonemacro 20:c9805b01e814 134 }
phonemacro 20:c9805b01e814 135 }
phonemacro 20:c9805b01e814 136
phonemacro 20:c9805b01e814 137 /******************************************************************************/
phonemacro 20:c9805b01e814 138 int MAX31725::write_reg16(int16_t value, char reg)
phonemacro 20:c9805b01e814 139 {
phonemacro 20:c9805b01e814 140 int32_t ret;
phonemacro 20:c9805b01e814 141 char cmd[3];
phonemacro 20:c9805b01e814 142 max31725_raw_data tmp;
phonemacro 20:c9805b01e814 143
phonemacro 20:c9805b01e814 144 if (reg >= MAX31725_REG_THYST_LOW_TRIP && reg <= MAX31725_REG_MAX) {
phonemacro 20:c9805b01e814 145 cmd[0] = reg;
phonemacro 20:c9805b01e814 146 tmp.swrd = value;
phonemacro 20:c9805b01e814 147 cmd[1] = tmp.msb;
phonemacro 20:c9805b01e814 148 cmd[2] = tmp.lsb;
phonemacro 20:c9805b01e814 149 ret = m_i2c.write(m_write_address, cmd, 3, false);
phonemacro 20:c9805b01e814 150 if (ret == 0) {
phonemacro 20:c9805b01e814 151 return MAX31725_NO_ERROR;
phonemacro 20:c9805b01e814 152 } else {
phonemacro 20:c9805b01e814 153 printf("%s: I2C write error %d\r\n",__func__, ret);
phonemacro 20:c9805b01e814 154 return MAX31725_ERROR;
phonemacro 20:c9805b01e814 155 }
phonemacro 20:c9805b01e814 156 } else {
phonemacro 20:c9805b01e814 157 printf("%s: register value invalid %x\r\n",__func__, reg);
phonemacro 20:c9805b01e814 158 return MAX31725_ERROR;
phonemacro 20:c9805b01e814 159 }
phonemacro 20:c9805b01e814 160 }
phonemacro 20:c9805b01e814 161
phonemacro 20:c9805b01e814 162
phonemacro 20:c9805b01e814 163 /******************************************************************************/
phonemacro 20:c9805b01e814 164 int MAX31725::write_cfg_reg(uint8_t cfg)
phonemacro 20:c9805b01e814 165 {
phonemacro 20:c9805b01e814 166 int32_t ret;
phonemacro 20:c9805b01e814 167 char cmd[2];
phonemacro 20:c9805b01e814 168
phonemacro 20:c9805b01e814 169 cmd[0] = MAX31725_REG_CONFIGURATION;
phonemacro 20:c9805b01e814 170 cmd[1] = cfg;
phonemacro 20:c9805b01e814 171 ret = m_i2c.write(m_write_address, cmd, 2, false);
phonemacro 20:c9805b01e814 172 if (ret == 0) {
phonemacro 20:c9805b01e814 173 max31725_extended_format = 0;
phonemacro 20:c9805b01e814 174 if (cfg & MAX31725_CFG_EXTENDED_FORMAT)
phonemacro 20:c9805b01e814 175 max31725_extended_format = 1;
phonemacro 20:c9805b01e814 176
phonemacro 20:c9805b01e814 177 return MAX31725_NO_ERROR;
phonemacro 20:c9805b01e814 178 } else {
phonemacro 20:c9805b01e814 179 printf("%s: I2C write error %d\r\n",__func__, ret);
phonemacro 20:c9805b01e814 180 return MAX31725_ERROR;
phonemacro 20:c9805b01e814 181 }
phonemacro 20:c9805b01e814 182 }
phonemacro 20:c9805b01e814 183
phonemacro 20:c9805b01e814 184 /******************************************************************************/
phonemacro 20:c9805b01e814 185 int MAX31725::write_trip_low_thyst(float temperature)
phonemacro 20:c9805b01e814 186 {
phonemacro 20:c9805b01e814 187 max31725_raw_data raw;
phonemacro 20:c9805b01e814 188 temperature /= MAX31725_CF_LSB;
phonemacro 20:c9805b01e814 189 raw.swrd = int16_t(temperature);
phonemacro 20:c9805b01e814 190 return write_reg16(raw.swrd, MAX31725_REG_THYST_LOW_TRIP);
phonemacro 20:c9805b01e814 191 }
phonemacro 20:c9805b01e814 192
phonemacro 20:c9805b01e814 193 /******************************************************************************/
phonemacro 20:c9805b01e814 194 int MAX31725::write_trip_high_tos(float temperature)
phonemacro 20:c9805b01e814 195 {
phonemacro 20:c9805b01e814 196 max31725_raw_data raw;
phonemacro 20:c9805b01e814 197 temperature /= MAX31725_CF_LSB;
phonemacro 20:c9805b01e814 198 raw.swrd = int16_t(temperature);
phonemacro 20:c9805b01e814 199 return write_reg16(raw.uwrd, MAX31725_REG_TOS_HIGH_TRIP);
phonemacro 20:c9805b01e814 200 }
phonemacro 20:c9805b01e814 201
phonemacro 20:c9805b01e814 202 /******************************************************************************/
phonemacro 20:c9805b01e814 203 float MAX31725::celsius_to_fahrenheit(float temp_c)
phonemacro 20:c9805b01e814 204 {
phonemacro 20:c9805b01e814 205 float temp_f;
phonemacro 20:c9805b01e814 206 temp_f = ((temp_c * 9)/5) + 32;
phonemacro 20:c9805b01e814 207 return temp_f;
phonemacro 20:c9805b01e814 208 }