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