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:
0:f51465145906
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 */
phonemacro 0:f51465145906 33 #include "max31875.h"
phonemacro 0:f51465145906 34 #include "mbed.h"
phonemacro 0:f51465145906 35 // #include "USBSerial.h"
phonemacro 0:f51465145906 36
phonemacro 0:f51465145906 37
phonemacro 0:f51465145906 38 /******************************************************************************/
phonemacro 0:f51465145906 39 MAX31875::MAX31875(I2C &i2c_bus, uint8_t slaveAddress):
phonemacro 0:f51465145906 40 m_i2c(i2c_bus),
phonemacro 0:f51465145906 41 m_writeAddress(slaveAddress <<1),
phonemacro 0:f51465145906 42 m_readAddress ((slaveAddress << 1) | 1),
phonemacro 0:f51465145906 43 m_extended_format(0)
phonemacro 0:f51465145906 44 {
phonemacro 0:f51465145906 45 }
phonemacro 0:f51465145906 46
phonemacro 0:f51465145906 47
phonemacro 0:f51465145906 48 /******************************************************************************/
phonemacro 0:f51465145906 49 MAX31875::~MAX31875(void)
phonemacro 0:f51465145906 50 {
phonemacro 0:f51465145906 51 /** empty block */
phonemacro 0:f51465145906 52 }
phonemacro 0:f51465145906 53
phonemacro 0:f51465145906 54
phonemacro 0:f51465145906 55 /******************************************************************************/
phonemacro 0:f51465145906 56 int MAX31875::read_reg(uint16_t *value, char reg)
phonemacro 0:f51465145906 57 {
phonemacro 0:f51465145906 58 int32_t ret;
phonemacro 0:f51465145906 59 char data[2] = {0, 0};
phonemacro 0:f51465145906 60 max31875_raw_data tmp;
phonemacro 0:f51465145906 61
phonemacro 0:f51465145906 62 if (reg <= MAX31875_REG_MAX) {
phonemacro 0:f51465145906 63 /* write to the Register Select */
phonemacro 0:f51465145906 64 ret = m_i2c.write(m_writeAddress, &reg, 1, true);
phonemacro 0:f51465145906 65 /* read the two bytes of data */
phonemacro 0:f51465145906 66 if (ret == 0) {
phonemacro 0:f51465145906 67 ret = m_i2c.read(m_readAddress, data, 2, false);
phonemacro 0:f51465145906 68 if (ret == 0) {
phonemacro 0:f51465145906 69 tmp.msb = data[0]; /* MSB */
phonemacro 0:f51465145906 70 tmp.lsb = data[1]; /* LSB */
phonemacro 0:f51465145906 71 *value = tmp.swrd;
phonemacro 0:f51465145906 72 return MAX31875_NO_ERROR;
phonemacro 0:f51465145906 73 } else {
phonemacro 0:f51465145906 74 printf(
phonemacro 0:f51465145906 75 "%s: failed to read data: ret: %d\r\n", __func__, ret);
phonemacro 0:f51465145906 76 }
phonemacro 0:f51465145906 77 } else {
phonemacro 0:f51465145906 78 printf("%s: failed to write to Register Select: ret: %d\r\n",
phonemacro 0:f51465145906 79 __func__, ret);
phonemacro 0:f51465145906 80 }
phonemacro 0:f51465145906 81 } else {
phonemacro 0:f51465145906 82 printf("%s: register address is not correct: register: %d\r\n",
phonemacro 0:f51465145906 83 __func__, reg);
phonemacro 0:f51465145906 84 }
phonemacro 0:f51465145906 85 return MAX31875_ERROR;
phonemacro 0:f51465145906 86 }
phonemacro 0:f51465145906 87
phonemacro 0:f51465145906 88
phonemacro 0:f51465145906 89 float MAX31875::read_reg_as_temperature(uint8_t reg)
phonemacro 0:f51465145906 90 {
phonemacro 0:f51465145906 91 max31875_raw_data tmp;
phonemacro 0:f51465145906 92 float temperature;
phonemacro 0:f51465145906 93 if (reg == MAX31875_REG_TEMPERATURE ||
phonemacro 0:f51465145906 94 reg == MAX31875_REG_THYST || reg == MAX31875_REG_TOS) {
phonemacro 0:f51465145906 95 read_reg(&tmp.uwrd, reg);
phonemacro 0:f51465145906 96 temperature = (float)tmp.swrd;
phonemacro 0:f51465145906 97 if (m_extended_format)
phonemacro 0:f51465145906 98 temperature *= MAX31875_CF_EXTENDED_FORMAT;
phonemacro 0:f51465145906 99 else
phonemacro 0:f51465145906 100 temperature *= MAX31875_CF_NORMAL_FORMAT;
phonemacro 0:f51465145906 101 return temperature;
phonemacro 0:f51465145906 102 } else {
phonemacro 0:f51465145906 103 printf("%s: register is invalid, %d r\n", __func__, reg);
phonemacro 0:f51465145906 104 return 0;
phonemacro 0:f51465145906 105 }
phonemacro 0:f51465145906 106 }
phonemacro 0:f51465145906 107
phonemacro 0:f51465145906 108
phonemacro 0:f51465145906 109 /******************************************************************************/
phonemacro 0:f51465145906 110 int MAX31875::write_reg(uint16_t value, char reg)
phonemacro 0:f51465145906 111 {
phonemacro 0:f51465145906 112 int32_t ret;
phonemacro 0:f51465145906 113 char cmd[3];
phonemacro 0:f51465145906 114 max31875_raw_data tmp;
phonemacro 0:f51465145906 115
phonemacro 0:f51465145906 116 if (reg >= MAX31875_REG_CONFIGURATION && reg <= MAX31875_REG_MAX) {
phonemacro 0:f51465145906 117 cmd[0] = reg;
phonemacro 0:f51465145906 118 tmp.uwrd = value;
phonemacro 0:f51465145906 119 cmd[1] = tmp.msb;
phonemacro 0:f51465145906 120 cmd[2] = tmp.lsb;
phonemacro 0:f51465145906 121 ret = m_i2c.write(m_writeAddress, cmd, 3, false);
phonemacro 0:f51465145906 122 if (ret == 0) {
phonemacro 0:f51465145906 123 if (tmp.uwrd & MAX31875_CFG_EXTENDED_FORMAT)
phonemacro 0:f51465145906 124 m_extended_format = 1;
phonemacro 0:f51465145906 125 else
phonemacro 0:f51465145906 126 m_extended_format = 0;
phonemacro 0:f51465145906 127 return MAX31875_NO_ERROR;
phonemacro 0:f51465145906 128 } else {
phonemacro 0:f51465145906 129 printf("%s: I2C write error %d\r\n",__func__, ret);
phonemacro 0:f51465145906 130 return MAX31875_ERROR;
phonemacro 0:f51465145906 131 }
phonemacro 0:f51465145906 132 } else {
phonemacro 0:f51465145906 133 printf("%s: register value invalid %x\r\n",__func__, reg);
phonemacro 0:f51465145906 134 return MAX31875_ERROR;
phonemacro 0:f51465145906 135 }
phonemacro 0:f51465145906 136 }
phonemacro 0:f51465145906 137
phonemacro 0:f51465145906 138
phonemacro 0:f51465145906 139 int MAX31875::write_cfg(uint16_t cfg)
phonemacro 0:f51465145906 140 {
phonemacro 0:f51465145906 141 return write_reg(cfg, MAX31875_REG_CONFIGURATION);
phonemacro 0:f51465145906 142 }
phonemacro 0:f51465145906 143
phonemacro 0:f51465145906 144
phonemacro 0:f51465145906 145 int MAX31875::write_trip_low(float temperature)
phonemacro 0:f51465145906 146 {
phonemacro 0:f51465145906 147 max31875_raw_data raw;
phonemacro 0:f51465145906 148 if (m_extended_format)
phonemacro 0:f51465145906 149 temperature /= MAX31875_CF_EXTENDED_FORMAT;
phonemacro 0:f51465145906 150 else
phonemacro 0:f51465145906 151 temperature /= MAX31875_CF_NORMAL_FORMAT;
phonemacro 0:f51465145906 152 raw.uwrd = uint16_t(temperature);
phonemacro 0:f51465145906 153 return write_reg(raw.uwrd, MAX31875_REG_THYST);
phonemacro 0:f51465145906 154 }
phonemacro 0:f51465145906 155
phonemacro 0:f51465145906 156
phonemacro 0:f51465145906 157 int MAX31875::write_trip_high(float temperature)
phonemacro 0:f51465145906 158 {
phonemacro 0:f51465145906 159 max31875_raw_data raw;
phonemacro 0:f51465145906 160 if (m_extended_format)
phonemacro 0:f51465145906 161 temperature /= MAX31875_CF_EXTENDED_FORMAT;
phonemacro 0:f51465145906 162 else
phonemacro 0:f51465145906 163 temperature /= MAX31875_CF_NORMAL_FORMAT;
phonemacro 0:f51465145906 164 raw.uwrd = uint16_t(temperature);
phonemacro 0:f51465145906 165 return write_reg(raw.uwrd, MAX31875_REG_TOS);
phonemacro 0:f51465145906 166 }
phonemacro 0:f51465145906 167
phonemacro 0:f51465145906 168
phonemacro 0:f51465145906 169 float MAX31875::celsius_to_fahrenheit(float temp_c)
phonemacro 0:f51465145906 170 {
phonemacro 0:f51465145906 171 float temp_f;
phonemacro 0:f51465145906 172 temp_f = ((temp_c * 9)/5) + 32;
phonemacro 0:f51465145906 173 return temp_f;
phonemacro 0:f51465145906 174 }