A feature complete driver for the LM75B temperature sensor from NXP.
Fork of LM75B by
LM75B.cpp@8:2b797c309258, 2013-08-14 (annotated)
- Committer:
- neilt6
- Date:
- Wed Aug 14 04:33:25 2013 +0000
- Revision:
- 8:2b797c309258
- Parent:
- 4:06676376766a
- Child:
- 9:74b44a27fa40
Modified library so as to be more compliant with C++ conventions
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 | #include "mbed.h" |
neilt6 | 0:557a92280097 | 19 | |
neilt6 | 8:2b797c309258 | 20 | LM75B::LM75B(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl) |
neilt6 | 0:557a92280097 | 21 | { |
neilt6 | 0:557a92280097 | 22 | //Set the internal device address |
neilt6 | 8:2b797c309258 | 23 | m_Addr = (int)addr; |
neilt6 | 0:557a92280097 | 24 | } |
neilt6 | 0:557a92280097 | 25 | |
neilt6 | 8:2b797c309258 | 26 | LM75B::PowerMode LM75B::powerMode(void) |
neilt6 | 0:557a92280097 | 27 | { |
neilt6 | 0:557a92280097 | 28 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 29 | char value = read8(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 | 8:2b797c309258 | 38 | void LM75B::powerMode(PowerMode mode) |
neilt6 | 0:557a92280097 | 39 | { |
neilt6 | 0:557a92280097 | 40 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 41 | char value = read8(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 | 8:2b797c309258 | 50 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 51 | } |
neilt6 | 0:557a92280097 | 52 | |
neilt6 | 8:2b797c309258 | 53 | LM75B::OSMode LM75B::osMode(void) |
neilt6 | 0:557a92280097 | 54 | { |
neilt6 | 0:557a92280097 | 55 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 56 | char value = read8(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 | 8:2b797c309258 | 65 | void LM75B::osMode(OSMode mode) |
neilt6 | 0:557a92280097 | 66 | { |
neilt6 | 0:557a92280097 | 67 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 68 | char value = read8(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 | 8:2b797c309258 | 77 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 78 | } |
neilt6 | 0:557a92280097 | 79 | |
neilt6 | 8:2b797c309258 | 80 | LM75B::OSPolarity LM75B::osPolarity(void) |
neilt6 | 0:557a92280097 | 81 | { |
neilt6 | 0:557a92280097 | 82 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 83 | char value = read8(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 | 8:2b797c309258 | 92 | void LM75B::osPolarity(OSPolarity polarity) |
neilt6 | 0:557a92280097 | 93 | { |
neilt6 | 0:557a92280097 | 94 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 95 | char value = read8(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 | 8:2b797c309258 | 104 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 105 | } |
neilt6 | 0:557a92280097 | 106 | |
neilt6 | 8:2b797c309258 | 107 | LM75B::OSFaultQueue LM75B::osFaultQueue(void) |
neilt6 | 0:557a92280097 | 108 | { |
neilt6 | 0:557a92280097 | 109 | //Read the 8-bit register value |
neilt6 | 8:2b797c309258 | 110 | char value = read8(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 | 8:2b797c309258 | 123 | void LM75B::osFaultQueue(OSFaultQueue queue) |
neilt6 | 0:557a92280097 | 124 | { |
neilt6 | 0:557a92280097 | 125 | //Read the current 8-bit register value |
neilt6 | 8:2b797c309258 | 126 | char value = read8(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 | 8:2b797c309258 | 140 | write8(REG_CONF, value); |
neilt6 | 0:557a92280097 | 141 | } |
neilt6 | 0:557a92280097 | 142 | |
neilt6 | 8:2b797c309258 | 143 | float LM75B::alertTemp(void) |
neilt6 | 0:557a92280097 | 144 | { |
neilt6 | 0:557a92280097 | 145 | //Use the 9-bit helper to read the TOS register |
neilt6 | 8:2b797c309258 | 146 | return readAlertTempHelper(REG_TOS); |
neilt6 | 0:557a92280097 | 147 | } |
neilt6 | 0:557a92280097 | 148 | |
neilt6 | 8:2b797c309258 | 149 | void LM75B::alertTemp(float temp) |
neilt6 | 0:557a92280097 | 150 | { |
neilt6 | 0:557a92280097 | 151 | //Use the 9-bit helper to write to the TOS register |
neilt6 | 8:2b797c309258 | 152 | return writeAlertTempHelper(REG_TOS, temp); |
neilt6 | 0:557a92280097 | 153 | } |
neilt6 | 0:557a92280097 | 154 | |
neilt6 | 8:2b797c309258 | 155 | float LM75B::alertHyst(void) |
neilt6 | 0:557a92280097 | 156 | { |
neilt6 | 0:557a92280097 | 157 | //Use the 9-bit helper to read the THYST register |
neilt6 | 8:2b797c309258 | 158 | return readAlertTempHelper(REG_THYST); |
neilt6 | 0:557a92280097 | 159 | } |
neilt6 | 0:557a92280097 | 160 | |
neilt6 | 8:2b797c309258 | 161 | void LM75B::alertHyst(float temp) |
neilt6 | 0:557a92280097 | 162 | { |
neilt6 | 0:557a92280097 | 163 | //Use the 9-bit helper to write to the THYST register |
neilt6 | 8:2b797c309258 | 164 | return writeAlertTempHelper(REG_THYST, temp); |
neilt6 | 0:557a92280097 | 165 | } |
neilt6 | 0:557a92280097 | 166 | |
neilt6 | 8:2b797c309258 | 167 | float LM75B::temp(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 | 8:2b797c309258 | 173 | value = read16(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 | 8:2b797c309258 | 183 | char LM75B::read8(char reg) |
neilt6 | 0:557a92280097 | 184 | { |
neilt6 | 0:557a92280097 | 185 | //Select the register |
neilt6 | 8:2b797c309258 | 186 | m_I2C.write(m_Addr, ®, 1); |
neilt6 | 0:557a92280097 | 187 | |
neilt6 | 0:557a92280097 | 188 | //Read the 8-bit register |
neilt6 | 8:2b797c309258 | 189 | m_I2C.read(m_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 | 8:2b797c309258 | 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 | 8:2b797c309258 | 205 | m_I2C.write(m_Addr, buff, 2); |
neilt6 | 0:557a92280097 | 206 | } |
neilt6 | 0:557a92280097 | 207 | |
neilt6 | 8:2b797c309258 | 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 | 8:2b797c309258 | 214 | m_I2C.write(m_Addr, ®, 1); |
neilt6 | 0:557a92280097 | 215 | |
neilt6 | 0:557a92280097 | 216 | //Read the 16-bit register |
neilt6 | 8:2b797c309258 | 217 | m_I2C.read(m_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 | 8:2b797c309258 | 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 | 8:2b797c309258 | 234 | m_I2C.write(m_Addr, buff, 3); |
neilt6 | 0:557a92280097 | 235 | } |
neilt6 | 0:557a92280097 | 236 | |
neilt6 | 8:2b797c309258 | 237 | float LM75B::readAlertTempHelper(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 | 8:2b797c309258 | 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 | 8:2b797c309258 | 253 | void LM75B::writeAlertTempHelper(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 | 8:2b797c309258 | 266 | write16(reg, value); |
neilt6 | 0:557a92280097 | 267 | } |