A feature complete driver for the LM75B temperature sensor from NXP.
Fork of LM75B by
LM75B.cpp@3:9d68eed28bfb, 2013-08-02 (annotated)
- Committer:
- neilt6
- Date:
- Fri Aug 02 18:26:48 2013 +0000
- Revision:
- 3:9d68eed28bfb
- Parent:
- 2:9ecc39b2ca70
- Child:
- 4:06676376766a
Changed license to Apache 2.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 2:9ecc39b2ca70 | 1 | /* LM75B Driver Library |
neilt6 | 3:9d68eed28bfb | 2 | * Copyright 2013 Neil Thiessen |
neilt6 | 0:557a92280097 | 3 | * |
neilt6 | 3:9d68eed28bfb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 3:9d68eed28bfb | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 3:9d68eed28bfb | 6 | * You may obtain a copy of the License at |
neilt6 | 3:9d68eed28bfb | 7 | * |
neilt6 | 3:9d68eed28bfb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:557a92280097 | 9 | * |
neilt6 | 3:9d68eed28bfb | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 3:9d68eed28bfb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 3:9d68eed28bfb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 3:9d68eed28bfb | 13 | * See the License for the specific language governing permissions and |
neilt6 | 3:9d68eed28bfb | 14 | * limitations under the License. |
neilt6 | 0:557a92280097 | 15 | */ |
neilt6 | 0:557a92280097 | 16 | |
neilt6 | 0:557a92280097 | 17 | #include "LM75B.h" |
neilt6 | 0:557a92280097 | 18 | #include "mbed.h" |
neilt6 | 0:557a92280097 | 19 | |
neilt6 | 2:9ecc39b2ca70 | 20 | LM75B::LM75B(PinName sda, PinName scl, Address addr) : _i2c(sda, scl) |
neilt6 | 0:557a92280097 | 21 | { |
neilt6 | 0:557a92280097 | 22 | //Set the internal device address |
neilt6 | 2:9ecc39b2ca70 | 23 | _addr = (int)addr; |
neilt6 | 0:557a92280097 | 24 | } |
neilt6 | 0:557a92280097 | 25 | |
neilt6 | 2:9ecc39b2ca70 | 26 | LM75B::PowerMode LM75B::getPowerMode(void) |
neilt6 | 0:557a92280097 | 27 | { |
neilt6 | 0:557a92280097 | 28 | //Read the 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 29 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 30 | |
neilt6 | 0:557a92280097 | 31 | //Return the status of the SHUTDOWN bit |
neilt6 | 0:557a92280097 | 32 | if (value & (1 << 0)) |
neilt6 | 2:9ecc39b2ca70 | 33 | return POWER_SHUTDOWN; |
neilt6 | 0:557a92280097 | 34 | else |
neilt6 | 2:9ecc39b2ca70 | 35 | return POWER_NORMAL; |
neilt6 | 0:557a92280097 | 36 | } |
neilt6 | 0:557a92280097 | 37 | |
neilt6 | 2:9ecc39b2ca70 | 38 | void LM75B::setPowerMode(PowerMode mode) |
neilt6 | 0:557a92280097 | 39 | { |
neilt6 | 0:557a92280097 | 40 | //Read the current 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 41 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 42 | |
neilt6 | 0:557a92280097 | 43 | //Set or clear the SHUTDOWN bit |
neilt6 | 2:9ecc39b2ca70 | 44 | if (mode == POWER_SHUTDOWN) |
neilt6 | 0:557a92280097 | 45 | value |= (1 << 0); |
neilt6 | 0:557a92280097 | 46 | else |
neilt6 | 0:557a92280097 | 47 | value &= ~(1 << 0); |
neilt6 | 0:557a92280097 | 48 | |
neilt6 | 0:557a92280097 | 49 | //Write the value back out |
neilt6 | 2:9ecc39b2ca70 | 50 | _write8(__LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 51 | } |
neilt6 | 0:557a92280097 | 52 | |
neilt6 | 2:9ecc39b2ca70 | 53 | LM75B::OSMode LM75B::getOSMode(void) |
neilt6 | 0:557a92280097 | 54 | { |
neilt6 | 0:557a92280097 | 55 | //Read the 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 56 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 57 | |
neilt6 | 0:557a92280097 | 58 | //Return the status of the OS_COMP_INT bit |
neilt6 | 0:557a92280097 | 59 | if (value & (1 << 1)) |
neilt6 | 2:9ecc39b2ca70 | 60 | return OS_INTERRUPT; |
neilt6 | 0:557a92280097 | 61 | else |
neilt6 | 2:9ecc39b2ca70 | 62 | return OS_COMPARATOR; |
neilt6 | 0:557a92280097 | 63 | } |
neilt6 | 0:557a92280097 | 64 | |
neilt6 | 2:9ecc39b2ca70 | 65 | void LM75B::setOSMode(OSMode mode) |
neilt6 | 0:557a92280097 | 66 | { |
neilt6 | 0:557a92280097 | 67 | //Read the current 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 68 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 69 | |
neilt6 | 0:557a92280097 | 70 | //Set or clear the OS_COMP_INT bit |
neilt6 | 2:9ecc39b2ca70 | 71 | if (mode == OS_INTERRUPT) |
neilt6 | 0:557a92280097 | 72 | value |= (1 << 1); |
neilt6 | 0:557a92280097 | 73 | else |
neilt6 | 0:557a92280097 | 74 | value &= ~(1 << 1); |
neilt6 | 0:557a92280097 | 75 | |
neilt6 | 0:557a92280097 | 76 | //Write the value back out |
neilt6 | 2:9ecc39b2ca70 | 77 | _write8(__LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 78 | } |
neilt6 | 0:557a92280097 | 79 | |
neilt6 | 2:9ecc39b2ca70 | 80 | LM75B::OSPolarity LM75B::getOSPolarity(void) |
neilt6 | 0:557a92280097 | 81 | { |
neilt6 | 0:557a92280097 | 82 | //Read the 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 83 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 84 | |
neilt6 | 0:557a92280097 | 85 | //Return the status of the OS_POL bit |
neilt6 | 0:557a92280097 | 86 | if (value & (1 << 2)) |
neilt6 | 2:9ecc39b2ca70 | 87 | return OS_ACTIVE_HIGH; |
neilt6 | 0:557a92280097 | 88 | else |
neilt6 | 2:9ecc39b2ca70 | 89 | return OS_ACTIVE_LOW; |
neilt6 | 0:557a92280097 | 90 | } |
neilt6 | 0:557a92280097 | 91 | |
neilt6 | 2:9ecc39b2ca70 | 92 | void LM75B::setOSPolarity(OSPolarity polarity) |
neilt6 | 0:557a92280097 | 93 | { |
neilt6 | 0:557a92280097 | 94 | //Read the current 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 95 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 96 | |
neilt6 | 0:557a92280097 | 97 | //Set or clear the OS_POL bit |
neilt6 | 2:9ecc39b2ca70 | 98 | if (polarity == OS_ACTIVE_HIGH) |
neilt6 | 0:557a92280097 | 99 | value |= (1 << 2); |
neilt6 | 0:557a92280097 | 100 | else |
neilt6 | 0:557a92280097 | 101 | value &= ~(1 << 2); |
neilt6 | 0:557a92280097 | 102 | |
neilt6 | 0:557a92280097 | 103 | //Write the value back out |
neilt6 | 2:9ecc39b2ca70 | 104 | _write8(__LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 105 | } |
neilt6 | 0:557a92280097 | 106 | |
neilt6 | 2:9ecc39b2ca70 | 107 | LM75B::OSFaultQueue LM75B::getOSFaultQueue(void) |
neilt6 | 0:557a92280097 | 108 | { |
neilt6 | 0:557a92280097 | 109 | //Read the 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 110 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 111 | |
neilt6 | 0:557a92280097 | 112 | //Return the status of the OS_F_QUE bits |
neilt6 | 0:557a92280097 | 113 | if ((value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 114 | return OS_FAULT_QUEUE_6; |
neilt6 | 0:557a92280097 | 115 | else if (!(value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 116 | return OS_FAULT_QUEUE_4; |
neilt6 | 0:557a92280097 | 117 | else if ((value & (1 << 3)) && !(value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 118 | return OS_FAULT_QUEUE_2; |
neilt6 | 0:557a92280097 | 119 | else |
neilt6 | 2:9ecc39b2ca70 | 120 | return OS_FAULT_QUEUE_1; |
neilt6 | 0:557a92280097 | 121 | } |
neilt6 | 0:557a92280097 | 122 | |
neilt6 | 2:9ecc39b2ca70 | 123 | void LM75B::setOSFaultQueue(OSFaultQueue queue) |
neilt6 | 0:557a92280097 | 124 | { |
neilt6 | 0:557a92280097 | 125 | //Read the current 8-bit register value |
neilt6 | 2:9ecc39b2ca70 | 126 | char value = _read8(__LM75B_REG_CONF); |
neilt6 | 0:557a92280097 | 127 | |
neilt6 | 0:557a92280097 | 128 | //Clear the old OS_F_QUE bits |
neilt6 | 0:557a92280097 | 129 | value &= ~(3 << 3); |
neilt6 | 0:557a92280097 | 130 | |
neilt6 | 0:557a92280097 | 131 | //Set the new OS_F_QUE bits |
neilt6 | 2:9ecc39b2ca70 | 132 | if (queue == OS_FAULT_QUEUE_2) |
neilt6 | 0:557a92280097 | 133 | value |= (1 << 3); |
neilt6 | 2:9ecc39b2ca70 | 134 | else if (queue == OS_FAULT_QUEUE_4) |
neilt6 | 0:557a92280097 | 135 | value |= (2 << 3); |
neilt6 | 2:9ecc39b2ca70 | 136 | else if (queue == OS_FAULT_QUEUE_6) |
neilt6 | 0:557a92280097 | 137 | value |= (3 << 3); |
neilt6 | 0:557a92280097 | 138 | |
neilt6 | 0:557a92280097 | 139 | //Write the value back out |
neilt6 | 2:9ecc39b2ca70 | 140 | _write8(__LM75B_REG_CONF, value); |
neilt6 | 0:557a92280097 | 141 | } |
neilt6 | 0:557a92280097 | 142 | |
neilt6 | 0:557a92280097 | 143 | float LM75B::getAlertTemp(void) |
neilt6 | 0:557a92280097 | 144 | { |
neilt6 | 0:557a92280097 | 145 | //Use the 9-bit helper to read the TOS register |
neilt6 | 2:9ecc39b2ca70 | 146 | return _readTempHelper(__LM75B_REG_TOS); |
neilt6 | 0:557a92280097 | 147 | } |
neilt6 | 0:557a92280097 | 148 | |
neilt6 | 0:557a92280097 | 149 | void LM75B::setAlertTemp(float temp) |
neilt6 | 0:557a92280097 | 150 | { |
neilt6 | 0:557a92280097 | 151 | //Use the 9-bit helper to write to the TOS register |
neilt6 | 2:9ecc39b2ca70 | 152 | return _writeTempHelper(__LM75B_REG_TOS, temp); |
neilt6 | 0:557a92280097 | 153 | } |
neilt6 | 0:557a92280097 | 154 | |
neilt6 | 0:557a92280097 | 155 | float LM75B::getAlertHyst(void) |
neilt6 | 0:557a92280097 | 156 | { |
neilt6 | 0:557a92280097 | 157 | //Use the 9-bit helper to read the THYST register |
neilt6 | 2:9ecc39b2ca70 | 158 | return _readTempHelper(__LM75B_REG_THYST); |
neilt6 | 0:557a92280097 | 159 | } |
neilt6 | 0:557a92280097 | 160 | |
neilt6 | 0:557a92280097 | 161 | void LM75B::setAlertHyst(float temp) |
neilt6 | 0:557a92280097 | 162 | { |
neilt6 | 0:557a92280097 | 163 | //Use the 9-bit helper to write to the THYST register |
neilt6 | 2:9ecc39b2ca70 | 164 | return _writeTempHelper(__LM75B_REG_THYST, temp); |
neilt6 | 0:557a92280097 | 165 | } |
neilt6 | 0:557a92280097 | 166 | |
neilt6 | 0:557a92280097 | 167 | float LM75B::getTemp(void) |
neilt6 | 0:557a92280097 | 168 | { |
neilt6 | 0:557a92280097 | 169 | //Signed return value |
neilt6 | 0:557a92280097 | 170 | short value; |
neilt6 | 0:557a92280097 | 171 | |
neilt6 | 0:557a92280097 | 172 | //Read the 11-bit raw temperature value |
neilt6 | 2:9ecc39b2ca70 | 173 | value = _read16(__LM75B_REG_TEMP) >> 5; |
neilt6 | 0:557a92280097 | 174 | |
neilt6 | 0:557a92280097 | 175 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 176 | if (value & (1 << 10)) |
neilt6 | 0:557a92280097 | 177 | value |= 0xFC00; |
neilt6 | 0:557a92280097 | 178 | |
neilt6 | 0:557a92280097 | 179 | //Return the temperature in °C |
neilt6 | 0:557a92280097 | 180 | return value * 0.125; |
neilt6 | 0:557a92280097 | 181 | } |
neilt6 | 0:557a92280097 | 182 | |
neilt6 | 0:557a92280097 | 183 | char LM75B::_read8(char reg) |
neilt6 | 0:557a92280097 | 184 | { |
neilt6 | 0:557a92280097 | 185 | //Select the register |
neilt6 | 0:557a92280097 | 186 | _i2c.write(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 187 | |
neilt6 | 0:557a92280097 | 188 | //Read the 8-bit register |
neilt6 | 0:557a92280097 | 189 | _i2c.read(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 190 | |
neilt6 | 0:557a92280097 | 191 | //Return the byte |
neilt6 | 0:557a92280097 | 192 | return reg; |
neilt6 | 0:557a92280097 | 193 | } |
neilt6 | 0:557a92280097 | 194 | |
neilt6 | 0:557a92280097 | 195 | void LM75B::_write8(char reg, char data) |
neilt6 | 0:557a92280097 | 196 | { |
neilt6 | 0:557a92280097 | 197 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 198 | char buff[2]; |
neilt6 | 0:557a92280097 | 199 | |
neilt6 | 0:557a92280097 | 200 | //Load the register address and 8-bit data |
neilt6 | 0:557a92280097 | 201 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 202 | buff[1] = data; |
neilt6 | 0:557a92280097 | 203 | |
neilt6 | 0:557a92280097 | 204 | //Write the data |
neilt6 | 0:557a92280097 | 205 | _i2c.write(_addr, buff, 2); |
neilt6 | 0:557a92280097 | 206 | } |
neilt6 | 0:557a92280097 | 207 | |
neilt6 | 0:557a92280097 | 208 | unsigned short LM75B::_read16(char reg) |
neilt6 | 0:557a92280097 | 209 | { |
neilt6 | 0:557a92280097 | 210 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 211 | char buff[2]; |
neilt6 | 0:557a92280097 | 212 | |
neilt6 | 0:557a92280097 | 213 | //Select the register |
neilt6 | 0:557a92280097 | 214 | _i2c.write(_addr, ®, 1); |
neilt6 | 0:557a92280097 | 215 | |
neilt6 | 0:557a92280097 | 216 | //Read the 16-bit register |
neilt6 | 0:557a92280097 | 217 | _i2c.read(_addr, buff, 2); |
neilt6 | 0:557a92280097 | 218 | |
neilt6 | 0:557a92280097 | 219 | //Return the combined 16-bit value |
neilt6 | 0:557a92280097 | 220 | return (buff[0] << 8) | buff[1]; |
neilt6 | 0:557a92280097 | 221 | } |
neilt6 | 0:557a92280097 | 222 | |
neilt6 | 0:557a92280097 | 223 | void LM75B::_write16(char reg, unsigned short data) |
neilt6 | 0:557a92280097 | 224 | { |
neilt6 | 0:557a92280097 | 225 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 226 | char buff[3]; |
neilt6 | 0:557a92280097 | 227 | |
neilt6 | 0:557a92280097 | 228 | //Load the register address and 16-bit data |
neilt6 | 0:557a92280097 | 229 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 230 | buff[1] = data >> 8; |
neilt6 | 0:557a92280097 | 231 | buff[2] = data; |
neilt6 | 0:557a92280097 | 232 | |
neilt6 | 0:557a92280097 | 233 | //Write the data |
neilt6 | 0:557a92280097 | 234 | _i2c.write(_addr, buff, 3); |
neilt6 | 0:557a92280097 | 235 | } |
neilt6 | 0:557a92280097 | 236 | |
neilt6 | 0:557a92280097 | 237 | float LM75B::_readTempHelper(char reg) |
neilt6 | 0:557a92280097 | 238 | { |
neilt6 | 0:557a92280097 | 239 | //Signed return value |
neilt6 | 0:557a92280097 | 240 | short value; |
neilt6 | 0:557a92280097 | 241 | |
neilt6 | 0:557a92280097 | 242 | //Read the 9-bit raw temperature value |
neilt6 | 0:557a92280097 | 243 | value = _read16(reg) >> 7; |
neilt6 | 0:557a92280097 | 244 | |
neilt6 | 0:557a92280097 | 245 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 246 | if (value & (1 << 8)) |
neilt6 | 0:557a92280097 | 247 | value |= 0xFF00; |
neilt6 | 0:557a92280097 | 248 | |
neilt6 | 0:557a92280097 | 249 | //Return the temperature in °C |
neilt6 | 0:557a92280097 | 250 | return value * 0.5; |
neilt6 | 0:557a92280097 | 251 | } |
neilt6 | 0:557a92280097 | 252 | |
neilt6 | 0:557a92280097 | 253 | void LM75B::_writeTempHelper(char reg, float temp) |
neilt6 | 0:557a92280097 | 254 | { |
neilt6 | 0:557a92280097 | 255 | //Range limit temp |
neilt6 | 0:557a92280097 | 256 | if (temp < -55.0) |
neilt6 | 0:557a92280097 | 257 | temp = -55.0; |
neilt6 | 0:557a92280097 | 258 | else if (temp > 125.0) |
neilt6 | 0:557a92280097 | 259 | temp = 125.0; |
neilt6 | 0:557a92280097 | 260 | |
neilt6 | 0:557a92280097 | 261 | //Extract and shift the signed integer |
neilt6 | 0:557a92280097 | 262 | short value = temp * 2; |
neilt6 | 0:557a92280097 | 263 | value <<= 7; |
neilt6 | 0:557a92280097 | 264 | |
neilt6 | 0:557a92280097 | 265 | //Send the new value |
neilt6 | 0:557a92280097 | 266 | _write16(reg, value); |
neilt6 | 0:557a92280097 | 267 | } |