modify the write function for temperature update If you use update temperature, you modify the repeat condition when write function use - read16() -> m_I2C.write(m_Addr, ®, 1); //stop condition - read16() -> m_I2C.write(m_Addr, ®, 1,1); //repeat condition
Fork of LM75B by
LM75B.cpp@17:c62f1e2d721e, 2015-07-20 (annotated)
- Committer:
- eunkyoungkim
- Date:
- Mon Jul 20 09:36:26 2015 +0000
- Revision:
- 17:c62f1e2d721e
- Parent:
- 9:74b44a27fa40
modify the Source code(in LM75B.cpp); - in LM75B::read16(char reg); write function is modified from stop condition to repeated condition for temperture value update; m_I2C.write(m_Addr, ®, 1) ->m_I2C.write(m_Addr, ®,1,1);;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 2:9ecc39b2ca70 | 1 | /* LM75B Driver Library |
neilt6 | 4:06676376766a | 2 | * Copyright (c) 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 | 4:06676376766a | 7 | * |
neilt6 | 4:06676376766a | 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 | |
eunkyoungkim | 17:c62f1e2d721e | 19 | LM75B::LM75B(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl) |
neilt6 | 0:557a92280097 | 20 | { |
neilt6 | 0:557a92280097 | 21 | //Set the internal device address |
neilt6 | 8:2b797c309258 | 22 | m_Addr = (int)addr; |
eunkyoungkim | 17:c62f1e2d721e | 23 | m_I2C.frequency(hz); |
neilt6 | 0:557a92280097 | 24 | } |
neilt6 | 0:557a92280097 | 25 | |
eunkyoungkim | 17:c62f1e2d721e | 26 | |
neilt6 | 9:74b44a27fa40 | 27 | bool LM75B::open(void) |
neilt6 | 9:74b44a27fa40 | 28 | { |
neilt6 | 9:74b44a27fa40 | 29 | //Probe for the LM75B using a Zero Length Transfer |
neilt6 | 9:74b44a27fa40 | 30 | if (!m_I2C.write(m_Addr, NULL, 0)) { |
neilt6 | 9:74b44a27fa40 | 31 | //Reset the device to default configuration |
neilt6 | 9:74b44a27fa40 | 32 | write8(REG_CONF, 0x00); |
neilt6 | 9:74b44a27fa40 | 33 | write16(REG_THYST, 0x4B00); |
neilt6 | 9:74b44a27fa40 | 34 | write16(REG_TOS, 0x5000); |
neilt6 | 9:74b44a27fa40 | 35 | |
neilt6 | 9:74b44a27fa40 | 36 | //Return success |
neilt6 | 9:74b44a27fa40 | 37 | return true; |
neilt6 | 9:74b44a27fa40 | 38 | } else { |
neilt6 | 9:74b44a27fa40 | 39 | //Return failure |
neilt6 | 9:74b44a27fa40 | 40 | return false; |
neilt6 | 9:74b44a27fa40 | 41 | } |
neilt6 | 9:74b44a27fa40 | 42 | } |
neilt6 | 9:74b44a27fa40 | 43 | |
neilt6 | 8:2b797c309258 | 44 | LM75B::PowerMode LM75B::powerMode(void) |
neilt6 | 0:557a92280097 | 45 | { |
neilt6 | 0:557a92280097 | 46 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 47 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 48 | |
neilt6 | 0:557a92280097 | 49 | //Return the status of the SHUTDOWN bit |
neilt6 | 0:557a92280097 | 50 | if (value & (1 << 0)) |
neilt6 | 2:9ecc39b2ca70 | 51 | return POWER_SHUTDOWN; |
neilt6 | 0:557a92280097 | 52 | else |
neilt6 | 2:9ecc39b2ca70 | 53 | return POWER_NORMAL; |
neilt6 | 0:557a92280097 | 54 | } |
neilt6 | 0:557a92280097 | 55 | |
neilt6 | 8:2b797c309258 | 56 | void LM75B::powerMode(PowerMode mode) |
neilt6 | 0:557a92280097 | 57 | { |
neilt6 | 0:557a92280097 | 58 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 59 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 60 | |
neilt6 | 0:557a92280097 | 61 | //Set or clear the SHUTDOWN bit |
neilt6 | 2:9ecc39b2ca70 | 62 | if (mode == POWER_SHUTDOWN) |
neilt6 | 0:557a92280097 | 63 | value |= (1 << 0); |
neilt6 | 0:557a92280097 | 64 | else |
neilt6 | 0:557a92280097 | 65 | value &= ~(1 << 0); |
neilt6 | 0:557a92280097 | 66 | |
neilt6 | 0:557a92280097 | 67 | //Write the value back out |
neilt6 | 8:2b797c309258 | 68 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 69 | } |
neilt6 | 0:557a92280097 | 70 | |
neilt6 | 8:2b797c309258 | 71 | LM75B::OSMode LM75B::osMode(void) |
neilt6 | 0:557a92280097 | 72 | { |
neilt6 | 0:557a92280097 | 73 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 74 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 75 | |
neilt6 | 0:557a92280097 | 76 | //Return the status of the OS_COMP_INT bit |
neilt6 | 0:557a92280097 | 77 | if (value & (1 << 1)) |
neilt6 | 2:9ecc39b2ca70 | 78 | return OS_INTERRUPT; |
neilt6 | 0:557a92280097 | 79 | else |
neilt6 | 2:9ecc39b2ca70 | 80 | return OS_COMPARATOR; |
neilt6 | 0:557a92280097 | 81 | } |
neilt6 | 0:557a92280097 | 82 | |
neilt6 | 8:2b797c309258 | 83 | void LM75B::osMode(OSMode mode) |
neilt6 | 0:557a92280097 | 84 | { |
neilt6 | 0:557a92280097 | 85 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 86 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 87 | |
neilt6 | 0:557a92280097 | 88 | //Set or clear the OS_COMP_INT bit |
neilt6 | 2:9ecc39b2ca70 | 89 | if (mode == OS_INTERRUPT) |
neilt6 | 0:557a92280097 | 90 | value |= (1 << 1); |
neilt6 | 0:557a92280097 | 91 | else |
neilt6 | 0:557a92280097 | 92 | value &= ~(1 << 1); |
neilt6 | 0:557a92280097 | 93 | |
neilt6 | 0:557a92280097 | 94 | //Write the value back out |
neilt6 | 8:2b797c309258 | 95 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 96 | } |
neilt6 | 0:557a92280097 | 97 | |
neilt6 | 8:2b797c309258 | 98 | LM75B::OSPolarity LM75B::osPolarity(void) |
neilt6 | 0:557a92280097 | 99 | { |
neilt6 | 0:557a92280097 | 100 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 101 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 102 | |
neilt6 | 0:557a92280097 | 103 | //Return the status of the OS_POL bit |
neilt6 | 0:557a92280097 | 104 | if (value & (1 << 2)) |
neilt6 | 2:9ecc39b2ca70 | 105 | return OS_ACTIVE_HIGH; |
neilt6 | 0:557a92280097 | 106 | else |
neilt6 | 2:9ecc39b2ca70 | 107 | return OS_ACTIVE_LOW; |
neilt6 | 0:557a92280097 | 108 | } |
neilt6 | 0:557a92280097 | 109 | |
neilt6 | 8:2b797c309258 | 110 | void LM75B::osPolarity(OSPolarity polarity) |
neilt6 | 0:557a92280097 | 111 | { |
neilt6 | 0:557a92280097 | 112 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 113 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 114 | |
neilt6 | 0:557a92280097 | 115 | //Set or clear the OS_POL bit |
neilt6 | 2:9ecc39b2ca70 | 116 | if (polarity == OS_ACTIVE_HIGH) |
neilt6 | 0:557a92280097 | 117 | value |= (1 << 2); |
neilt6 | 0:557a92280097 | 118 | else |
neilt6 | 0:557a92280097 | 119 | value &= ~(1 << 2); |
neilt6 | 0:557a92280097 | 120 | |
neilt6 | 0:557a92280097 | 121 | //Write the value back out |
neilt6 | 8:2b797c309258 | 122 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 123 | } |
neilt6 | 0:557a92280097 | 124 | |
neilt6 | 8:2b797c309258 | 125 | LM75B::OSFaultQueue LM75B::osFaultQueue(void) |
neilt6 | 0:557a92280097 | 126 | { |
neilt6 | 0:557a92280097 | 127 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 128 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 129 | |
neilt6 | 0:557a92280097 | 130 | //Return the status of the OS_F_QUE bits |
neilt6 | 0:557a92280097 | 131 | if ((value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 132 | return OS_FAULT_QUEUE_6; |
neilt6 | 0:557a92280097 | 133 | else if (!(value & (1 << 3)) && (value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 134 | return OS_FAULT_QUEUE_4; |
neilt6 | 0:557a92280097 | 135 | else if ((value & (1 << 3)) && !(value & (1 << 4))) |
neilt6 | 2:9ecc39b2ca70 | 136 | return OS_FAULT_QUEUE_2; |
neilt6 | 0:557a92280097 | 137 | else |
neilt6 | 2:9ecc39b2ca70 | 138 | return OS_FAULT_QUEUE_1; |
neilt6 | 0:557a92280097 | 139 | } |
neilt6 | 0:557a92280097 | 140 | |
neilt6 | 8:2b797c309258 | 141 | void LM75B::osFaultQueue(OSFaultQueue queue) |
neilt6 | 0:557a92280097 | 142 | { |
neilt6 | 0:557a92280097 | 143 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 144 | char value = read8(REG_CONF); |
neilt6 | 0:557a92280097 | 145 | |
neilt6 | 0:557a92280097 | 146 | //Clear the old OS_F_QUE bits |
neilt6 | 0:557a92280097 | 147 | value &= ~(3 << 3); |
neilt6 | 0:557a92280097 | 148 | |
neilt6 | 0:557a92280097 | 149 | //Set the new OS_F_QUE bits |
neilt6 | 2:9ecc39b2ca70 | 150 | if (queue == OS_FAULT_QUEUE_2) |
neilt6 | 0:557a92280097 | 151 | value |= (1 << 3); |
neilt6 | 2:9ecc39b2ca70 | 152 | else if (queue == OS_FAULT_QUEUE_4) |
neilt6 | 0:557a92280097 | 153 | value |= (2 << 3); |
neilt6 | 2:9ecc39b2ca70 | 154 | else if (queue == OS_FAULT_QUEUE_6) |
neilt6 | 0:557a92280097 | 155 | value |= (3 << 3); |
neilt6 | 0:557a92280097 | 156 | |
neilt6 | 0:557a92280097 | 157 | //Write the value back out |
neilt6 | 8:2b797c309258 | 158 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 159 | } |
neilt6 | 0:557a92280097 | 160 | |
neilt6 | 8:2b797c309258 | 161 | float LM75B::alertTemp(void) |
neilt6 | 0:557a92280097 | 162 | { |
neilt6 | 0:557a92280097 | 163 | //Use the 9-bit helper to read the TOS register |
neilt6 | 8:2b797c309258 | 164 | return readAlertTempHelper(REG_TOS); |
neilt6 | 0:557a92280097 | 165 | } |
neilt6 | 0:557a92280097 | 166 | |
neilt6 | 8:2b797c309258 | 167 | void LM75B::alertTemp(float temp) |
neilt6 | 0:557a92280097 | 168 | { |
neilt6 | 0:557a92280097 | 169 | //Use the 9-bit helper to write to the TOS register |
neilt6 | 8:2b797c309258 | 170 | return writeAlertTempHelper(REG_TOS, temp); |
neilt6 | 0:557a92280097 | 171 | } |
neilt6 | 0:557a92280097 | 172 | |
neilt6 | 8:2b797c309258 | 173 | float LM75B::alertHyst(void) |
neilt6 | 0:557a92280097 | 174 | { |
neilt6 | 0:557a92280097 | 175 | //Use the 9-bit helper to read the THYST register |
neilt6 | 8:2b797c309258 | 176 | return readAlertTempHelper(REG_THYST); |
neilt6 | 0:557a92280097 | 177 | } |
neilt6 | 0:557a92280097 | 178 | |
neilt6 | 8:2b797c309258 | 179 | void LM75B::alertHyst(float temp) |
neilt6 | 0:557a92280097 | 180 | { |
neilt6 | 0:557a92280097 | 181 | //Use the 9-bit helper to write to the THYST register |
neilt6 | 8:2b797c309258 | 182 | return writeAlertTempHelper(REG_THYST, temp); |
neilt6 | 0:557a92280097 | 183 | } |
neilt6 | 0:557a92280097 | 184 | |
neilt6 | 8:2b797c309258 | 185 | float LM75B::temp(void) |
neilt6 | 0:557a92280097 | 186 | { |
neilt6 | 0:557a92280097 | 187 | //Signed return value |
neilt6 | 0:557a92280097 | 188 | short value; |
neilt6 | 0:557a92280097 | 189 | |
neilt6 | 0:557a92280097 | 190 | //Read the 11-bit raw temperature value |
neilt6 | 8:2b797c309258 | 191 | value = read16(REG_TEMP) >> 5; |
neilt6 | 0:557a92280097 | 192 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 193 | if (value & (1 << 10)) |
neilt6 | 0:557a92280097 | 194 | value |= 0xFC00; |
neilt6 | 0:557a92280097 | 195 | |
neilt6 | 0:557a92280097 | 196 | //Return the temperature in °C |
eunkyoungkim | 17:c62f1e2d721e | 197 | return value*0.125; |
neilt6 | 0:557a92280097 | 198 | } |
neilt6 | 0:557a92280097 | 199 | |
neilt6 | 8:2b797c309258 | 200 | char LM75B::read8(char reg) |
neilt6 | 0:557a92280097 | 201 | { |
neilt6 | 0:557a92280097 | 202 | //Select the register |
neilt6 | 8:2b797c309258 | 203 | m_I2C.write(m_Addr, ®, 1); |
neilt6 | 0:557a92280097 | 204 | |
neilt6 | 0:557a92280097 | 205 | //Read the 8-bit register |
neilt6 | 8:2b797c309258 | 206 | m_I2C.read(m_Addr, ®, 1); |
neilt6 | 0:557a92280097 | 207 | |
neilt6 | 0:557a92280097 | 208 | //Return the byte |
neilt6 | 0:557a92280097 | 209 | return reg; |
neilt6 | 0:557a92280097 | 210 | } |
neilt6 | 0:557a92280097 | 211 | |
neilt6 | 8:2b797c309258 | 212 | void LM75B::write8(char reg, char data) |
neilt6 | 0:557a92280097 | 213 | { |
neilt6 | 0:557a92280097 | 214 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 215 | char buff[2]; |
neilt6 | 0:557a92280097 | 216 | |
neilt6 | 0:557a92280097 | 217 | //Load the register address and 8-bit data |
neilt6 | 0:557a92280097 | 218 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 219 | buff[1] = data; |
neilt6 | 0:557a92280097 | 220 | |
neilt6 | 0:557a92280097 | 221 | //Write the data |
neilt6 | 8:2b797c309258 | 222 | m_I2C.write(m_Addr, buff, 2); |
neilt6 | 0:557a92280097 | 223 | } |
neilt6 | 0:557a92280097 | 224 | |
neilt6 | 8:2b797c309258 | 225 | unsigned short LM75B::read16(char reg) |
neilt6 | 0:557a92280097 | 226 | { |
neilt6 | 0:557a92280097 | 227 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 228 | char buff[2]; |
neilt6 | 0:557a92280097 | 229 | |
neilt6 | 0:557a92280097 | 230 | //Select the register |
eunkyoungkim | 17:c62f1e2d721e | 231 | m_I2C.write(m_Addr, ®, 1,1); |
neilt6 | 0:557a92280097 | 232 | |
neilt6 | 0:557a92280097 | 233 | //Read the 16-bit register |
neilt6 | 8:2b797c309258 | 234 | m_I2C.read(m_Addr, buff, 2); |
neilt6 | 0:557a92280097 | 235 | |
neilt6 | 0:557a92280097 | 236 | //Return the combined 16-bit value |
neilt6 | 0:557a92280097 | 237 | return (buff[0] << 8) | buff[1]; |
neilt6 | 0:557a92280097 | 238 | } |
neilt6 | 0:557a92280097 | 239 | |
neilt6 | 8:2b797c309258 | 240 | void LM75B::write16(char reg, unsigned short data) |
neilt6 | 0:557a92280097 | 241 | { |
neilt6 | 0:557a92280097 | 242 | //Create a temporary buffer |
neilt6 | 0:557a92280097 | 243 | char buff[3]; |
neilt6 | 0:557a92280097 | 244 | |
neilt6 | 0:557a92280097 | 245 | //Load the register address and 16-bit data |
neilt6 | 0:557a92280097 | 246 | buff[0] = reg; |
neilt6 | 0:557a92280097 | 247 | buff[1] = data >> 8; |
neilt6 | 0:557a92280097 | 248 | buff[2] = data; |
neilt6 | 0:557a92280097 | 249 | |
neilt6 | 0:557a92280097 | 250 | //Write the data |
neilt6 | 8:2b797c309258 | 251 | m_I2C.write(m_Addr, buff, 3); |
neilt6 | 0:557a92280097 | 252 | } |
neilt6 | 0:557a92280097 | 253 | |
neilt6 | 8:2b797c309258 | 254 | float LM75B::readAlertTempHelper(char reg) |
neilt6 | 0:557a92280097 | 255 | { |
neilt6 | 0:557a92280097 | 256 | //Signed return value |
neilt6 | 0:557a92280097 | 257 | short value; |
neilt6 | 0:557a92280097 | 258 | |
neilt6 | 0:557a92280097 | 259 | //Read the 9-bit raw temperature value |
neilt6 | 8:2b797c309258 | 260 | value = read16(reg) >> 7; |
neilt6 | 0:557a92280097 | 261 | |
neilt6 | 0:557a92280097 | 262 | //Sign extend negative numbers |
neilt6 | 0:557a92280097 | 263 | if (value & (1 << 8)) |
neilt6 | 0:557a92280097 | 264 | value |= 0xFF00; |
neilt6 | 0:557a92280097 | 265 | |
neilt6 | 0:557a92280097 | 266 | //Return the temperature in °C |
neilt6 | 0:557a92280097 | 267 | return value * 0.5; |
neilt6 | 0:557a92280097 | 268 | } |
neilt6 | 0:557a92280097 | 269 | |
neilt6 | 8:2b797c309258 | 270 | void LM75B::writeAlertTempHelper(char reg, float temp) |
neilt6 | 0:557a92280097 | 271 | { |
neilt6 | 0:557a92280097 | 272 | //Range limit temp |
neilt6 | 0:557a92280097 | 273 | if (temp < -55.0) |
neilt6 | 0:557a92280097 | 274 | temp = -55.0; |
neilt6 | 0:557a92280097 | 275 | else if (temp > 125.0) |
neilt6 | 0:557a92280097 | 276 | temp = 125.0; |
neilt6 | 0:557a92280097 | 277 | |
neilt6 | 0:557a92280097 | 278 | //Extract and shift the signed integer |
neilt6 | 0:557a92280097 | 279 | short value = temp * 2; |
neilt6 | 0:557a92280097 | 280 | value <<= 7; |
neilt6 | 0:557a92280097 | 281 | |
neilt6 | 0:557a92280097 | 282 | //Send the new value |
neilt6 | 8:2b797c309258 | 283 | write16(reg, value); |
neilt6 | 0:557a92280097 | 284 | } |