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, &reg, 1); //stop condition - read16() -> m_I2C.write(m_Addr, &reg, 1,1); //repeat condition

Fork of LM75B by Neil Thiessen

Committer:
neilt6
Date:
Thu Nov 07 17:42:41 2013 +0000
Revision:
13:27c19044ace6
Parent:
9:74b44a27fa40
Child:
14:3a44310726fe
Minor improvements

Who changed what in which revision?

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