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