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 #ifndef LM75B_H
neilt6 0:557a92280097 21 #define LM75B_H
neilt6 0:557a92280097 22
neilt6 0:557a92280097 23 #include "mbed.h"
neilt6 0:557a92280097 24
neilt6 0:557a92280097 25 //i2c register definitions
neilt6 2:9ecc39b2ca70 26 #define __LM75B_REG_TEMP 0x00
neilt6 2:9ecc39b2ca70 27 #define __LM75B_REG_CONF 0x01
neilt6 2:9ecc39b2ca70 28 #define __LM75B_REG_THYST 0x02
neilt6 2:9ecc39b2ca70 29 #define __LM75B_REG_TOS 0x03
neilt6 0:557a92280097 30
neilt6 0:557a92280097 31 /** LM75B class.
neilt6 0:557a92280097 32 * Used for controlling an LM75B temperature sensor connected via I2C.
neilt6 0:557a92280097 33 *
neilt6 0:557a92280097 34 * Example:
neilt6 0:557a92280097 35 * @code
neilt6 0:557a92280097 36 * #include "mbed.h"
neilt6 0:557a92280097 37 * #include "LM75B.h"
neilt6 0:557a92280097 38 *
neilt6 0:557a92280097 39 * Serial pc(USBTX, USBRX);
neilt6 2:9ecc39b2ca70 40 * LM75B sensor(p28, p27, LM75B::ADDRESS_0);
neilt6 0:557a92280097 41 *
neilt6 0:557a92280097 42 * int main() {
neilt6 0:557a92280097 43 * while (1) {
neilt6 0:557a92280097 44 * //Read the temperature
neilt6 0:557a92280097 45 * float temp = sensor.getTemp();
neilt6 0:557a92280097 46 *
neilt6 0:557a92280097 47 * //Print the temperature
neilt6 0:557a92280097 48 * pc.printf("Temp = %.3f\n", temp);
neilt6 0:557a92280097 49 *
neilt6 0:557a92280097 50 * //Sleep for 0.5 seconds
neilt6 0:557a92280097 51 * wait(0.5);
neilt6 0:557a92280097 52 * }
neilt6 0:557a92280097 53 * }
neilt6 0:557a92280097 54 * @endcode
neilt6 0:557a92280097 55 */
neilt6 0:557a92280097 56 class LM75B
neilt6 0:557a92280097 57 {
neilt6 0:557a92280097 58 public:
neilt6 2:9ecc39b2ca70 59 /** Represents the different I2C address possibilities for the LM75B
neilt6 2:9ecc39b2ca70 60 */
neilt6 2:9ecc39b2ca70 61 enum Address {
neilt6 2:9ecc39b2ca70 62 ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */
neilt6 2:9ecc39b2ca70 63 ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */
neilt6 2:9ecc39b2ca70 64 ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */
neilt6 2:9ecc39b2ca70 65 ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */
neilt6 2:9ecc39b2ca70 66 ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */
neilt6 2:9ecc39b2ca70 67 ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */
neilt6 2:9ecc39b2ca70 68 ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */
neilt6 2:9ecc39b2ca70 69 ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */
neilt6 2:9ecc39b2ca70 70 };
neilt6 2:9ecc39b2ca70 71
neilt6 2:9ecc39b2ca70 72 /** Represents the power mode of the LM75B
neilt6 2:9ecc39b2ca70 73 */
neilt6 2:9ecc39b2ca70 74 enum PowerMode {
neilt6 2:9ecc39b2ca70 75 POWER_NORMAL, /**< Chip is enabled and samples every 100ms */
neilt6 2:9ecc39b2ca70 76 POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */
neilt6 2:9ecc39b2ca70 77 };
neilt6 2:9ecc39b2ca70 78
neilt6 2:9ecc39b2ca70 79 /** Represents OS pin mode of the LM75B
neilt6 2:9ecc39b2ca70 80 */
neilt6 2:9ecc39b2ca70 81 enum OSMode {
neilt6 2:9ecc39b2ca70 82 OS_COMPARATOR, /**< OS is asserted when the temperature reaches the alert threshold, and de-asserted when the temperature drops below the alert hysteresis threshold */
neilt6 2:9ecc39b2ca70 83 OS_INTERRUPT /**< OS is asserted when the temperature reaches the alert threshold, or drops below the alert hysteresis threshold, and only de-asserted when a register has been read */
neilt6 2:9ecc39b2ca70 84 };
neilt6 2:9ecc39b2ca70 85
neilt6 2:9ecc39b2ca70 86 /** Represents OS pin polarity of the LM75B
neilt6 2:9ecc39b2ca70 87 */
neilt6 2:9ecc39b2ca70 88 enum OSPolarity {
neilt6 2:9ecc39b2ca70 89 OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */
neilt6 2:9ecc39b2ca70 90 OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */
neilt6 2:9ecc39b2ca70 91 };
neilt6 2:9ecc39b2ca70 92
neilt6 2:9ecc39b2ca70 93 /** Represents OS pin fault queue length of the LM75B
neilt6 2:9ecc39b2ca70 94 */
neilt6 2:9ecc39b2ca70 95 enum OSFaultQueue {
neilt6 2:9ecc39b2ca70 96 OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */
neilt6 2:9ecc39b2ca70 97 OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */
neilt6 2:9ecc39b2ca70 98 OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */
neilt6 2:9ecc39b2ca70 99 OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */
neilt6 2:9ecc39b2ca70 100 };
neilt6 2:9ecc39b2ca70 101
neilt6 0:557a92280097 102 /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address
neilt6 0:557a92280097 103 *
neilt6 0:557a92280097 104 * @param sda I2C data pin
neilt6 0:557a92280097 105 * @param scl I2C clock pin
neilt6 0:557a92280097 106 * @param addr I2C slave address
neilt6 0:557a92280097 107 */
neilt6 2:9ecc39b2ca70 108 LM75B(PinName sda, PinName scl, Address addr);
neilt6 2:9ecc39b2ca70 109
neilt6 0:557a92280097 110 /** Get the current power mode of the LM75B
neilt6 0:557a92280097 111 *
neilt6 0:557a92280097 112 * @returns The current power mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 113 */
neilt6 2:9ecc39b2ca70 114 LM75B::PowerMode getPowerMode(void);
neilt6 2:9ecc39b2ca70 115
neilt6 0:557a92280097 116 /** Set the power mode of the LM75B
neilt6 0:557a92280097 117 *
neilt6 0:557a92280097 118 * @param mode The new power mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 119 */
neilt6 2:9ecc39b2ca70 120 void setPowerMode(PowerMode mode);
neilt6 2:9ecc39b2ca70 121
neilt6 0:557a92280097 122 /** Get the current OS pin mode of the LM75B
neilt6 0:557a92280097 123 *
neilt6 0:557a92280097 124 * @returns The current OS pin mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 125 */
neilt6 2:9ecc39b2ca70 126 LM75B::OSMode getOSMode(void);
neilt6 2:9ecc39b2ca70 127
neilt6 0:557a92280097 128 /** Set the OS pin mode of the LM75B
neilt6 0:557a92280097 129 *
neilt6 0:557a92280097 130 * @param mode The new OS pin mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 131 */
neilt6 2:9ecc39b2ca70 132 void setOSMode(OSMode mode);
neilt6 2:9ecc39b2ca70 133
neilt6 0:557a92280097 134 /** Get the current OS pin polarity of the LM75B
neilt6 0:557a92280097 135 *
neilt6 0:557a92280097 136 * @returns The current OS pin polarity as an LM75B_OS_Polarity_t enum.
neilt6 0:557a92280097 137 */
neilt6 2:9ecc39b2ca70 138 LM75B::OSPolarity getOSPolarity(void);
neilt6 2:9ecc39b2ca70 139
neilt6 0:557a92280097 140 /** Set the OS pin polarity of the LM75B
neilt6 0:557a92280097 141 *
neilt6 0:557a92280097 142 * @param polarity The new OS pin polarity as an LM75B_OS_Polarity_t enum.
neilt6 0:557a92280097 143 */
neilt6 2:9ecc39b2ca70 144 void setOSPolarity(OSPolarity polarity);
neilt6 2:9ecc39b2ca70 145
neilt6 0:557a92280097 146 /** Get the current OS pin fault queue length of the LM75B
neilt6 0:557a92280097 147 *
neilt6 0:557a92280097 148 * @returns The current OS pin fault queue length as an LM75B_OS_FaultQueue_t enum.
neilt6 0:557a92280097 149 */
neilt6 2:9ecc39b2ca70 150 LM75B::OSFaultQueue getOSFaultQueue(void);
neilt6 2:9ecc39b2ca70 151
neilt6 0:557a92280097 152 /** Set the OS pin fault queue length of the LM75B
neilt6 0:557a92280097 153 *
neilt6 0:557a92280097 154 * @param queue The new OS pin fault queue length as an LM75B_OS_FaultQueue_t enum.
neilt6 0:557a92280097 155 */
neilt6 2:9ecc39b2ca70 156 void setOSFaultQueue(OSFaultQueue queue);
neilt6 2:9ecc39b2ca70 157
neilt6 0:557a92280097 158 /** Get the current alert temperature threshold of the LM75B
neilt6 0:557a92280097 159 *
neilt6 0:557a92280097 160 * @returns The current alert temperature threshold as a float.
neilt6 0:557a92280097 161 */
neilt6 0:557a92280097 162 float getAlertTemp(void);
neilt6 2:9ecc39b2ca70 163
neilt6 0:557a92280097 164 /** Set the alert temperature threshold of the LM75B
neilt6 0:557a92280097 165 *
neilt6 0:557a92280097 166 * @param temp The new alert temperature threshold as a float.
neilt6 0:557a92280097 167 */
neilt6 0:557a92280097 168 void setAlertTemp(float temp);
neilt6 2:9ecc39b2ca70 169
neilt6 0:557a92280097 170 /** Get the current alert temperature hysteresis threshold of the LM75B
neilt6 0:557a92280097 171 *
neilt6 0:557a92280097 172 * @returns The current alert temperature hysteresis threshold as a float.
neilt6 0:557a92280097 173 */
neilt6 0:557a92280097 174 float getAlertHyst(void);
neilt6 2:9ecc39b2ca70 175
neilt6 0:557a92280097 176 /** Set the alert temperature hysteresis threshold of the LM75B
neilt6 0:557a92280097 177 *
neilt6 0:557a92280097 178 * @param temp The new alert temperature hysteresis threshold as a float.
neilt6 0:557a92280097 179 */
neilt6 0:557a92280097 180 void setAlertHyst(float temp);
neilt6 2:9ecc39b2ca70 181
neilt6 0:557a92280097 182 /** Get the current temperature measurement of the LM75B
neilt6 0:557a92280097 183 *
neilt6 0:557a92280097 184 * @returns The current temperature measurement as a float.
neilt6 0:557a92280097 185 */
neilt6 0:557a92280097 186 float getTemp(void);
neilt6 0:557a92280097 187
neilt6 0:557a92280097 188 private:
neilt6 0:557a92280097 189 I2C _i2c;
neilt6 0:557a92280097 190 int _addr;
neilt6 0:557a92280097 191 char _read8(char reg);
neilt6 0:557a92280097 192 void _write8(char reg, char data);
neilt6 0:557a92280097 193 unsigned short _read16(char reg);
neilt6 0:557a92280097 194 void _write16(char reg, unsigned short data);
neilt6 0:557a92280097 195 float _readTempHelper(char reg);
neilt6 0:557a92280097 196 void _writeTempHelper(char reg, float temp);
neilt6 0:557a92280097 197 };
neilt6 0:557a92280097 198
neilt6 0:557a92280097 199 #endif