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