A feature complete driver for the LM75B temperature sensor from NXP.

Dependents:   app-board-TempAlarm LM75B IoTWorkshopSensors EduRobot ... more

Committer:
neilt6
Date:
Thu Aug 01 16:41:49 2013 +0000
Revision:
2:9ecc39b2ca70
Parent:
1:3da8df4319e8
Child:
3:9d68eed28bfb
Improved documentation, relocated enums, changed address macros to an enum

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 2:9ecc39b2ca70 1 /* LM75B Driver Library
neilt6 2:9ecc39b2ca70 2 * Copyright (c) 2013 Neil Thiessen, MIT License
neilt6 0:557a92280097 3 *
neilt6 2:9ecc39b2ca70 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
neilt6 2:9ecc39b2ca70 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
neilt6 2:9ecc39b2ca70 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
neilt6 2:9ecc39b2ca70 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
neilt6 0:557a92280097 8 * furnished to do so, subject to the following conditions:
neilt6 0:557a92280097 9 *
neilt6 2:9ecc39b2ca70 10 * The above copyright notice and this permission notice shall be included in all copies or
neilt6 1:3da8df4319e8 11 * substantial portions of the Software.
neilt6 0:557a92280097 12 *
neilt6 2:9ecc39b2ca70 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
neilt6 2:9ecc39b2ca70 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
neilt6 2:9ecc39b2ca70 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
neilt6 2:9ecc39b2ca70 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
neilt6 1:3da8df4319e8 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
neilt6 0:557a92280097 18 */
neilt6 0:557a92280097 19
neilt6 0:557a92280097 20 #include "LM75B.h"
neilt6 0:557a92280097 21 #include "mbed.h"
neilt6 0:557a92280097 22
neilt6 2:9ecc39b2ca70 23 LM75B::LM75B(PinName sda, PinName scl, Address addr) : _i2c(sda, scl)
neilt6 0:557a92280097 24 {
neilt6 0:557a92280097 25 //Set the internal device address
neilt6 2:9ecc39b2ca70 26 _addr = (int)addr;
neilt6 0:557a92280097 27 }
neilt6 0:557a92280097 28
neilt6 2:9ecc39b2ca70 29 LM75B::PowerMode LM75B::getPowerMode(void)
neilt6 0:557a92280097 30 {
neilt6 0:557a92280097 31 //Read the 8-bit register value
neilt6 2:9ecc39b2ca70 32 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 33
neilt6 0:557a92280097 34 //Return the status of the SHUTDOWN bit
neilt6 0:557a92280097 35 if (value & (1 << 0))
neilt6 2:9ecc39b2ca70 36 return POWER_SHUTDOWN;
neilt6 0:557a92280097 37 else
neilt6 2:9ecc39b2ca70 38 return POWER_NORMAL;
neilt6 0:557a92280097 39 }
neilt6 0:557a92280097 40
neilt6 2:9ecc39b2ca70 41 void LM75B::setPowerMode(PowerMode mode)
neilt6 0:557a92280097 42 {
neilt6 0:557a92280097 43 //Read the current 8-bit register value
neilt6 2:9ecc39b2ca70 44 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 45
neilt6 0:557a92280097 46 //Set or clear the SHUTDOWN bit
neilt6 2:9ecc39b2ca70 47 if (mode == POWER_SHUTDOWN)
neilt6 0:557a92280097 48 value |= (1 << 0);
neilt6 0:557a92280097 49 else
neilt6 0:557a92280097 50 value &= ~(1 << 0);
neilt6 0:557a92280097 51
neilt6 0:557a92280097 52 //Write the value back out
neilt6 2:9ecc39b2ca70 53 _write8(__LM75B_REG_CONF, value);
neilt6 0:557a92280097 54 }
neilt6 0:557a92280097 55
neilt6 2:9ecc39b2ca70 56 LM75B::OSMode LM75B::getOSMode(void)
neilt6 0:557a92280097 57 {
neilt6 0:557a92280097 58 //Read the 8-bit register value
neilt6 2:9ecc39b2ca70 59 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 60
neilt6 0:557a92280097 61 //Return the status of the OS_COMP_INT bit
neilt6 0:557a92280097 62 if (value & (1 << 1))
neilt6 2:9ecc39b2ca70 63 return OS_INTERRUPT;
neilt6 0:557a92280097 64 else
neilt6 2:9ecc39b2ca70 65 return OS_COMPARATOR;
neilt6 0:557a92280097 66 }
neilt6 0:557a92280097 67
neilt6 2:9ecc39b2ca70 68 void LM75B::setOSMode(OSMode mode)
neilt6 0:557a92280097 69 {
neilt6 0:557a92280097 70 //Read the current 8-bit register value
neilt6 2:9ecc39b2ca70 71 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 72
neilt6 0:557a92280097 73 //Set or clear the OS_COMP_INT bit
neilt6 2:9ecc39b2ca70 74 if (mode == OS_INTERRUPT)
neilt6 0:557a92280097 75 value |= (1 << 1);
neilt6 0:557a92280097 76 else
neilt6 0:557a92280097 77 value &= ~(1 << 1);
neilt6 0:557a92280097 78
neilt6 0:557a92280097 79 //Write the value back out
neilt6 2:9ecc39b2ca70 80 _write8(__LM75B_REG_CONF, value);
neilt6 0:557a92280097 81 }
neilt6 0:557a92280097 82
neilt6 2:9ecc39b2ca70 83 LM75B::OSPolarity LM75B::getOSPolarity(void)
neilt6 0:557a92280097 84 {
neilt6 0:557a92280097 85 //Read the 8-bit register value
neilt6 2:9ecc39b2ca70 86 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 87
neilt6 0:557a92280097 88 //Return the status of the OS_POL bit
neilt6 0:557a92280097 89 if (value & (1 << 2))
neilt6 2:9ecc39b2ca70 90 return OS_ACTIVE_HIGH;
neilt6 0:557a92280097 91 else
neilt6 2:9ecc39b2ca70 92 return OS_ACTIVE_LOW;
neilt6 0:557a92280097 93 }
neilt6 0:557a92280097 94
neilt6 2:9ecc39b2ca70 95 void LM75B::setOSPolarity(OSPolarity polarity)
neilt6 0:557a92280097 96 {
neilt6 0:557a92280097 97 //Read the current 8-bit register value
neilt6 2:9ecc39b2ca70 98 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 99
neilt6 0:557a92280097 100 //Set or clear the OS_POL bit
neilt6 2:9ecc39b2ca70 101 if (polarity == OS_ACTIVE_HIGH)
neilt6 0:557a92280097 102 value |= (1 << 2);
neilt6 0:557a92280097 103 else
neilt6 0:557a92280097 104 value &= ~(1 << 2);
neilt6 0:557a92280097 105
neilt6 0:557a92280097 106 //Write the value back out
neilt6 2:9ecc39b2ca70 107 _write8(__LM75B_REG_CONF, value);
neilt6 0:557a92280097 108 }
neilt6 0:557a92280097 109
neilt6 2:9ecc39b2ca70 110 LM75B::OSFaultQueue LM75B::getOSFaultQueue(void)
neilt6 0:557a92280097 111 {
neilt6 0:557a92280097 112 //Read the 8-bit register value
neilt6 2:9ecc39b2ca70 113 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 114
neilt6 0:557a92280097 115 //Return the status of the OS_F_QUE bits
neilt6 0:557a92280097 116 if ((value & (1 << 3)) && (value & (1 << 4)))
neilt6 2:9ecc39b2ca70 117 return OS_FAULT_QUEUE_6;
neilt6 0:557a92280097 118 else if (!(value & (1 << 3)) && (value & (1 << 4)))
neilt6 2:9ecc39b2ca70 119 return OS_FAULT_QUEUE_4;
neilt6 0:557a92280097 120 else if ((value & (1 << 3)) && !(value & (1 << 4)))
neilt6 2:9ecc39b2ca70 121 return OS_FAULT_QUEUE_2;
neilt6 0:557a92280097 122 else
neilt6 2:9ecc39b2ca70 123 return OS_FAULT_QUEUE_1;
neilt6 0:557a92280097 124 }
neilt6 0:557a92280097 125
neilt6 2:9ecc39b2ca70 126 void LM75B::setOSFaultQueue(OSFaultQueue queue)
neilt6 0:557a92280097 127 {
neilt6 0:557a92280097 128 //Read the current 8-bit register value
neilt6 2:9ecc39b2ca70 129 char value = _read8(__LM75B_REG_CONF);
neilt6 0:557a92280097 130
neilt6 0:557a92280097 131 //Clear the old OS_F_QUE bits
neilt6 0:557a92280097 132 value &= ~(3 << 3);
neilt6 0:557a92280097 133
neilt6 0:557a92280097 134 //Set the new OS_F_QUE bits
neilt6 2:9ecc39b2ca70 135 if (queue == OS_FAULT_QUEUE_2)
neilt6 0:557a92280097 136 value |= (1 << 3);
neilt6 2:9ecc39b2ca70 137 else if (queue == OS_FAULT_QUEUE_4)
neilt6 0:557a92280097 138 value |= (2 << 3);
neilt6 2:9ecc39b2ca70 139 else if (queue == OS_FAULT_QUEUE_6)
neilt6 0:557a92280097 140 value |= (3 << 3);
neilt6 0:557a92280097 141
neilt6 0:557a92280097 142 //Write the value back out
neilt6 2:9ecc39b2ca70 143 _write8(__LM75B_REG_CONF, value);
neilt6 0:557a92280097 144 }
neilt6 0:557a92280097 145
neilt6 0:557a92280097 146 float LM75B::getAlertTemp(void)
neilt6 0:557a92280097 147 {
neilt6 0:557a92280097 148 //Use the 9-bit helper to read the TOS register
neilt6 2:9ecc39b2ca70 149 return _readTempHelper(__LM75B_REG_TOS);
neilt6 0:557a92280097 150 }
neilt6 0:557a92280097 151
neilt6 0:557a92280097 152 void LM75B::setAlertTemp(float temp)
neilt6 0:557a92280097 153 {
neilt6 0:557a92280097 154 //Use the 9-bit helper to write to the TOS register
neilt6 2:9ecc39b2ca70 155 return _writeTempHelper(__LM75B_REG_TOS, temp);
neilt6 0:557a92280097 156 }
neilt6 0:557a92280097 157
neilt6 0:557a92280097 158 float LM75B::getAlertHyst(void)
neilt6 0:557a92280097 159 {
neilt6 0:557a92280097 160 //Use the 9-bit helper to read the THYST register
neilt6 2:9ecc39b2ca70 161 return _readTempHelper(__LM75B_REG_THYST);
neilt6 0:557a92280097 162 }
neilt6 0:557a92280097 163
neilt6 0:557a92280097 164 void LM75B::setAlertHyst(float temp)
neilt6 0:557a92280097 165 {
neilt6 0:557a92280097 166 //Use the 9-bit helper to write to the THYST register
neilt6 2:9ecc39b2ca70 167 return _writeTempHelper(__LM75B_REG_THYST, temp);
neilt6 0:557a92280097 168 }
neilt6 0:557a92280097 169
neilt6 0:557a92280097 170 float LM75B::getTemp(void)
neilt6 0:557a92280097 171 {
neilt6 0:557a92280097 172 //Signed return value
neilt6 0:557a92280097 173 short value;
neilt6 0:557a92280097 174
neilt6 0:557a92280097 175 //Read the 11-bit raw temperature value
neilt6 2:9ecc39b2ca70 176 value = _read16(__LM75B_REG_TEMP) >> 5;
neilt6 0:557a92280097 177
neilt6 0:557a92280097 178 //Sign extend negative numbers
neilt6 0:557a92280097 179 if (value & (1 << 10))
neilt6 0:557a92280097 180 value |= 0xFC00;
neilt6 0:557a92280097 181
neilt6 0:557a92280097 182 //Return the temperature in °C
neilt6 0:557a92280097 183 return value * 0.125;
neilt6 0:557a92280097 184 }
neilt6 0:557a92280097 185
neilt6 0:557a92280097 186 char LM75B::_read8(char reg)
neilt6 0:557a92280097 187 {
neilt6 0:557a92280097 188 //Select the register
neilt6 0:557a92280097 189 _i2c.write(_addr, &reg, 1);
neilt6 0:557a92280097 190
neilt6 0:557a92280097 191 //Read the 8-bit register
neilt6 0:557a92280097 192 _i2c.read(_addr, &reg, 1);
neilt6 0:557a92280097 193
neilt6 0:557a92280097 194 //Return the byte
neilt6 0:557a92280097 195 return reg;
neilt6 0:557a92280097 196 }
neilt6 0:557a92280097 197
neilt6 0:557a92280097 198 void LM75B::_write8(char reg, char data)
neilt6 0:557a92280097 199 {
neilt6 0:557a92280097 200 //Create a temporary buffer
neilt6 0:557a92280097 201 char buff[2];
neilt6 0:557a92280097 202
neilt6 0:557a92280097 203 //Load the register address and 8-bit data
neilt6 0:557a92280097 204 buff[0] = reg;
neilt6 0:557a92280097 205 buff[1] = data;
neilt6 0:557a92280097 206
neilt6 0:557a92280097 207 //Write the data
neilt6 0:557a92280097 208 _i2c.write(_addr, buff, 2);
neilt6 0:557a92280097 209 }
neilt6 0:557a92280097 210
neilt6 0:557a92280097 211 unsigned short LM75B::_read16(char reg)
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 //Select the register
neilt6 0:557a92280097 217 _i2c.write(_addr, &reg, 1);
neilt6 0:557a92280097 218
neilt6 0:557a92280097 219 //Read the 16-bit register
neilt6 0:557a92280097 220 _i2c.read(_addr, buff, 2);
neilt6 0:557a92280097 221
neilt6 0:557a92280097 222 //Return the combined 16-bit value
neilt6 0:557a92280097 223 return (buff[0] << 8) | buff[1];
neilt6 0:557a92280097 224 }
neilt6 0:557a92280097 225
neilt6 0:557a92280097 226 void LM75B::_write16(char reg, unsigned short data)
neilt6 0:557a92280097 227 {
neilt6 0:557a92280097 228 //Create a temporary buffer
neilt6 0:557a92280097 229 char buff[3];
neilt6 0:557a92280097 230
neilt6 0:557a92280097 231 //Load the register address and 16-bit data
neilt6 0:557a92280097 232 buff[0] = reg;
neilt6 0:557a92280097 233 buff[1] = data >> 8;
neilt6 0:557a92280097 234 buff[2] = data;
neilt6 0:557a92280097 235
neilt6 0:557a92280097 236 //Write the data
neilt6 0:557a92280097 237 _i2c.write(_addr, buff, 3);
neilt6 0:557a92280097 238 }
neilt6 0:557a92280097 239
neilt6 0:557a92280097 240 float LM75B::_readTempHelper(char reg)
neilt6 0:557a92280097 241 {
neilt6 0:557a92280097 242 //Signed return value
neilt6 0:557a92280097 243 short value;
neilt6 0:557a92280097 244
neilt6 0:557a92280097 245 //Read the 9-bit raw temperature value
neilt6 0:557a92280097 246 value = _read16(reg) >> 7;
neilt6 0:557a92280097 247
neilt6 0:557a92280097 248 //Sign extend negative numbers
neilt6 0:557a92280097 249 if (value & (1 << 8))
neilt6 0:557a92280097 250 value |= 0xFF00;
neilt6 0:557a92280097 251
neilt6 0:557a92280097 252 //Return the temperature in °C
neilt6 0:557a92280097 253 return value * 0.5;
neilt6 0:557a92280097 254 }
neilt6 0:557a92280097 255
neilt6 0:557a92280097 256 void LM75B::_writeTempHelper(char reg, float temp)
neilt6 0:557a92280097 257 {
neilt6 0:557a92280097 258 //Range limit temp
neilt6 0:557a92280097 259 if (temp < -55.0)
neilt6 0:557a92280097 260 temp = -55.0;
neilt6 0:557a92280097 261 else if (temp > 125.0)
neilt6 0:557a92280097 262 temp = 125.0;
neilt6 0:557a92280097 263
neilt6 0:557a92280097 264 //Extract and shift the signed integer
neilt6 0:557a92280097 265 short value = temp * 2;
neilt6 0:557a92280097 266 value <<= 7;
neilt6 0:557a92280097 267
neilt6 0:557a92280097 268 //Send the new value
neilt6 0:557a92280097 269 _write16(reg, value);
neilt6 0:557a92280097 270 }