A feature complete driver for the LM75B temperature sensor from NXP.
Fork of LM75B by
LM75B.cpp@0:557a92280097, 2013-07-31 (annotated)
- Committer:
- neilt6
- Date:
- Wed Jul 31 22:04:57 2013 +0000
- Revision:
- 0:557a92280097
- Child:
- 1:3da8df4319e8
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:557a92280097 | 1 | /* mbed LM75B Library |
neilt6 | 0:557a92280097 | 2 | * Copyright (c) 2013 neilt6 |
neilt6 | 0:557a92280097 | 3 | * |
neilt6 | 0:557a92280097 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
neilt6 | 0:557a92280097 | 5 | * of this software and associated documentation files (the "Software"), to deal |
neilt6 | 0:557a92280097 | 6 | * in the Software without restriction, including without limitation the rights |
neilt6 | 0:557a92280097 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
neilt6 | 0:557a92280097 | 8 | * copies of the Software, and to permit persons to whom the Software is |
neilt6 | 0:557a92280097 | 9 | * furnished to do so, subject to the following conditions: |
neilt6 | 0:557a92280097 | 10 | * |
neilt6 | 0:557a92280097 | 11 | * The above copyright notice and this permission notice shall be included in |
neilt6 | 0:557a92280097 | 12 | * all copies or substantial portions of the Software. |
neilt6 | 0:557a92280097 | 13 | * |
neilt6 | 0:557a92280097 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
neilt6 | 0:557a92280097 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
neilt6 | 0:557a92280097 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
neilt6 | 0:557a92280097 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
neilt6 | 0:557a92280097 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
neilt6 | 0:557a92280097 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
neilt6 | 0:557a92280097 | 20 | * THE SOFTWARE. |
neilt6 | 0:557a92280097 | 21 | */ |
neilt6 | 0:557a92280097 | 22 | |
neilt6 | 0:557a92280097 | 23 | #include "LM75B.h" |
neilt6 | 0:557a92280097 | 24 | #include "mbed.h" |
neilt6 | 0:557a92280097 | 25 | |
neilt6 | 0:557a92280097 | 26 | LM75B::LM75B(PinName sda, PinName scl, int addr) : _i2c(sda, scl) |
neilt6 | 0:557a92280097 | 27 | { |
neilt6 | 0:557a92280097 | 28 | //Set the internal device address |
neilt6 | 0:557a92280097 | 29 | _addr = addr; |
neilt6 | 0:557a92280097 | 30 | } |
neilt6 | 0:557a92280097 | 31 | |
neilt6 | 0:557a92280097 | 32 | LM75B_Power_t LM75B::getPowerMode(void) |
neilt6 | 0:557a92280097 | 33 | { |
neilt6 | 0:557a92280097 | 34 | //Read the 8-bit register value |
neilt6 | 0:557a92280097 | 35 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 36 | |
neilt6 | 0:557a92280097 | 37 | //Return the status of the SHUTDOWN bit |
neilt6 | 0:557a92280097 | 38 | if (value & (1 << 0)) |
neilt6 | 0:557a92280097 | 39 | return LM75B_Power_Shutdown; |
neilt6 | 0:557a92280097 | 40 | else |
neilt6 | 0:557a92280097 | 41 | return LM75B_Power_Normal; |
neilt6 | 0:557a92280097 | 42 | } |
neilt6 | 0:557a92280097 | 43 | |
neilt6 | 0:557a92280097 | 44 | void LM75B::setPowerMode(LM75B_Power_t mode) |
neilt6 | 0:557a92280097 | 45 | { |
neilt6 | 0:557a92280097 | 46 | //Read the current 8-bit register value |
neilt6 | 0:557a92280097 | 47 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 48 | |
neilt6 | 0:557a92280097 | 49 | //Set or clear the SHUTDOWN bit |
neilt6 | 0:557a92280097 | 50 | if (mode == LM75B_Power_Shutdown) |
neilt6 | 0:557a92280097 | 51 | value |= (1 << 0); |
neilt6 | 0:557a92280097 | 52 | else |
neilt6 | 0:557a92280097 | 53 | value &= ~(1 << 0); |
neilt6 | 0:557a92280097 | 54 | |
neilt6 | 0:557a92280097 | 55 | //Write the value back out |
neilt6 | 0:557a92280097 | 56 | _write8(LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 57 | } |
neilt6 | 0:557a92280097 | 58 | |
neilt6 | 0:557a92280097 | 59 | LM75B_OS_Mode_t LM75B::getOSMode(void) |
neilt6 | 0:557a92280097 | 60 | { |
neilt6 | 0:557a92280097 | 61 | //Read the 8-bit register value |
neilt6 | 0:557a92280097 | 62 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 63 | |
neilt6 | 0:557a92280097 | 64 | //Return the status of the OS_COMP_INT bit |
neilt6 | 0:557a92280097 | 65 | if (value & (1 << 1)) |
neilt6 | 0:557a92280097 | 66 | return LM75B_OS_Interrupt; |
neilt6 | 0:557a92280097 | 67 | else |
neilt6 | 0:557a92280097 | 68 | return LM75B_OS_Comparator; |
neilt6 | 0:557a92280097 | 69 | } |
neilt6 | 0:557a92280097 | 70 | |
neilt6 | 0:557a92280097 | 71 | void LM75B::setOSMode(LM75B_OS_Mode_t mode) |
neilt6 | 0:557a92280097 | 72 | { |
neilt6 | 0:557a92280097 | 73 | //Read the current 8-bit register value |
neilt6 | 0:557a92280097 | 74 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 75 | |
neilt6 | 0:557a92280097 | 76 | //Set or clear the OS_COMP_INT bit |
neilt6 | 0:557a92280097 | 77 | if (mode == LM75B_OS_Interrupt) |
neilt6 | 0:557a92280097 | 78 | value |= (1 << 1); |
neilt6 | 0:557a92280097 | 79 | else |
neilt6 | 0:557a92280097 | 80 | value &= ~(1 << 1); |
neilt6 | 0:557a92280097 | 81 | |
neilt6 | 0:557a92280097 | 82 | //Write the value back out |
neilt6 | 0:557a92280097 | 83 | _write8(LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 84 | } |
neilt6 | 0:557a92280097 | 85 | |
neilt6 | 0:557a92280097 | 86 | LM75B_OS_Polarity_t LM75B::getOSPolarity(void) |
neilt6 | 0:557a92280097 | 87 | { |
neilt6 | 0:557a92280097 | 88 | //Read the 8-bit register value |
neilt6 | 0:557a92280097 | 89 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 90 | |
neilt6 | 0:557a92280097 | 91 | //Return the status of the OS_POL bit |
neilt6 | 0:557a92280097 | 92 | if (value & (1 << 2)) |
neilt6 | 0:557a92280097 | 93 | return LM75B_OS_ActiveHigh; |
neilt6 | 0:557a92280097 | 94 | else |
neilt6 | 0:557a92280097 | 95 | return LM75B_OS_ActiveLow; |
neilt6 | 0:557a92280097 | 96 | } |
neilt6 | 0:557a92280097 | 97 | |
neilt6 | 0:557a92280097 | 98 | void LM75B::setOSPolarity(LM75B_OS_Polarity_t polarity) |
neilt6 | 0:557a92280097 | 99 | { |
neilt6 | 0:557a92280097 | 100 | //Read the current 8-bit register value |
neilt6 | 0:557a92280097 | 101 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 102 | |
neilt6 | 0:557a92280097 | 103 | //Set or clear the OS_POL bit |
neilt6 | 0:557a92280097 | 104 | if (polarity == LM75B_OS_ActiveHigh) |
neilt6 | 0:557a92280097 | 105 | value |= (1 << 2); |
neilt6 | 0:557a92280097 | 106 | else |
neilt6 | 0:557a92280097 | 107 | value &= ~(1 << 2); |
neilt6 | 0:557a92280097 | 108 | |
neilt6 | 0:557a92280097 | 109 | //Write the value back out |
neilt6 | 0:557a92280097 | 110 | _write8(LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 111 | } |
neilt6 | 0:557a92280097 | 112 | |
neilt6 | 0:557a92280097 | 113 | LM75B_OS_FaultQueue_t LM75B::getOSFaultQueue(void) |
neilt6 | 0:557a92280097 | 114 | { |
neilt6 | 0:557a92280097 | 115 | //Read the 8-bit register value |
neilt6 | 0:557a92280097 | 116 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 117 | |
neilt6 | 0:557a92280097 | 118 | //Return the status of the OS_F_QUE bits |
neilt6 | 0:557a92280097 | 119 | if ((value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 0:557a92280097 | 120 | return LM75B_OS_FaultQueue_6; |
neilt6 | 0:557a92280097 | 121 | else if (!(value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 0:557a92280097 | 122 | return LM75B_OS_FaultQueue_4; |
neilt6 | 0:557a92280097 | 123 | else if ((value & (1 << 3)) && !(value & (1 << 4))) |
neilt6 | 0:557a92280097 | 124 | return LM75B_OS_FaultQueue_2; |
neilt6 | 0:557a92280097 | 125 | else |
neilt6 | 0:557a92280097 | 126 | return LM75B_OS_FaultQueue_1; |
neilt6 | 0:557a92280097 | 127 | } |
neilt6 | 0:557a92280097 | 128 | |
neilt6 | 0:557a92280097 | 129 | void LM75B::setOSFaultQueue(LM75B_OS_FaultQueue_t queue) |
neilt6 | 0:557a92280097 | 130 | { |
neilt6 | 0:557a92280097 | 131 | //Read the current 8-bit register value |
neilt6 | 0:557a92280097 | 132 | char value = _read8(LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 133 | |
neilt6 | 0:557a92280097 | 134 | //Clear the old OS_F_QUE bits |
neilt6 | 0:557a92280097 | 135 | value &= ~(3 << 3); |
neilt6 | 0:557a92280097 | 136 | |
neilt6 | 0:557a92280097 | 137 | //Set the new OS_F_QUE bits |
neilt6 | 0:557a92280097 | 138 | if (queue == LM75B_OS_FaultQueue_2) |
neilt6 | 0:557a92280097 | 139 | value |= (1 << 3); |
neilt6 | 0:557a92280097 | 140 | else if (queue == LM75B_OS_FaultQueue_4) |
neilt6 | 0:557a92280097 | 141 | value |= (2 << 3); |
neilt6 | 0:557a92280097 | 142 | else if (queue == LM75B_OS_FaultQueue_6) |
neilt6 | 0:557a92280097 | 143 | value |= (3 << 3); |
neilt6 | 0:557a92280097 | 144 | |
neilt6 | 0:557a92280097 | 145 | //Write the value back out |
neilt6 | 0:557a92280097 | 146 | _write8(LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 147 | } |
neilt6 | 0:557a92280097 | 148 | |
neilt6 | 0:557a92280097 | 149 | float LM75B::getAlertTemp(void) |
neilt6 | 0:557a92280097 | 150 | { |
neilt6 | 0:557a92280097 | 151 | //Use the 9-bit helper to read the TOS register |
neilt6 | 0:557a92280097 | 152 | return _readTempHelper(LM75B_REG_TOS); |
neilt6 | 0:557a92280097 | 153 | } |
neilt6 | 0:557a92280097 | 154 | |
neilt6 | 0:557a92280097 | 155 | void LM75B::setAlertTemp(float temp) |
neilt6 | 0:557a92280097 | 156 | { |
neilt6 | 0:557a92280097 | 157 | //Use the 9-bit helper to write to the TOS register |
neilt6 | 0:557a92280097 | 158 | return _writeTempHelper(LM75B_REG_TOS, temp); |
neilt6 | 0:557a92280097 | 159 | } |
neilt6 | 0:557a92280097 | 160 | |
neilt6 | 0:557a92280097 | 161 | float LM75B::getAlertHyst(void) |
neilt6 | 0:557a92280097 | 162 | { |
neilt6 | 0:557a92280097 | 163 | //Use the 9-bit helper to read the THYST register |
neilt6 | 0:557a92280097 | 164 | return _readTempHelper(LM75B_REG_THYST); |
neilt6 | 0:557a92280097 | 165 | } |
neilt6 | 0:557a92280097 | 166 | |
neilt6 | 0:557a92280097 | 167 | void LM75B::setAlertHyst(float temp) |
neilt6 | 0:557a92280097 | 168 | { |
neilt6 | 0:557a92280097 | 169 | //Use the 9-bit helper to write to the THYST register |
neilt6 | 0:557a92280097 | 170 | return _writeTempHelper(LM75B_REG_THYST, temp); |
neilt6 | 0:557a92280097 | 171 | } |
neilt6 | 0:557a92280097 | 172 | |
neilt6 | 0:557a92280097 | 173 | float LM75B::getTemp(void) |
neilt6 | 0:557a92280097 | 174 | { |
neilt6 | 0:557a92280097 | 175 | //Signed return value |
neilt6 | 0:557a92280097 | 176 | short value; |
neilt6 | 0:557a92280097 | 177 | |
neilt6 | 0:557a92280097 | 178 | //Read the 11-bit raw temperature value |
neilt6 | 0:557a92280097 | 179 | value = _read16(LM75B_REG_TEMP) >> 5; |
neilt6 | 0:557a92280097 | 180 | |
neilt6 | 0:557a92280097 | 181 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 182 | if (value & (1 << 10)) |
neilt6 | 0:557a92280097 | 183 | value |= 0xFC00; |
neilt6 | 0:557a92280097 | 184 | |
neilt6 | 0:557a92280097 | 185 | //Return the temperature in °C |
neilt6 | 0:557a92280097 | 186 | return value * 0.125; |
neilt6 | 0:557a92280097 | 187 | } |
neilt6 | 0:557a92280097 | 188 | |
neilt6 | 0:557a92280097 | 189 | char LM75B::_read8(char reg) |
neilt6 | 0:557a92280097 | 190 | { |
neilt6 | 0:557a92280097 | 191 | //Select the register |
neilt6 | 0:557a92280097 | 192 | _i2c.write(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 193 | |
neilt6 | 0:557a92280097 | 194 | //Read the 8-bit register |
neilt6 | 0:557a92280097 | 195 | _i2c.read(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 196 | |
neilt6 | 0:557a92280097 | 197 | //Return the byte |
neilt6 | 0:557a92280097 | 198 | return reg; |
neilt6 | 0:557a92280097 | 199 | } |
neilt6 | 0:557a92280097 | 200 | |
neilt6 | 0:557a92280097 | 201 | void LM75B::_write8(char reg, char data) |
neilt6 | 0:557a92280097 | 202 | { |
neilt6 | 0:557a92280097 | 203 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 204 | char buff[2]; |
neilt6 | 0:557a92280097 | 205 | |
neilt6 | 0:557a92280097 | 206 | //Load the register address and 8-bit data |
neilt6 | 0:557a92280097 | 207 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 208 | buff[1] = data; |
neilt6 | 0:557a92280097 | 209 | |
neilt6 | 0:557a92280097 | 210 | //Write the data |
neilt6 | 0:557a92280097 | 211 | _i2c.write(_addr, buff, 2); |
neilt6 | 0:557a92280097 | 212 | } |
neilt6 | 0:557a92280097 | 213 | |
neilt6 | 0:557a92280097 | 214 | unsigned short LM75B::_read16(char reg) |
neilt6 | 0:557a92280097 | 215 | { |
neilt6 | 0:557a92280097 | 216 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 217 | char buff[2]; |
neilt6 | 0:557a92280097 | 218 | |
neilt6 | 0:557a92280097 | 219 | //Select the register |
neilt6 | 0:557a92280097 | 220 | _i2c.write(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 221 | |
neilt6 | 0:557a92280097 | 222 | //Read the 16-bit register |
neilt6 | 0:557a92280097 | 223 | _i2c.read(_addr, buff, 2); |
neilt6 | 0:557a92280097 | 224 | |
neilt6 | 0:557a92280097 | 225 | //Return the combined 16-bit value |
neilt6 | 0:557a92280097 | 226 | return (buff[0] << 8) | buff[1]; |
neilt6 | 0:557a92280097 | 227 | } |
neilt6 | 0:557a92280097 | 228 | |
neilt6 | 0:557a92280097 | 229 | void LM75B::_write16(char reg, unsigned short data) |
neilt6 | 0:557a92280097 | 230 | { |
neilt6 | 0:557a92280097 | 231 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 232 | char buff[3]; |
neilt6 | 0:557a92280097 | 233 | |
neilt6 | 0:557a92280097 | 234 | //Load the register address and 16-bit data |
neilt6 | 0:557a92280097 | 235 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 236 | buff[1] = data >> 8; |
neilt6 | 0:557a92280097 | 237 | buff[2] = data; |
neilt6 | 0:557a92280097 | 238 | |
neilt6 | 0:557a92280097 | 239 | //Write the data |
neilt6 | 0:557a92280097 | 240 | _i2c.write(_addr, buff, 3); |
neilt6 | 0:557a92280097 | 241 | } |
neilt6 | 0:557a92280097 | 242 | |
neilt6 | 0:557a92280097 | 243 | float LM75B::_readTempHelper(char reg) |
neilt6 | 0:557a92280097 | 244 | { |
neilt6 | 0:557a92280097 | 245 | //Signed return value |
neilt6 | 0:557a92280097 | 246 | short value; |
neilt6 | 0:557a92280097 | 247 | |
neilt6 | 0:557a92280097 | 248 | //Read the 9-bit raw temperature value |
neilt6 | 0:557a92280097 | 249 | value = _read16(reg) >> 7; |
neilt6 | 0:557a92280097 | 250 | |
neilt6 | 0:557a92280097 | 251 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 252 | if (value & (1 << 8)) |
neilt6 | 0:557a92280097 | 253 | value |= 0xFF00; |
neilt6 | 0:557a92280097 | 254 | |
neilt6 | 0:557a92280097 | 255 | //Return the temperature in °C |
neilt6 | 0:557a92280097 | 256 | return value * 0.5; |
neilt6 | 0:557a92280097 | 257 | } |
neilt6 | 0:557a92280097 | 258 | |
neilt6 | 0:557a92280097 | 259 | void LM75B::_writeTempHelper(char reg, float temp) |
neilt6 | 0:557a92280097 | 260 | { |
neilt6 | 0:557a92280097 | 261 | //Range limit temp |
neilt6 | 0:557a92280097 | 262 | if (temp < -55.0) |
neilt6 | 0:557a92280097 | 263 | temp = -55.0; |
neilt6 | 0:557a92280097 | 264 | else if (temp > 125.0) |
neilt6 | 0:557a92280097 | 265 | temp = 125.0; |
neilt6 | 0:557a92280097 | 266 | |
neilt6 | 0:557a92280097 | 267 | //Extract and shift the signed integer |
neilt6 | 0:557a92280097 | 268 | short value = temp * 2; |
neilt6 | 0:557a92280097 | 269 | value <<= 7; |
neilt6 | 0:557a92280097 | 270 | |
neilt6 | 0:557a92280097 | 271 | //Send the new value |
neilt6 | 0:557a92280097 | 272 | _write16(reg, value); |
neilt6 | 0:557a92280097 | 273 | } |