LM77 Temperature sensor with I2C interface. Provides temperature in celsius and fahrenheit. The device also supports temperature alerts.
LM77.cpp@0:8e812deb9f66, 2015-01-10 (annotated)
- Committer:
- wim
- Date:
- Sat Jan 10 19:10:40 2015 +0000
- Revision:
- 0:8e812deb9f66
First version of LM77 library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:8e812deb9f66 | 1 | /* mbed LM77 Library, for an I2C Temperature sensor |
wim | 0:8e812deb9f66 | 2 | * Copyright (c) 2015, v01: WH, Initial version |
wim | 0:8e812deb9f66 | 3 | * |
wim | 0:8e812deb9f66 | 4 | * The LM77 is a digital temperature sensor and thermal window comparator with an I2C Serial Bus interface. |
wim | 0:8e812deb9f66 | 5 | * The open-drain Interrupt (INT) output becomes active whenever temperature goes outside a programmable window, |
wim | 0:8e812deb9f66 | 6 | * while a separate Critical Temperature Alarm (T_CRIT_A) output becomes active when the temperature exceeds a |
wim | 0:8e812deb9f66 | 7 | * programmable critical limit. The INT output can operate in either a comparator or event mode, while the T_CRIT_A |
wim | 0:8e812deb9f66 | 8 | * output operates in comparator mode only. The host can program both the upper and lower limits of the window as |
wim | 0:8e812deb9f66 | 9 | * well as the critical temperature limit. Programmable hysterisis as well as a fault queue are available to minimize |
wim | 0:8e812deb9f66 | 10 | * false tripping. Two pins (A0, A1) are available for address selection. The sensor powers up with default thresholds |
wim | 0:8e812deb9f66 | 11 | * of 2°C THYST, 10°C TLOW, 64°C THIGH, and 80°C TCRIT. |
wim | 0:8e812deb9f66 | 12 | * |
wim | 0:8e812deb9f66 | 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:8e812deb9f66 | 14 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:8e812deb9f66 | 15 | * in the Software without restriction, including without limitation the rights |
wim | 0:8e812deb9f66 | 16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:8e812deb9f66 | 17 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:8e812deb9f66 | 18 | * furnished to do so, subject to the following conditions: |
wim | 0:8e812deb9f66 | 19 | * |
wim | 0:8e812deb9f66 | 20 | * The above copyright notice and this permission notice shall be included in |
wim | 0:8e812deb9f66 | 21 | * all copies or substantial portions of the Software. |
wim | 0:8e812deb9f66 | 22 | * |
wim | 0:8e812deb9f66 | 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:8e812deb9f66 | 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:8e812deb9f66 | 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:8e812deb9f66 | 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:8e812deb9f66 | 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:8e812deb9f66 | 28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:8e812deb9f66 | 29 | * THE SOFTWARE. |
wim | 0:8e812deb9f66 | 30 | */ |
wim | 0:8e812deb9f66 | 31 | #include "LM77.h" |
wim | 0:8e812deb9f66 | 32 | |
wim | 0:8e812deb9f66 | 33 | /** Create an LM77 device instance |
wim | 0:8e812deb9f66 | 34 | * |
wim | 0:8e812deb9f66 | 35 | * @param i2c I2C Bus |
wim | 0:8e812deb9f66 | 36 | * @param char deviceAddress I2C slaveaddress |
wim | 0:8e812deb9f66 | 37 | */ |
wim | 0:8e812deb9f66 | 38 | LM77::LM77(I2C *i2c, char deviceAddress) : _i2c(i2c), _slaveAddress(deviceAddress) { |
wim | 0:8e812deb9f66 | 39 | |
wim | 0:8e812deb9f66 | 40 | } |
wim | 0:8e812deb9f66 | 41 | |
wim | 0:8e812deb9f66 | 42 | /** Get Status |
wim | 0:8e812deb9f66 | 43 | * |
wim | 0:8e812deb9f66 | 44 | * @return bool Sensor ready |
wim | 0:8e812deb9f66 | 45 | */ |
wim | 0:8e812deb9f66 | 46 | bool LM77::getStatus(void) { |
wim | 0:8e812deb9f66 | 47 | char buffer[2]; |
wim | 0:8e812deb9f66 | 48 | int status; |
wim | 0:8e812deb9f66 | 49 | |
wim | 0:8e812deb9f66 | 50 | // Dummy operation to check status |
wim | 0:8e812deb9f66 | 51 | buffer[0] = 0x00; |
wim | 0:8e812deb9f66 | 52 | status=_i2c->write(_slaveAddress, buffer, 0); |
wim | 0:8e812deb9f66 | 53 | |
wim | 0:8e812deb9f66 | 54 | return (status==0); // True when device found |
wim | 0:8e812deb9f66 | 55 | } |
wim | 0:8e812deb9f66 | 56 | |
wim | 0:8e812deb9f66 | 57 | |
wim | 0:8e812deb9f66 | 58 | /** Get the current power mode of the LM77 |
wim | 0:8e812deb9f66 | 59 | * |
wim | 0:8e812deb9f66 | 60 | * @returns The current power mode as a PowerMode enum. |
wim | 0:8e812deb9f66 | 61 | */ |
wim | 0:8e812deb9f66 | 62 | LM77::PowerMode LM77::getPowerMode() { |
wim | 0:8e812deb9f66 | 63 | |
wim | 0:8e812deb9f66 | 64 | //Read the 8-bit register value |
wim | 0:8e812deb9f66 | 65 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 66 | |
wim | 0:8e812deb9f66 | 67 | //Return the status of the SHUTDOWN bit |
wim | 0:8e812deb9f66 | 68 | if (value & LM77_PWR_MSK) { |
wim | 0:8e812deb9f66 | 69 | return POWER_SHUTDOWN; |
wim | 0:8e812deb9f66 | 70 | } |
wim | 0:8e812deb9f66 | 71 | else { |
wim | 0:8e812deb9f66 | 72 | return POWER_NORMAL; |
wim | 0:8e812deb9f66 | 73 | } |
wim | 0:8e812deb9f66 | 74 | } |
wim | 0:8e812deb9f66 | 75 | |
wim | 0:8e812deb9f66 | 76 | |
wim | 0:8e812deb9f66 | 77 | /** Set the power mode of the LM77 |
wim | 0:8e812deb9f66 | 78 | * |
wim | 0:8e812deb9f66 | 79 | * @param mode The new power mode as a PowerMode enum. |
wim | 0:8e812deb9f66 | 80 | */ |
wim | 0:8e812deb9f66 | 81 | void LM77::setPowerMode(PowerMode mode) { |
wim | 0:8e812deb9f66 | 82 | |
wim | 0:8e812deb9f66 | 83 | //Read the current 8-bit register value |
wim | 0:8e812deb9f66 | 84 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 85 | |
wim | 0:8e812deb9f66 | 86 | //Set or clear the SHUTDOWN bit |
wim | 0:8e812deb9f66 | 87 | if (mode == POWER_SHUTDOWN) { |
wim | 0:8e812deb9f66 | 88 | value |= LM77_PWR_DWN; |
wim | 0:8e812deb9f66 | 89 | } |
wim | 0:8e812deb9f66 | 90 | else { |
wim | 0:8e812deb9f66 | 91 | value &= ~LM77_PWR_DWN; |
wim | 0:8e812deb9f66 | 92 | } |
wim | 0:8e812deb9f66 | 93 | |
wim | 0:8e812deb9f66 | 94 | //Write the value back out |
wim | 0:8e812deb9f66 | 95 | _writeReg8(LM77_REG_CONF, value); |
wim | 0:8e812deb9f66 | 96 | } |
wim | 0:8e812deb9f66 | 97 | |
wim | 0:8e812deb9f66 | 98 | |
wim | 0:8e812deb9f66 | 99 | /** Get the current INT pin mode of the LM77 |
wim | 0:8e812deb9f66 | 100 | * |
wim | 0:8e812deb9f66 | 101 | * @returns The current INT pin mode as an IntMode enum. |
wim | 0:8e812deb9f66 | 102 | */ |
wim | 0:8e812deb9f66 | 103 | LM77::IntMode LM77::getIntMode() { |
wim | 0:8e812deb9f66 | 104 | |
wim | 0:8e812deb9f66 | 105 | //Read the 8-bit register value |
wim | 0:8e812deb9f66 | 106 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 107 | |
wim | 0:8e812deb9f66 | 108 | //Return the status of the INT_MODE bit |
wim | 0:8e812deb9f66 | 109 | if (value & LM77_INT_MSK) { |
wim | 0:8e812deb9f66 | 110 | return INT_EVENT; |
wim | 0:8e812deb9f66 | 111 | } |
wim | 0:8e812deb9f66 | 112 | else { |
wim | 0:8e812deb9f66 | 113 | return INT_COMPARATOR; |
wim | 0:8e812deb9f66 | 114 | } |
wim | 0:8e812deb9f66 | 115 | } |
wim | 0:8e812deb9f66 | 116 | |
wim | 0:8e812deb9f66 | 117 | |
wim | 0:8e812deb9f66 | 118 | /** Set the INT pin mode of the LM77 |
wim | 0:8e812deb9f66 | 119 | * |
wim | 0:8e812deb9f66 | 120 | * @param mode The new INT pin mode as an IntMode enum. |
wim | 0:8e812deb9f66 | 121 | */ |
wim | 0:8e812deb9f66 | 122 | void LM77::setIntMode(IntMode mode) { |
wim | 0:8e812deb9f66 | 123 | |
wim | 0:8e812deb9f66 | 124 | //Read the current 8-bit register value |
wim | 0:8e812deb9f66 | 125 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 126 | |
wim | 0:8e812deb9f66 | 127 | //Set or clear the OS_COMP_INT bit |
wim | 0:8e812deb9f66 | 128 | if (mode == INT_EVENT) { |
wim | 0:8e812deb9f66 | 129 | value |= LM77_INT_EVENT; |
wim | 0:8e812deb9f66 | 130 | } |
wim | 0:8e812deb9f66 | 131 | else { |
wim | 0:8e812deb9f66 | 132 | // Comparator mode |
wim | 0:8e812deb9f66 | 133 | value &= ~LM77_INT_EVENT; |
wim | 0:8e812deb9f66 | 134 | } |
wim | 0:8e812deb9f66 | 135 | |
wim | 0:8e812deb9f66 | 136 | //Write the value back out |
wim | 0:8e812deb9f66 | 137 | _writeReg8(LM77_REG_CONF, value); |
wim | 0:8e812deb9f66 | 138 | } |
wim | 0:8e812deb9f66 | 139 | |
wim | 0:8e812deb9f66 | 140 | |
wim | 0:8e812deb9f66 | 141 | /** Get the current INT pin polarity of the LM77 |
wim | 0:8e812deb9f66 | 142 | * |
wim | 0:8e812deb9f66 | 143 | * @returns The current INT pin polarity as an PinPolarity enum. |
wim | 0:8e812deb9f66 | 144 | */ |
wim | 0:8e812deb9f66 | 145 | LM77::PinPolarity LM77::getIntPolarity() { |
wim | 0:8e812deb9f66 | 146 | |
wim | 0:8e812deb9f66 | 147 | //Read the 8-bit register value |
wim | 0:8e812deb9f66 | 148 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 149 | |
wim | 0:8e812deb9f66 | 150 | //Return the status of the POL_INT bit |
wim | 0:8e812deb9f66 | 151 | if (value & LM77_POL_INT_MSK) { |
wim | 0:8e812deb9f66 | 152 | return ACTIVE_HIGH; |
wim | 0:8e812deb9f66 | 153 | } |
wim | 0:8e812deb9f66 | 154 | else { |
wim | 0:8e812deb9f66 | 155 | return ACTIVE_LOW; |
wim | 0:8e812deb9f66 | 156 | } |
wim | 0:8e812deb9f66 | 157 | } |
wim | 0:8e812deb9f66 | 158 | |
wim | 0:8e812deb9f66 | 159 | /** Set the INT pin polarity of the LM77 |
wim | 0:8e812deb9f66 | 160 | * |
wim | 0:8e812deb9f66 | 161 | * @param polarity The new INT pin polarity as an PinPolarity enum. |
wim | 0:8e812deb9f66 | 162 | */ |
wim | 0:8e812deb9f66 | 163 | void LM77::setIntPolarity(PinPolarity polarity) |
wim | 0:8e812deb9f66 | 164 | { |
wim | 0:8e812deb9f66 | 165 | //Read the current 8-bit register value |
wim | 0:8e812deb9f66 | 166 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 167 | |
wim | 0:8e812deb9f66 | 168 | //Set or clear the POL_INT bit |
wim | 0:8e812deb9f66 | 169 | if (polarity == ACTIVE_HIGH) { |
wim | 0:8e812deb9f66 | 170 | value |= LM77_POL_INT_H; |
wim | 0:8e812deb9f66 | 171 | } |
wim | 0:8e812deb9f66 | 172 | else { |
wim | 0:8e812deb9f66 | 173 | value &= ~LM77_POL_INT_H; |
wim | 0:8e812deb9f66 | 174 | } |
wim | 0:8e812deb9f66 | 175 | |
wim | 0:8e812deb9f66 | 176 | //Write the value back out |
wim | 0:8e812deb9f66 | 177 | _writeReg8(LM77_REG_CONF, value); |
wim | 0:8e812deb9f66 | 178 | } |
wim | 0:8e812deb9f66 | 179 | |
wim | 0:8e812deb9f66 | 180 | /** Get the current T_CRIT_A pin polarity of the LM77 |
wim | 0:8e812deb9f66 | 181 | * |
wim | 0:8e812deb9f66 | 182 | * @returns The current T_CRIT_A pin polarity as an PinPolarity enum. |
wim | 0:8e812deb9f66 | 183 | */ |
wim | 0:8e812deb9f66 | 184 | LM77::PinPolarity LM77::getTCritPolarity() { |
wim | 0:8e812deb9f66 | 185 | |
wim | 0:8e812deb9f66 | 186 | //Read the 8-bit register value |
wim | 0:8e812deb9f66 | 187 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 188 | |
wim | 0:8e812deb9f66 | 189 | //Return the status of the POL_TCRIT bit |
wim | 0:8e812deb9f66 | 190 | if (value & LM77_POL_TCRIT_MSK) { |
wim | 0:8e812deb9f66 | 191 | return ACTIVE_HIGH; |
wim | 0:8e812deb9f66 | 192 | } |
wim | 0:8e812deb9f66 | 193 | else { |
wim | 0:8e812deb9f66 | 194 | return ACTIVE_LOW; |
wim | 0:8e812deb9f66 | 195 | } |
wim | 0:8e812deb9f66 | 196 | } |
wim | 0:8e812deb9f66 | 197 | |
wim | 0:8e812deb9f66 | 198 | /** Set the T_CRIT_A pin polarity of the LM77 |
wim | 0:8e812deb9f66 | 199 | * |
wim | 0:8e812deb9f66 | 200 | * @param polarity The new T_CRIT_A pin polarity as an PinPolarity enum. |
wim | 0:8e812deb9f66 | 201 | */ |
wim | 0:8e812deb9f66 | 202 | void LM77::setTCritPolarity(PinPolarity polarity) |
wim | 0:8e812deb9f66 | 203 | { |
wim | 0:8e812deb9f66 | 204 | //Read the current 8-bit register value |
wim | 0:8e812deb9f66 | 205 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 206 | |
wim | 0:8e812deb9f66 | 207 | //Set or clear the POL_TCRIT bit |
wim | 0:8e812deb9f66 | 208 | if (polarity == ACTIVE_HIGH) { |
wim | 0:8e812deb9f66 | 209 | value |= LM77_POL_TCRIT_H; |
wim | 0:8e812deb9f66 | 210 | } |
wim | 0:8e812deb9f66 | 211 | else { |
wim | 0:8e812deb9f66 | 212 | value &= ~LM77_POL_TCRIT_H; |
wim | 0:8e812deb9f66 | 213 | } |
wim | 0:8e812deb9f66 | 214 | |
wim | 0:8e812deb9f66 | 215 | //Write the value back out |
wim | 0:8e812deb9f66 | 216 | _writeReg8(LM77_REG_CONF, value); |
wim | 0:8e812deb9f66 | 217 | } |
wim | 0:8e812deb9f66 | 218 | |
wim | 0:8e812deb9f66 | 219 | |
wim | 0:8e812deb9f66 | 220 | /** Get the current pin and flag fault queue length of the LM77 |
wim | 0:8e812deb9f66 | 221 | * |
wim | 0:8e812deb9f66 | 222 | * @returns The current pin and flag fault queue length as an FaultQueue enum. |
wim | 0:8e812deb9f66 | 223 | */ |
wim | 0:8e812deb9f66 | 224 | LM77::FaultQueue LM77::getFaultQueue() |
wim | 0:8e812deb9f66 | 225 | { |
wim | 0:8e812deb9f66 | 226 | //Read the 8-bit register value |
wim | 0:8e812deb9f66 | 227 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 228 | |
wim | 0:8e812deb9f66 | 229 | //Return the status of the FAULT_QUE bit |
wim | 0:8e812deb9f66 | 230 | if (value & LM77_FQU_MSK) { |
wim | 0:8e812deb9f66 | 231 | return FAULT_QUEUE_4; |
wim | 0:8e812deb9f66 | 232 | } |
wim | 0:8e812deb9f66 | 233 | else { |
wim | 0:8e812deb9f66 | 234 | return FAULT_QUEUE_1; |
wim | 0:8e812deb9f66 | 235 | } |
wim | 0:8e812deb9f66 | 236 | } |
wim | 0:8e812deb9f66 | 237 | |
wim | 0:8e812deb9f66 | 238 | |
wim | 0:8e812deb9f66 | 239 | /** Set the pin and flag fault queue length of the LM77 |
wim | 0:8e812deb9f66 | 240 | * |
wim | 0:8e812deb9f66 | 241 | * @param queue The new pin and flag fault queue length as an FaultQueue enum. |
wim | 0:8e812deb9f66 | 242 | */ |
wim | 0:8e812deb9f66 | 243 | void LM77::setFaultQueue(FaultQueue queue) |
wim | 0:8e812deb9f66 | 244 | { |
wim | 0:8e812deb9f66 | 245 | //Read the current 8-bit register value |
wim | 0:8e812deb9f66 | 246 | char value = _readReg8(LM77_REG_CONF); |
wim | 0:8e812deb9f66 | 247 | |
wim | 0:8e812deb9f66 | 248 | //Set the new FAULT_QUE bit |
wim | 0:8e812deb9f66 | 249 | if (queue == FAULT_QUEUE_4) { |
wim | 0:8e812deb9f66 | 250 | value |= LM77_FQU_4; |
wim | 0:8e812deb9f66 | 251 | } |
wim | 0:8e812deb9f66 | 252 | else { |
wim | 0:8e812deb9f66 | 253 | value &= ~LM77_FQU_4; |
wim | 0:8e812deb9f66 | 254 | } |
wim | 0:8e812deb9f66 | 255 | |
wim | 0:8e812deb9f66 | 256 | //Write the value back out |
wim | 0:8e812deb9f66 | 257 | _writeReg8(LM77_REG_CONF, value); |
wim | 0:8e812deb9f66 | 258 | } |
wim | 0:8e812deb9f66 | 259 | |
wim | 0:8e812deb9f66 | 260 | /** Get the current Critical alert temperature threshold of the LM77 |
wim | 0:8e812deb9f66 | 261 | * Reset value is 80.0 °C. |
wim | 0:8e812deb9f66 | 262 | * |
wim | 0:8e812deb9f66 | 263 | * @returns The current Critical alert temperature threshold in °C. |
wim | 0:8e812deb9f66 | 264 | */ |
wim | 0:8e812deb9f66 | 265 | float LM77::getCritAlertTemp() { |
wim | 0:8e812deb9f66 | 266 | |
wim | 0:8e812deb9f66 | 267 | //Use the 9-bit helper to read the TCRIT register |
wim | 0:8e812deb9f66 | 268 | return _readTempHelper(LM77_REG_TCRIT); |
wim | 0:8e812deb9f66 | 269 | } |
wim | 0:8e812deb9f66 | 270 | |
wim | 0:8e812deb9f66 | 271 | /** Set the critical alert temperature threshold of the LM77 |
wim | 0:8e812deb9f66 | 272 | * Reset value is 80.0 °C. |
wim | 0:8e812deb9f66 | 273 | * |
wim | 0:8e812deb9f66 | 274 | * @param temp The new critical alert temperature threshold in °C. |
wim | 0:8e812deb9f66 | 275 | */ |
wim | 0:8e812deb9f66 | 276 | void LM77::setCritAlertTemp(float temp) { |
wim | 0:8e812deb9f66 | 277 | |
wim | 0:8e812deb9f66 | 278 | //Use the 9-bit helper to write to the TCRIT register |
wim | 0:8e812deb9f66 | 279 | _writeTempHelper(LM77_REG_TCRIT, temp); |
wim | 0:8e812deb9f66 | 280 | } |
wim | 0:8e812deb9f66 | 281 | |
wim | 0:8e812deb9f66 | 282 | /** Get the current Low temperature alert threshold of the LM77 |
wim | 0:8e812deb9f66 | 283 | * Reset value is 10.0 °C. |
wim | 0:8e812deb9f66 | 284 | * |
wim | 0:8e812deb9f66 | 285 | * @returns The current Low temperature alert threshold in °C. |
wim | 0:8e812deb9f66 | 286 | */ |
wim | 0:8e812deb9f66 | 287 | float LM77::getLowAlertTemp() { |
wim | 0:8e812deb9f66 | 288 | |
wim | 0:8e812deb9f66 | 289 | //Use the 9-bit helper to read the TLOW register |
wim | 0:8e812deb9f66 | 290 | return _readTempHelper(LM77_REG_TLOW); |
wim | 0:8e812deb9f66 | 291 | } |
wim | 0:8e812deb9f66 | 292 | |
wim | 0:8e812deb9f66 | 293 | /** Set the current Low temperature alert threshold of the LM77 |
wim | 0:8e812deb9f66 | 294 | * Reset value is 10.0 °C. |
wim | 0:8e812deb9f66 | 295 | * |
wim | 0:8e812deb9f66 | 296 | * @param temp The new Low alert temperature threshold in °C. |
wim | 0:8e812deb9f66 | 297 | */ |
wim | 0:8e812deb9f66 | 298 | void LM77::setLowAlertTemp(float temp){ |
wim | 0:8e812deb9f66 | 299 | |
wim | 0:8e812deb9f66 | 300 | //Use the 9-bit helper to write to the TLOW register |
wim | 0:8e812deb9f66 | 301 | _writeTempHelper(LM77_REG_TLOW, temp); |
wim | 0:8e812deb9f66 | 302 | } |
wim | 0:8e812deb9f66 | 303 | |
wim | 0:8e812deb9f66 | 304 | /** Get the current High temperature alert threshold of the LM77 |
wim | 0:8e812deb9f66 | 305 | * Reset value is 64.0 °C. |
wim | 0:8e812deb9f66 | 306 | * |
wim | 0:8e812deb9f66 | 307 | * @returns The current High temperature alert threshold in °C. |
wim | 0:8e812deb9f66 | 308 | */ |
wim | 0:8e812deb9f66 | 309 | float LM77::getHighAlertTemp(){ |
wim | 0:8e812deb9f66 | 310 | |
wim | 0:8e812deb9f66 | 311 | //Use the 9-bit helper to read the THIGH register |
wim | 0:8e812deb9f66 | 312 | return _readTempHelper(LM77_REG_THIGH); |
wim | 0:8e812deb9f66 | 313 | } |
wim | 0:8e812deb9f66 | 314 | |
wim | 0:8e812deb9f66 | 315 | /** Set the High temperature alert threshold of the LM77 |
wim | 0:8e812deb9f66 | 316 | * Reset value is 64.0 °C. |
wim | 0:8e812deb9f66 | 317 | * |
wim | 0:8e812deb9f66 | 318 | * @param temp The new High temperature alert threshold in °C. |
wim | 0:8e812deb9f66 | 319 | */ |
wim | 0:8e812deb9f66 | 320 | void LM77::setHighAlertTemp(float temp) { |
wim | 0:8e812deb9f66 | 321 | |
wim | 0:8e812deb9f66 | 322 | //Use the 9-bit helper to write to the THIGH register |
wim | 0:8e812deb9f66 | 323 | _writeTempHelper(LM77_REG_THIGH, temp); |
wim | 0:8e812deb9f66 | 324 | } |
wim | 0:8e812deb9f66 | 325 | |
wim | 0:8e812deb9f66 | 326 | |
wim | 0:8e812deb9f66 | 327 | /** Get the current alert temperature hysteresis of the LM77 |
wim | 0:8e812deb9f66 | 328 | * Reset value is 2.0 °C. |
wim | 0:8e812deb9f66 | 329 | * |
wim | 0:8e812deb9f66 | 330 | * @returns The current alert temperature hysteresis in °C. |
wim | 0:8e812deb9f66 | 331 | */ |
wim | 0:8e812deb9f66 | 332 | float LM77::getAlertHyst() { |
wim | 0:8e812deb9f66 | 333 | |
wim | 0:8e812deb9f66 | 334 | //Use the 9-bit helper to read the THYST register |
wim | 0:8e812deb9f66 | 335 | return _readTempHelper(LM77_REG_THYST); |
wim | 0:8e812deb9f66 | 336 | } |
wim | 0:8e812deb9f66 | 337 | |
wim | 0:8e812deb9f66 | 338 | /** Set the alert temperature hysteresis of the LM77 |
wim | 0:8e812deb9f66 | 339 | * Reset value is 2.0 °C. |
wim | 0:8e812deb9f66 | 340 | * |
wim | 0:8e812deb9f66 | 341 | * @param temp The new alert temperature hysteresis in °C. |
wim | 0:8e812deb9f66 | 342 | */ |
wim | 0:8e812deb9f66 | 343 | void LM77::setAlertHyst(float temp) { |
wim | 0:8e812deb9f66 | 344 | |
wim | 0:8e812deb9f66 | 345 | //Use the 9-bit helper to write to the THYST register |
wim | 0:8e812deb9f66 | 346 | _writeTempHelper(LM77_REG_THYST, temp); |
wim | 0:8e812deb9f66 | 347 | } |
wim | 0:8e812deb9f66 | 348 | |
wim | 0:8e812deb9f66 | 349 | |
wim | 0:8e812deb9f66 | 350 | /** Get Temperature as int in °Celsius x 10 |
wim | 0:8e812deb9f66 | 351 | * |
wim | 0:8e812deb9f66 | 352 | * @return int Temperature in °Celsius x 10 |
wim | 0:8e812deb9f66 | 353 | */ |
wim | 0:8e812deb9f66 | 354 | int LM77::getTemperatureInt(void) { |
wim | 0:8e812deb9f66 | 355 | |
wim | 0:8e812deb9f66 | 356 | //Signed return value |
wim | 0:8e812deb9f66 | 357 | int16_t value; |
wim | 0:8e812deb9f66 | 358 | |
wim | 0:8e812deb9f66 | 359 | //Read the 9-bit raw temperature value in 0.5 degree resolution |
wim | 0:8e812deb9f66 | 360 | value = _readReg16(LM77_REG_TEMP) >> 3; |
wim | 0:8e812deb9f66 | 361 | |
wim | 0:8e812deb9f66 | 362 | //Sign extend negative numbers |
wim | 0:8e812deb9f66 | 363 | if (value & (1 << 9)) |
wim | 0:8e812deb9f66 | 364 | value |= 0xFE00; |
wim | 0:8e812deb9f66 | 365 | |
wim | 0:8e812deb9f66 | 366 | //Return the temperature in °C x 10 |
wim | 0:8e812deb9f66 | 367 | return (int) value * 5; // x 10 x 0.5 |
wim | 0:8e812deb9f66 | 368 | } |
wim | 0:8e812deb9f66 | 369 | |
wim | 0:8e812deb9f66 | 370 | /** Get the Alert flags of the LM77 |
wim | 0:8e812deb9f66 | 371 | * |
wim | 0:8e812deb9f66 | 372 | * @returns The current Alert flags as int. |
wim | 0:8e812deb9f66 | 373 | */ |
wim | 0:8e812deb9f66 | 374 | int LM77::getAlertFlags() { |
wim | 0:8e812deb9f66 | 375 | |
wim | 0:8e812deb9f66 | 376 | //Signed return value |
wim | 0:8e812deb9f66 | 377 | int16_t value; |
wim | 0:8e812deb9f66 | 378 | |
wim | 0:8e812deb9f66 | 379 | //Read the 9-bit raw temperature value and the flags |
wim | 0:8e812deb9f66 | 380 | value = _readReg16(LM77_REG_TEMP); |
wim | 0:8e812deb9f66 | 381 | |
wim | 0:8e812deb9f66 | 382 | //Return the flag bits |
wim | 0:8e812deb9f66 | 383 | // LM77_FLAG_LOW 0x01 |
wim | 0:8e812deb9f66 | 384 | // LM77_FLAG_HIGH 0x02 |
wim | 0:8e812deb9f66 | 385 | // LM77_FLAG_CRIT 0x04 |
wim | 0:8e812deb9f66 | 386 | // LM77_FLAG_MSK 0x07 |
wim | 0:8e812deb9f66 | 387 | return (value & LM77_FLAG_MSK); |
wim | 0:8e812deb9f66 | 388 | } |
wim | 0:8e812deb9f66 | 389 | |
wim | 0:8e812deb9f66 | 390 | |
wim | 0:8e812deb9f66 | 391 | /** Get Temperature as float in °Celsius |
wim | 0:8e812deb9f66 | 392 | * |
wim | 0:8e812deb9f66 | 393 | * @return float Temperature in °Celsius |
wim | 0:8e812deb9f66 | 394 | */ |
wim | 0:8e812deb9f66 | 395 | float LM77::getTemperature(void) { |
wim | 0:8e812deb9f66 | 396 | |
wim | 0:8e812deb9f66 | 397 | return _readTempHelper(LM77_REG_TEMP); |
wim | 0:8e812deb9f66 | 398 | } |
wim | 0:8e812deb9f66 | 399 | |
wim | 0:8e812deb9f66 | 400 | #ifdef MBED_OPERATORS |
wim | 0:8e812deb9f66 | 401 | LM77::operator float() { |
wim | 0:8e812deb9f66 | 402 | |
wim | 0:8e812deb9f66 | 403 | //Return the current temperature reading |
wim | 0:8e812deb9f66 | 404 | return getTemperature(); |
wim | 0:8e812deb9f66 | 405 | } |
wim | 0:8e812deb9f66 | 406 | #endif |
wim | 0:8e812deb9f66 | 407 | |
wim | 0:8e812deb9f66 | 408 | /** Convert Temperature from °Celsius into °Fahrenheit |
wim | 0:8e812deb9f66 | 409 | * |
wim | 0:8e812deb9f66 | 410 | * @param float celsius in °Celsius |
wim | 0:8e812deb9f66 | 411 | * @return float temperature in °Fahrenheit |
wim | 0:8e812deb9f66 | 412 | */ |
wim | 0:8e812deb9f66 | 413 | float LM77::celsiusToFahrenheit(float celsius) { |
wim | 0:8e812deb9f66 | 414 | |
wim | 0:8e812deb9f66 | 415 | return ((celsius * 9.0f) / 5.0f) + 32.0f; // Convert to Fahrenheit |
wim | 0:8e812deb9f66 | 416 | } |
wim | 0:8e812deb9f66 | 417 | |
wim | 0:8e812deb9f66 | 418 | |
wim | 0:8e812deb9f66 | 419 | /** Read 8 bit value from register |
wim | 0:8e812deb9f66 | 420 | * |
wim | 0:8e812deb9f66 | 421 | * @param reg Index of register |
wim | 0:8e812deb9f66 | 422 | * @return data value from register |
wim | 0:8e812deb9f66 | 423 | */ |
wim | 0:8e812deb9f66 | 424 | char LM77::_readReg8(char reg) { |
wim | 0:8e812deb9f66 | 425 | |
wim | 0:8e812deb9f66 | 426 | //Select the register |
wim | 0:8e812deb9f66 | 427 | _i2c->write(_slaveAddress, ®, 1, true); |
wim | 0:8e812deb9f66 | 428 | |
wim | 0:8e812deb9f66 | 429 | //Read the 8-bit register |
wim | 0:8e812deb9f66 | 430 | _i2c->read(_slaveAddress, ®, 1); |
wim | 0:8e812deb9f66 | 431 | |
wim | 0:8e812deb9f66 | 432 | //Return the byte |
wim | 0:8e812deb9f66 | 433 | return reg; |
wim | 0:8e812deb9f66 | 434 | } |
wim | 0:8e812deb9f66 | 435 | |
wim | 0:8e812deb9f66 | 436 | /** Write 8 bit value to register |
wim | 0:8e812deb9f66 | 437 | * |
wim | 0:8e812deb9f66 | 438 | * @param reg Index of register |
wim | 0:8e812deb9f66 | 439 | * @param data value to write |
wim | 0:8e812deb9f66 | 440 | */ |
wim | 0:8e812deb9f66 | 441 | void LM77::_writeReg8(char reg, char data) { |
wim | 0:8e812deb9f66 | 442 | |
wim | 0:8e812deb9f66 | 443 | //Create a temporary buffer |
wim | 0:8e812deb9f66 | 444 | char buf[2]; |
wim | 0:8e812deb9f66 | 445 | |
wim | 0:8e812deb9f66 | 446 | //Load the register address and 8-bit data |
wim | 0:8e812deb9f66 | 447 | buf[0] = reg; |
wim | 0:8e812deb9f66 | 448 | buf[1] = data; |
wim | 0:8e812deb9f66 | 449 | |
wim | 0:8e812deb9f66 | 450 | //Write the data |
wim | 0:8e812deb9f66 | 451 | _i2c->write(_slaveAddress, buf, 2); |
wim | 0:8e812deb9f66 | 452 | } |
wim | 0:8e812deb9f66 | 453 | |
wim | 0:8e812deb9f66 | 454 | /** Read 16 bit value from register |
wim | 0:8e812deb9f66 | 455 | * Used for Critical temp threshold, Low and High temp threshold window and Hysteresis |
wim | 0:8e812deb9f66 | 456 | * |
wim | 0:8e812deb9f66 | 457 | * @param reg Index of register |
wim | 0:8e812deb9f66 | 458 | * @return data value from register |
wim | 0:8e812deb9f66 | 459 | */ |
wim | 0:8e812deb9f66 | 460 | int16_t LM77::_readReg16(char reg) { |
wim | 0:8e812deb9f66 | 461 | |
wim | 0:8e812deb9f66 | 462 | //Create a temporary buffer |
wim | 0:8e812deb9f66 | 463 | char buf[2]; |
wim | 0:8e812deb9f66 | 464 | |
wim | 0:8e812deb9f66 | 465 | //Select the register |
wim | 0:8e812deb9f66 | 466 | _i2c->write(_slaveAddress, ®, 1, true); |
wim | 0:8e812deb9f66 | 467 | |
wim | 0:8e812deb9f66 | 468 | //Read the 16-bit register |
wim | 0:8e812deb9f66 | 469 | _i2c->read(_slaveAddress, buf, 2); |
wim | 0:8e812deb9f66 | 470 | |
wim | 0:8e812deb9f66 | 471 | //Return the combined 16-bit value |
wim | 0:8e812deb9f66 | 472 | return (buf[0] << 8) | buf[1]; |
wim | 0:8e812deb9f66 | 473 | } |
wim | 0:8e812deb9f66 | 474 | |
wim | 0:8e812deb9f66 | 475 | /** Write 16 bit value to register |
wim | 0:8e812deb9f66 | 476 | * Used for Critical temp threshold, Low and High temp threshold window and Hysteresis |
wim | 0:8e812deb9f66 | 477 | * |
wim | 0:8e812deb9f66 | 478 | * @param reg Index of register |
wim | 0:8e812deb9f66 | 479 | * @param data value to write |
wim | 0:8e812deb9f66 | 480 | */ |
wim | 0:8e812deb9f66 | 481 | void LM77::_writeReg16(char reg, int16_t data) { |
wim | 0:8e812deb9f66 | 482 | |
wim | 0:8e812deb9f66 | 483 | //Create a temporary buffer |
wim | 0:8e812deb9f66 | 484 | char buf[3]; |
wim | 0:8e812deb9f66 | 485 | |
wim | 0:8e812deb9f66 | 486 | //Load the register address and 16-bit data |
wim | 0:8e812deb9f66 | 487 | buf[0] = reg; |
wim | 0:8e812deb9f66 | 488 | buf[1] = data >> 8; |
wim | 0:8e812deb9f66 | 489 | buf[2] = data; |
wim | 0:8e812deb9f66 | 490 | |
wim | 0:8e812deb9f66 | 491 | //Write the data |
wim | 0:8e812deb9f66 | 492 | _i2c->write(_slaveAddress, buf, 3); |
wim | 0:8e812deb9f66 | 493 | } |
wim | 0:8e812deb9f66 | 494 | |
wim | 0:8e812deb9f66 | 495 | /** Get Temperature as float in °Celsius |
wim | 0:8e812deb9f66 | 496 | * Used for Critical temp threshold, Low and High temp threshold window and Hysteresis |
wim | 0:8e812deb9f66 | 497 | * |
wim | 0:8e812deb9f66 | 498 | * @param reg Index of register to read temp value |
wim | 0:8e812deb9f66 | 499 | * @return float Temperature in °Celsius |
wim | 0:8e812deb9f66 | 500 | */ |
wim | 0:8e812deb9f66 | 501 | float LM77::_readTempHelper(char reg) { |
wim | 0:8e812deb9f66 | 502 | |
wim | 0:8e812deb9f66 | 503 | //Signed return value |
wim | 0:8e812deb9f66 | 504 | int16_t value; |
wim | 0:8e812deb9f66 | 505 | |
wim | 0:8e812deb9f66 | 506 | #if(1) |
wim | 0:8e812deb9f66 | 507 | //Read the 9-bit raw temperature value in 0.5 degree resolution |
wim | 0:8e812deb9f66 | 508 | value = _readReg16(reg) >> 3; |
wim | 0:8e812deb9f66 | 509 | #else |
wim | 0:8e812deb9f66 | 510 | int16_t readreg; |
wim | 0:8e812deb9f66 | 511 | |
wim | 0:8e812deb9f66 | 512 | //Test neg temps |
wim | 0:8e812deb9f66 | 513 | readreg = 0xFE70; //-25 degree |
wim | 0:8e812deb9f66 | 514 | value = readreg >> 3; |
wim | 0:8e812deb9f66 | 515 | #endif |
wim | 0:8e812deb9f66 | 516 | |
wim | 0:8e812deb9f66 | 517 | //Sign extend negative numbers |
wim | 0:8e812deb9f66 | 518 | if (value & (1 << 9)) |
wim | 0:8e812deb9f66 | 519 | value |= 0xFE00; |
wim | 0:8e812deb9f66 | 520 | |
wim | 0:8e812deb9f66 | 521 | //Return the temperature in °C |
wim | 0:8e812deb9f66 | 522 | return (float) value * 0.5f; |
wim | 0:8e812deb9f66 | 523 | } |
wim | 0:8e812deb9f66 | 524 | |
wim | 0:8e812deb9f66 | 525 | |
wim | 0:8e812deb9f66 | 526 | /** Set Temperature as float in °Celsius |
wim | 0:8e812deb9f66 | 527 | * Used for Critical temp threshold, Low and High temp threshold window and Hysteresis |
wim | 0:8e812deb9f66 | 528 | * |
wim | 0:8e812deb9f66 | 529 | * @param reg Index of register to write temp value |
wim | 0:8e812deb9f66 | 530 | * @param temp float Temperature value in °Celsius |
wim | 0:8e812deb9f66 | 531 | */ |
wim | 0:8e812deb9f66 | 532 | void LM77::_writeTempHelper(char reg, float temp) { |
wim | 0:8e812deb9f66 | 533 | //Signed value |
wim | 0:8e812deb9f66 | 534 | int16_t value; |
wim | 0:8e812deb9f66 | 535 | |
wim | 0:8e812deb9f66 | 536 | //Range limit temp |
wim | 0:8e812deb9f66 | 537 | if (temp < -55.0) |
wim | 0:8e812deb9f66 | 538 | temp = -55.0; |
wim | 0:8e812deb9f66 | 539 | else if (temp > 125.0) |
wim | 0:8e812deb9f66 | 540 | temp = 125.0; |
wim | 0:8e812deb9f66 | 541 | |
wim | 0:8e812deb9f66 | 542 | //Extract and shift the signed integer |
wim | 0:8e812deb9f66 | 543 | value = temp * 2.0f; |
wim | 0:8e812deb9f66 | 544 | value <<= 3; |
wim | 0:8e812deb9f66 | 545 | |
wim | 0:8e812deb9f66 | 546 | //Send the new value |
wim | 0:8e812deb9f66 | 547 | _writeReg16(reg, value); |
wim | 0:8e812deb9f66 | 548 | } |