temperature library

Fork of LM75B by Chris Styles

Committer:
co838_ok87
Date:
Wed Feb 24 15:50:09 2016 +0000
Revision:
2:cc474eead249
Parent:
0:1be38995591b
ok87
; changes to lm75b from chris styles.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_ok87 2:cc474eead249 1 /* LM75B Driver Library
co838_ok87 2:cc474eead249 2 * Copyright (c) 2013 Neil Thiessen
chris 0:1be38995591b 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
chris 0:1be38995591b 7 *
co838_ok87 2:cc474eead249 8 * http://www.apache.org/licenses/LICENSE-2.0
chris 0:1be38995591b 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.
chris 0:1be38995591b 15 */
chris 0:1be38995591b 16
chris 0:1be38995591b 17 #ifndef LM75B_H
chris 0:1be38995591b 18 #define LM75B_H
chris 0:1be38995591b 19
chris 0:1be38995591b 20 #include "mbed.h"
chris 0:1be38995591b 21
co838_ok87 2:cc474eead249 22 /** LM75B class.
co838_ok87 2:cc474eead249 23 * Used for controlling an LM75B temperature sensor connected via I2C.
co838_ok87 2:cc474eead249 24 *
co838_ok87 2:cc474eead249 25 * Example:
co838_ok87 2:cc474eead249 26 * @code
co838_ok87 2:cc474eead249 27 * #include "mbed.h"
co838_ok87 2:cc474eead249 28 * #include "LM75B.h"
co838_ok87 2:cc474eead249 29 *
co838_ok87 2:cc474eead249 30 * //Create an LM75B object at the default address (ADDRESS_0)
co838_ok87 2:cc474eead249 31 * LM75B sensor(p28, p27);
co838_ok87 2:cc474eead249 32 *
co838_ok87 2:cc474eead249 33 * int main()
co838_ok87 2:cc474eead249 34 * {
co838_ok87 2:cc474eead249 35 * //Try to open the LM75B
co838_ok87 2:cc474eead249 36 * if (sensor.open()) {
co838_ok87 2:cc474eead249 37 * printf("Device detected!\n");
co838_ok87 2:cc474eead249 38 *
co838_ok87 2:cc474eead249 39 * while (1) {
co838_ok87 2:cc474eead249 40 * //Print the current temperature
co838_ok87 2:cc474eead249 41 * printf("Temp = %.3f\n", (float)sensor);
co838_ok87 2:cc474eead249 42 *
co838_ok87 2:cc474eead249 43 * //Sleep for 0.5 seconds
co838_ok87 2:cc474eead249 44 * wait(0.5);
co838_ok87 2:cc474eead249 45 * }
co838_ok87 2:cc474eead249 46 * } else {
co838_ok87 2:cc474eead249 47 * error("Device not detected!\n");
co838_ok87 2:cc474eead249 48 * }
co838_ok87 2:cc474eead249 49 * }
co838_ok87 2:cc474eead249 50 * @endcode
co838_ok87 2:cc474eead249 51 */
chris 0:1be38995591b 52 class LM75B
chris 0:1be38995591b 53 {
chris 0:1be38995591b 54 public:
co838_ok87 2:cc474eead249 55 /** Represents the different I2C address possibilities for the LM75B
co838_ok87 2:cc474eead249 56 */
co838_ok87 2:cc474eead249 57 enum Address {
co838_ok87 2:cc474eead249 58 ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */
co838_ok87 2:cc474eead249 59 ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */
co838_ok87 2:cc474eead249 60 ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */
co838_ok87 2:cc474eead249 61 ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */
co838_ok87 2:cc474eead249 62 ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */
co838_ok87 2:cc474eead249 63 ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */
co838_ok87 2:cc474eead249 64 ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */
co838_ok87 2:cc474eead249 65 ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */
co838_ok87 2:cc474eead249 66 };
co838_ok87 2:cc474eead249 67
co838_ok87 2:cc474eead249 68 /** Represents the power mode of the LM75B
co838_ok87 2:cc474eead249 69 */
co838_ok87 2:cc474eead249 70 enum PowerMode {
co838_ok87 2:cc474eead249 71 POWER_NORMAL, /**< Chip is enabled and samples every 100ms */
co838_ok87 2:cc474eead249 72 POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */
co838_ok87 2:cc474eead249 73 };
co838_ok87 2:cc474eead249 74
co838_ok87 2:cc474eead249 75 /** Represents OS pin mode of the LM75B
co838_ok87 2:cc474eead249 76 */
co838_ok87 2:cc474eead249 77 enum OSMode {
co838_ok87 2:cc474eead249 78 OS_COMPARATOR, /**< OS is asserted when the temperature reaches the alert threshold, and de-asserted when the temperature drops below the alert hysteresis threshold */
co838_ok87 2:cc474eead249 79 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 */
co838_ok87 2:cc474eead249 80 };
co838_ok87 2:cc474eead249 81
co838_ok87 2:cc474eead249 82 /** Represents OS pin polarity of the LM75B
co838_ok87 2:cc474eead249 83 */
co838_ok87 2:cc474eead249 84 enum OSPolarity {
co838_ok87 2:cc474eead249 85 OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */
co838_ok87 2:cc474eead249 86 OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */
co838_ok87 2:cc474eead249 87 };
co838_ok87 2:cc474eead249 88
co838_ok87 2:cc474eead249 89 /** Represents OS pin fault queue length of the LM75B
co838_ok87 2:cc474eead249 90 */
co838_ok87 2:cc474eead249 91 enum OSFaultQueue {
co838_ok87 2:cc474eead249 92 OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */
co838_ok87 2:cc474eead249 93 OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */
co838_ok87 2:cc474eead249 94 OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */
co838_ok87 2:cc474eead249 95 OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */
co838_ok87 2:cc474eead249 96 };
co838_ok87 2:cc474eead249 97
co838_ok87 2:cc474eead249 98 /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address
co838_ok87 2:cc474eead249 99 *
co838_ok87 2:cc474eead249 100 * @param sda The I2C data pin.
co838_ok87 2:cc474eead249 101 * @param scl The I2C clock pin.
co838_ok87 2:cc474eead249 102 * @param addr The I2C slave address (defaults to ADDRESS_0).
co838_ok87 2:cc474eead249 103 */
co838_ok87 2:cc474eead249 104 LM75B(PinName sda, PinName scl, Address addr = ADDRESS_0);
co838_ok87 2:cc474eead249 105
co838_ok87 2:cc474eead249 106 /** Probe for the LM75B and reset it to default configuration if present
co838_ok87 2:cc474eead249 107 *
co838_ok87 2:cc474eead249 108 * @returns
co838_ok87 2:cc474eead249 109 * 'true' if the device exists on the bus,
co838_ok87 2:cc474eead249 110 * 'false' if the device doesn't exist on the bus.
co838_ok87 2:cc474eead249 111 */
co838_ok87 2:cc474eead249 112 bool open(void);
co838_ok87 2:cc474eead249 113
co838_ok87 2:cc474eead249 114 /** Get the current power mode of the LM75B
co838_ok87 2:cc474eead249 115 *
co838_ok87 2:cc474eead249 116 * @returns The current power mode as a PowerMode enum.
co838_ok87 2:cc474eead249 117 */
co838_ok87 2:cc474eead249 118 LM75B::PowerMode powerMode(void);
co838_ok87 2:cc474eead249 119
co838_ok87 2:cc474eead249 120 /** Set the power mode of the LM75B
co838_ok87 2:cc474eead249 121 *
co838_ok87 2:cc474eead249 122 * @param mode The new power mode as a PowerMode enum.
co838_ok87 2:cc474eead249 123 */
co838_ok87 2:cc474eead249 124 void powerMode(PowerMode mode);
co838_ok87 2:cc474eead249 125
co838_ok87 2:cc474eead249 126 /** Get the current OS pin mode of the LM75B
co838_ok87 2:cc474eead249 127 *
co838_ok87 2:cc474eead249 128 * @returns The current OS pin mode as an OSMode enum.
co838_ok87 2:cc474eead249 129 */
co838_ok87 2:cc474eead249 130 LM75B::OSMode osMode(void);
co838_ok87 2:cc474eead249 131
co838_ok87 2:cc474eead249 132 /** Set the OS pin mode of the LM75B
co838_ok87 2:cc474eead249 133 *
co838_ok87 2:cc474eead249 134 * @param mode The new OS pin mode as an OSMode enum.
co838_ok87 2:cc474eead249 135 */
co838_ok87 2:cc474eead249 136 void osMode(OSMode mode);
co838_ok87 2:cc474eead249 137
co838_ok87 2:cc474eead249 138 /** Get the current OS pin polarity of the LM75B
co838_ok87 2:cc474eead249 139 *
co838_ok87 2:cc474eead249 140 * @returns The current OS pin polarity as an OSPolarity enum.
co838_ok87 2:cc474eead249 141 */
co838_ok87 2:cc474eead249 142 LM75B::OSPolarity osPolarity(void);
co838_ok87 2:cc474eead249 143
co838_ok87 2:cc474eead249 144 /** Set the OS pin polarity of the LM75B
co838_ok87 2:cc474eead249 145 *
co838_ok87 2:cc474eead249 146 * @param polarity The new OS pin polarity as an OSPolarity enum.
co838_ok87 2:cc474eead249 147 */
co838_ok87 2:cc474eead249 148 void osPolarity(OSPolarity polarity);
co838_ok87 2:cc474eead249 149
co838_ok87 2:cc474eead249 150 /** Get the current OS pin fault queue length of the LM75B
co838_ok87 2:cc474eead249 151 *
co838_ok87 2:cc474eead249 152 * @returns The current OS pin fault queue length as an OSFaultQueue enum.
co838_ok87 2:cc474eead249 153 */
co838_ok87 2:cc474eead249 154 LM75B::OSFaultQueue osFaultQueue(void);
co838_ok87 2:cc474eead249 155
co838_ok87 2:cc474eead249 156 /** Set the OS pin fault queue length of the LM75B
co838_ok87 2:cc474eead249 157 *
co838_ok87 2:cc474eead249 158 * @param queue The new OS pin fault queue length as an OSFaultQueue enum.
co838_ok87 2:cc474eead249 159 */
co838_ok87 2:cc474eead249 160 void osFaultQueue(OSFaultQueue queue);
co838_ok87 2:cc474eead249 161
co838_ok87 2:cc474eead249 162 /** Get the current alert temperature threshold of the LM75B
co838_ok87 2:cc474eead249 163 *
co838_ok87 2:cc474eead249 164 * @returns The current alert temperature threshold in °C.
co838_ok87 2:cc474eead249 165 */
co838_ok87 2:cc474eead249 166 float alertTemp(void);
co838_ok87 2:cc474eead249 167
co838_ok87 2:cc474eead249 168 /** Set the alert temperature threshold of the LM75B
co838_ok87 2:cc474eead249 169 *
co838_ok87 2:cc474eead249 170 * @param temp The new alert temperature threshold in °C.
co838_ok87 2:cc474eead249 171 */
co838_ok87 2:cc474eead249 172 void alertTemp(float temp);
co838_ok87 2:cc474eead249 173
co838_ok87 2:cc474eead249 174 /** Get the current alert temperature hysteresis threshold of the LM75B
co838_ok87 2:cc474eead249 175 *
co838_ok87 2:cc474eead249 176 * @returns The current alert temperature hysteresis threshold in °C.
co838_ok87 2:cc474eead249 177 */
co838_ok87 2:cc474eead249 178 float alertHyst(void);
co838_ok87 2:cc474eead249 179
co838_ok87 2:cc474eead249 180 /** Set the alert temperature hysteresis threshold of the LM75B
co838_ok87 2:cc474eead249 181 *
co838_ok87 2:cc474eead249 182 * @param temp The new alert temperature hysteresis threshold in °C.
co838_ok87 2:cc474eead249 183 */
co838_ok87 2:cc474eead249 184 void alertHyst(float temp);
co838_ok87 2:cc474eead249 185
co838_ok87 2:cc474eead249 186 /** Get the current temperature measurement of the LM75B
co838_ok87 2:cc474eead249 187 *
co838_ok87 2:cc474eead249 188 * @returns The current temperature measurement in °C.
co838_ok87 2:cc474eead249 189 */
co838_ok87 2:cc474eead249 190 float temp(void);
co838_ok87 2:cc474eead249 191
co838_ok87 2:cc474eead249 192 #ifdef MBED_OPERATORS
co838_ok87 2:cc474eead249 193 /** A shorthand for temp()
co838_ok87 2:cc474eead249 194 *
co838_ok87 2:cc474eead249 195 * @returns The current temperature measurement in °C.
co838_ok87 2:cc474eead249 196 */
co838_ok87 2:cc474eead249 197 operator float() {
co838_ok87 2:cc474eead249 198 return temp();
co838_ok87 2:cc474eead249 199 }
co838_ok87 2:cc474eead249 200 #endif
co838_ok87 2:cc474eead249 201
chris 0:1be38995591b 202 private:
co838_ok87 2:cc474eead249 203 //I2C register addresses
co838_ok87 2:cc474eead249 204 enum Register {
co838_ok87 2:cc474eead249 205 REG_TEMP = 0x00,
co838_ok87 2:cc474eead249 206 REG_CONF = 0x01,
co838_ok87 2:cc474eead249 207 REG_THYST = 0x02,
co838_ok87 2:cc474eead249 208 REG_TOS = 0x03
co838_ok87 2:cc474eead249 209 };
chris 0:1be38995591b 210
co838_ok87 2:cc474eead249 211 //Member variables
co838_ok87 2:cc474eead249 212 I2C m_I2C;
co838_ok87 2:cc474eead249 213 int m_Addr;
co838_ok87 2:cc474eead249 214
co838_ok87 2:cc474eead249 215 //Internal functions
co838_ok87 2:cc474eead249 216 char read8(char reg);
co838_ok87 2:cc474eead249 217 void write8(char reg, char data);
co838_ok87 2:cc474eead249 218 unsigned short read16(char reg);
co838_ok87 2:cc474eead249 219 void write16(char reg, unsigned short data);
co838_ok87 2:cc474eead249 220 float readAlertTempHelper(char reg);
co838_ok87 2:cc474eead249 221 void writeAlertTempHelper(char reg, float temp);
chris 0:1be38995591b 222 };
chris 0:1be38995591b 223
co838_ok87 2:cc474eead249 224 #endif