lel

Dependents:   Project2

Fork of LM75B by Neil Thiessen

Committer:
gimohd
Date:
Thu Mar 23 12:51:11 2017 +0000
Revision:
17:a0b87cc796ad
Parent:
9:74b44a27fa40
Child:
13:27c19044ace6
changed some functions to protected

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 8:2b797c309258 19 LM75B::LM75B(PinName sda, PinName scl, Address addr) : 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;
neilt6 0:557a92280097 23 }
neilt6 0:557a92280097 24
neilt6 9:74b44a27fa40 25 bool LM75B::open(void)
neilt6 9:74b44a27fa40 26 {
neilt6 9:74b44a27fa40 27 //Probe for the LM75B using a Zero Length Transfer
neilt6 9:74b44a27fa40 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 8:2b797c309258 42 LM75B::PowerMode LM75B::powerMode(void)
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 8:2b797c309258 69 LM75B::OSMode LM75B::osMode(void)
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 8:2b797c309258 96 LM75B::OSPolarity LM75B::osPolarity(void)
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 8:2b797c309258 123 LM75B::OSFaultQueue LM75B::osFaultQueue(void)
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 8:2b797c309258 159 float LM75B::alertTemp(void)
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 8:2b797c309258 171 float LM75B::alertHyst(void)
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 8:2b797c309258 183 float LM75B::temp(void)
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 8:2b797c309258 199 char LM75B::read8(char reg)
neilt6 0:557a92280097 200 {
neilt6 0:557a92280097 201 //Select the register
neilt6 8:2b797c309258 202 m_I2C.write(m_Addr, &reg, 1);
neilt6 0:557a92280097 203
neilt6 0:557a92280097 204 //Read the 8-bit register
neilt6 8:2b797c309258 205 m_I2C.read(m_Addr, &reg, 1);
neilt6 0:557a92280097 206
neilt6 0:557a92280097 207 //Return the byte
neilt6 0:557a92280097 208 return reg;
neilt6 0:557a92280097 209 }
neilt6 0:557a92280097 210
neilt6 8:2b797c309258 211 void LM75B::write8(char reg, char data)
neilt6 0:557a92280097 212 {
neilt6 0:557a92280097 213 //Create a temporary buffer
neilt6 0:557a92280097 214 char buff[2];
neilt6 0:557a92280097 215
neilt6 0:557a92280097 216 //Load the register address and 8-bit data
neilt6 0:557a92280097 217 buff[0] = reg;
neilt6 0:557a92280097 218 buff[1] = data;
neilt6 0:557a92280097 219
neilt6 0:557a92280097 220 //Write the data
neilt6 8:2b797c309258 221 m_I2C.write(m_Addr, buff, 2);
neilt6 0:557a92280097 222 }
neilt6 0:557a92280097 223
neilt6 8:2b797c309258 224 unsigned short LM75B::read16(char reg)
neilt6 0:557a92280097 225 {
neilt6 0:557a92280097 226 //Create a temporary buffer
neilt6 0:557a92280097 227 char buff[2];
neilt6 0:557a92280097 228
neilt6 0:557a92280097 229 //Select the register
neilt6 8:2b797c309258 230 m_I2C.write(m_Addr, &reg, 1);
neilt6 0:557a92280097 231
neilt6 0:557a92280097 232 //Read the 16-bit register
neilt6 8:2b797c309258 233 m_I2C.read(m_Addr, buff, 2);
neilt6 0:557a92280097 234
neilt6 0:557a92280097 235 //Return the combined 16-bit value
neilt6 0:557a92280097 236 return (buff[0] << 8) | buff[1];
neilt6 0:557a92280097 237 }
neilt6 0:557a92280097 238
neilt6 8:2b797c309258 239 void LM75B::write16(char reg, unsigned short data)
neilt6 0:557a92280097 240 {
neilt6 0:557a92280097 241 //Create a temporary buffer
neilt6 0:557a92280097 242 char buff[3];
neilt6 0:557a92280097 243
neilt6 0:557a92280097 244 //Load the register address and 16-bit data
neilt6 0:557a92280097 245 buff[0] = reg;
neilt6 0:557a92280097 246 buff[1] = data >> 8;
neilt6 0:557a92280097 247 buff[2] = data;
neilt6 0:557a92280097 248
neilt6 0:557a92280097 249 //Write the data
neilt6 8:2b797c309258 250 m_I2C.write(m_Addr, buff, 3);
neilt6 0:557a92280097 251 }
neilt6 0:557a92280097 252
neilt6 8:2b797c309258 253 float LM75B::readAlertTempHelper(char reg)
neilt6 0:557a92280097 254 {
neilt6 0:557a92280097 255 //Signed return value
neilt6 0:557a92280097 256 short value;
neilt6 0:557a92280097 257
neilt6 0:557a92280097 258 //Read the 9-bit raw temperature value
neilt6 8:2b797c309258 259 value = read16(reg) >> 7;
neilt6 0:557a92280097 260
neilt6 0:557a92280097 261 //Sign extend negative numbers
neilt6 0:557a92280097 262 if (value & (1 << 8))
neilt6 0:557a92280097 263 value |= 0xFF00;
neilt6 0:557a92280097 264
neilt6 0:557a92280097 265 //Return the temperature in °C
neilt6 0:557a92280097 266 return value * 0.5;
neilt6 0:557a92280097 267 }
neilt6 0:557a92280097 268
neilt6 8:2b797c309258 269 void LM75B::writeAlertTempHelper(char reg, float temp)
neilt6 0:557a92280097 270 {
neilt6 0:557a92280097 271 //Range limit temp
neilt6 0:557a92280097 272 if (temp < -55.0)
neilt6 0:557a92280097 273 temp = -55.0;
neilt6 0:557a92280097 274 else if (temp > 125.0)
neilt6 0:557a92280097 275 temp = 125.0;
neilt6 0:557a92280097 276
neilt6 0:557a92280097 277 //Extract and shift the signed integer
neilt6 0:557a92280097 278 short value = temp * 2;
neilt6 0:557a92280097 279 value <<= 7;
neilt6 0:557a92280097 280
neilt6 0:557a92280097 281 //Send the new value
neilt6 8:2b797c309258 282 write16(reg, value);
neilt6 0:557a92280097 283 }