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

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

Committer:
neilt6
Date:
Fri Aug 09 22:21:36 2013 +0000
Revision:
7:bebde8098fca
Parent:
6:6b8a9d1ad49a
Child:
8:2b797c309258
Minor documentation improvements

Who changed what in which revision?

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