A feature complete driver for the LM75B temperature sensor from NXP.
Dependents: app-board-TempAlarm LM75B IoTWorkshopSensors EduRobot ... more
LM75B.h@11:9bd544dd9e07, 2013-08-28 (annotated)
- Committer:
- neilt6
- Date:
- Wed Aug 28 17:42:55 2013 +0000
- Revision:
- 11:9bd544dd9e07
- Parent:
- 10:51d149fa47bd
- Child:
- 12:fc27dc535ea9
Minor example improvements (exits on failure)
Who changed what in which revision?
User | Revision | Line number | New 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 | /** LM75B class. |
neilt6 | 0:557a92280097 | 23 | * Used for controlling an LM75B temperature sensor connected via I2C. |
neilt6 | 0:557a92280097 | 24 | * |
neilt6 | 0:557a92280097 | 25 | * Example: |
neilt6 | 0:557a92280097 | 26 | * @code |
neilt6 | 0:557a92280097 | 27 | * #include "mbed.h" |
neilt6 | 0:557a92280097 | 28 | * #include "LM75B.h" |
neilt6 | 0:557a92280097 | 29 | * |
neilt6 | 2:9ecc39b2ca70 | 30 | * LM75B sensor(p28, p27, LM75B::ADDRESS_0); |
neilt6 | 0:557a92280097 | 31 | * |
neilt6 | 9:74b44a27fa40 | 32 | * int main() |
neilt6 | 9:74b44a27fa40 | 33 | * { |
neilt6 | 9:74b44a27fa40 | 34 | * //Try to open the LM75B |
neilt6 | 9:74b44a27fa40 | 35 | * if (sensor.open()) { |
neilt6 | 9:74b44a27fa40 | 36 | * printf("Device detected!\n"); |
neilt6 | 9:74b44a27fa40 | 37 | * |
neilt6 | 9:74b44a27fa40 | 38 | * while (1) { |
neilt6 | 9:74b44a27fa40 | 39 | * //Read the temperature |
neilt6 | 9:74b44a27fa40 | 40 | * float temp = sensor.temp(); |
neilt6 | 0:557a92280097 | 41 | * |
neilt6 | 9:74b44a27fa40 | 42 | * //Print the temperature |
neilt6 | 9:74b44a27fa40 | 43 | * printf("Temp = %.3f\n", temp); |
neilt6 | 0:557a92280097 | 44 | * |
neilt6 | 9:74b44a27fa40 | 45 | * //Sleep for 0.5 seconds |
neilt6 | 9:74b44a27fa40 | 46 | * wait(0.5); |
neilt6 | 9:74b44a27fa40 | 47 | * } |
neilt6 | 9:74b44a27fa40 | 48 | * } else { |
neilt6 | 9:74b44a27fa40 | 49 | * printf("Device not detected!\n"); |
neilt6 | 0:557a92280097 | 50 | * } |
neilt6 | 0:557a92280097 | 51 | * } |
neilt6 | 0:557a92280097 | 52 | * @endcode |
neilt6 | 0:557a92280097 | 53 | */ |
neilt6 | 0:557a92280097 | 54 | class LM75B |
neilt6 | 0:557a92280097 | 55 | { |
neilt6 | 0:557a92280097 | 56 | public: |
neilt6 | 2:9ecc39b2ca70 | 57 | /** Represents the different I2C address possibilities for the LM75B |
neilt6 | 2:9ecc39b2ca70 | 58 | */ |
neilt6 | 2:9ecc39b2ca70 | 59 | enum Address { |
neilt6 | 2:9ecc39b2ca70 | 60 | ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */ |
neilt6 | 2:9ecc39b2ca70 | 61 | ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */ |
neilt6 | 2:9ecc39b2ca70 | 62 | ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */ |
neilt6 | 2:9ecc39b2ca70 | 63 | ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */ |
neilt6 | 2:9ecc39b2ca70 | 64 | ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */ |
neilt6 | 2:9ecc39b2ca70 | 65 | ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */ |
neilt6 | 2:9ecc39b2ca70 | 66 | ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */ |
neilt6 | 2:9ecc39b2ca70 | 67 | ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */ |
neilt6 | 2:9ecc39b2ca70 | 68 | }; |
neilt6 | 2:9ecc39b2ca70 | 69 | |
neilt6 | 2:9ecc39b2ca70 | 70 | /** Represents the power mode of the LM75B |
neilt6 | 2:9ecc39b2ca70 | 71 | */ |
neilt6 | 2:9ecc39b2ca70 | 72 | enum PowerMode { |
neilt6 | 2:9ecc39b2ca70 | 73 | POWER_NORMAL, /**< Chip is enabled and samples every 100ms */ |
neilt6 | 2:9ecc39b2ca70 | 74 | POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */ |
neilt6 | 2:9ecc39b2ca70 | 75 | }; |
neilt6 | 2:9ecc39b2ca70 | 76 | |
neilt6 | 2:9ecc39b2ca70 | 77 | /** Represents OS pin mode of the LM75B |
neilt6 | 2:9ecc39b2ca70 | 78 | */ |
neilt6 | 2:9ecc39b2ca70 | 79 | enum OSMode { |
neilt6 | 2:9ecc39b2ca70 | 80 | 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 | 81 | 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 | 82 | }; |
neilt6 | 2:9ecc39b2ca70 | 83 | |
neilt6 | 2:9ecc39b2ca70 | 84 | /** Represents OS pin polarity of the LM75B |
neilt6 | 2:9ecc39b2ca70 | 85 | */ |
neilt6 | 2:9ecc39b2ca70 | 86 | enum OSPolarity { |
neilt6 | 2:9ecc39b2ca70 | 87 | OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */ |
neilt6 | 2:9ecc39b2ca70 | 88 | OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */ |
neilt6 | 2:9ecc39b2ca70 | 89 | }; |
neilt6 | 2:9ecc39b2ca70 | 90 | |
neilt6 | 2:9ecc39b2ca70 | 91 | /** Represents OS pin fault queue length of the LM75B |
neilt6 | 2:9ecc39b2ca70 | 92 | */ |
neilt6 | 2:9ecc39b2ca70 | 93 | enum OSFaultQueue { |
neilt6 | 2:9ecc39b2ca70 | 94 | OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */ |
neilt6 | 2:9ecc39b2ca70 | 95 | OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */ |
neilt6 | 2:9ecc39b2ca70 | 96 | OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */ |
neilt6 | 2:9ecc39b2ca70 | 97 | OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */ |
neilt6 | 2:9ecc39b2ca70 | 98 | }; |
neilt6 | 2:9ecc39b2ca70 | 99 | |
neilt6 | 0:557a92280097 | 100 | /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address |
neilt6 | 0:557a92280097 | 101 | * |
neilt6 | 7:bebde8098fca | 102 | * @param sda The I2C data pin. |
neilt6 | 7:bebde8098fca | 103 | * @param scl The I2C clock pin. |
neilt6 | 7:bebde8098fca | 104 | * @param addr The I2C slave address. |
neilt6 | 0:557a92280097 | 105 | */ |
neilt6 | 2:9ecc39b2ca70 | 106 | LM75B(PinName sda, PinName scl, Address addr); |
neilt6 | 2:9ecc39b2ca70 | 107 | |
neilt6 | 10:51d149fa47bd | 108 | /** Probe for the LM75B and reset it to default configuration if present |
neilt6 | 9:74b44a27fa40 | 109 | * |
neilt6 | 9:74b44a27fa40 | 110 | * @returns |
neilt6 | 9:74b44a27fa40 | 111 | * 'true' if the device exists on the bus, |
neilt6 | 9:74b44a27fa40 | 112 | * 'false' if the device doesn't exist on the bus. |
neilt6 | 9:74b44a27fa40 | 113 | */ |
neilt6 | 9:74b44a27fa40 | 114 | bool open(void); |
neilt6 | 9:74b44a27fa40 | 115 | |
neilt6 | 0:557a92280097 | 116 | /** Get the current power mode of the LM75B |
neilt6 | 0:557a92280097 | 117 | * |
neilt6 | 6:6b8a9d1ad49a | 118 | * @returns The current power mode as a PowerMode enum. |
neilt6 | 0:557a92280097 | 119 | */ |
neilt6 | 8:2b797c309258 | 120 | LM75B::PowerMode powerMode(void); |
neilt6 | 2:9ecc39b2ca70 | 121 | |
neilt6 | 0:557a92280097 | 122 | /** Set the power mode of the LM75B |
neilt6 | 0:557a92280097 | 123 | * |
neilt6 | 6:6b8a9d1ad49a | 124 | * @param mode The new power mode as a PowerMode enum. |
neilt6 | 0:557a92280097 | 125 | */ |
neilt6 | 8:2b797c309258 | 126 | void powerMode(PowerMode mode); |
neilt6 | 2:9ecc39b2ca70 | 127 | |
neilt6 | 0:557a92280097 | 128 | /** Get the current OS pin mode of the LM75B |
neilt6 | 0:557a92280097 | 129 | * |
neilt6 | 6:6b8a9d1ad49a | 130 | * @returns The current OS pin mode as an OSMode enum. |
neilt6 | 0:557a92280097 | 131 | */ |
neilt6 | 8:2b797c309258 | 132 | LM75B::OSMode osMode(void); |
neilt6 | 2:9ecc39b2ca70 | 133 | |
neilt6 | 0:557a92280097 | 134 | /** Set the OS pin mode of the LM75B |
neilt6 | 0:557a92280097 | 135 | * |
neilt6 | 6:6b8a9d1ad49a | 136 | * @param mode The new OS pin mode as an OSMode enum. |
neilt6 | 0:557a92280097 | 137 | */ |
neilt6 | 8:2b797c309258 | 138 | void osMode(OSMode mode); |
neilt6 | 2:9ecc39b2ca70 | 139 | |
neilt6 | 0:557a92280097 | 140 | /** Get the current OS pin polarity of the LM75B |
neilt6 | 0:557a92280097 | 141 | * |
neilt6 | 6:6b8a9d1ad49a | 142 | * @returns The current OS pin polarity as an OSPolarity enum. |
neilt6 | 0:557a92280097 | 143 | */ |
neilt6 | 8:2b797c309258 | 144 | LM75B::OSPolarity osPolarity(void); |
neilt6 | 2:9ecc39b2ca70 | 145 | |
neilt6 | 0:557a92280097 | 146 | /** Set the OS pin polarity of the LM75B |
neilt6 | 0:557a92280097 | 147 | * |
neilt6 | 6:6b8a9d1ad49a | 148 | * @param polarity The new OS pin polarity as an OSPolarity enum. |
neilt6 | 0:557a92280097 | 149 | */ |
neilt6 | 8:2b797c309258 | 150 | void osPolarity(OSPolarity polarity); |
neilt6 | 2:9ecc39b2ca70 | 151 | |
neilt6 | 0:557a92280097 | 152 | /** Get the current OS pin fault queue length of the LM75B |
neilt6 | 0:557a92280097 | 153 | * |
neilt6 | 6:6b8a9d1ad49a | 154 | * @returns The current OS pin fault queue length as an OSFaultQueue enum. |
neilt6 | 0:557a92280097 | 155 | */ |
neilt6 | 8:2b797c309258 | 156 | LM75B::OSFaultQueue osFaultQueue(void); |
neilt6 | 2:9ecc39b2ca70 | 157 | |
neilt6 | 0:557a92280097 | 158 | /** Set the OS pin fault queue length of the LM75B |
neilt6 | 0:557a92280097 | 159 | * |
neilt6 | 6:6b8a9d1ad49a | 160 | * @param queue The new OS pin fault queue length as an OSFaultQueue enum. |
neilt6 | 0:557a92280097 | 161 | */ |
neilt6 | 8:2b797c309258 | 162 | void osFaultQueue(OSFaultQueue queue); |
neilt6 | 2:9ecc39b2ca70 | 163 | |
neilt6 | 0:557a92280097 | 164 | /** Get the current alert temperature threshold of the LM75B |
neilt6 | 0:557a92280097 | 165 | * |
neilt6 | 6:6b8a9d1ad49a | 166 | * @returns The current alert temperature threshold in °C. |
neilt6 | 0:557a92280097 | 167 | */ |
neilt6 | 8:2b797c309258 | 168 | float alertTemp(void); |
neilt6 | 2:9ecc39b2ca70 | 169 | |
neilt6 | 0:557a92280097 | 170 | /** Set the alert temperature threshold of the LM75B |
neilt6 | 0:557a92280097 | 171 | * |
neilt6 | 6:6b8a9d1ad49a | 172 | * @param temp The new alert temperature threshold in °C. |
neilt6 | 0:557a92280097 | 173 | */ |
neilt6 | 8:2b797c309258 | 174 | void alertTemp(float temp); |
neilt6 | 2:9ecc39b2ca70 | 175 | |
neilt6 | 0:557a92280097 | 176 | /** Get the current alert temperature hysteresis threshold of the LM75B |
neilt6 | 0:557a92280097 | 177 | * |
neilt6 | 6:6b8a9d1ad49a | 178 | * @returns The current alert temperature hysteresis threshold in °C. |
neilt6 | 0:557a92280097 | 179 | */ |
neilt6 | 8:2b797c309258 | 180 | float alertHyst(void); |
neilt6 | 2:9ecc39b2ca70 | 181 | |
neilt6 | 0:557a92280097 | 182 | /** Set the alert temperature hysteresis threshold of the LM75B |
neilt6 | 0:557a92280097 | 183 | * |
neilt6 | 6:6b8a9d1ad49a | 184 | * @param temp The new alert temperature hysteresis threshold in °C. |
neilt6 | 0:557a92280097 | 185 | */ |
neilt6 | 8:2b797c309258 | 186 | void alertHyst(float temp); |
neilt6 | 2:9ecc39b2ca70 | 187 | |
neilt6 | 0:557a92280097 | 188 | /** Get the current temperature measurement of the LM75B |
neilt6 | 0:557a92280097 | 189 | * |
neilt6 | 6:6b8a9d1ad49a | 190 | * @returns The current temperature measurement in °C. |
neilt6 | 0:557a92280097 | 191 | */ |
neilt6 | 8:2b797c309258 | 192 | float temp(void); |
neilt6 | 0:557a92280097 | 193 | |
neilt6 | 0:557a92280097 | 194 | private: |
neilt6 | 8:2b797c309258 | 195 | //I2C register addresses |
neilt6 | 8:2b797c309258 | 196 | enum Register { |
neilt6 | 8:2b797c309258 | 197 | REG_TEMP = 0x00, |
neilt6 | 8:2b797c309258 | 198 | REG_CONF = 0x01, |
neilt6 | 8:2b797c309258 | 199 | REG_THYST = 0x02, |
neilt6 | 8:2b797c309258 | 200 | REG_TOS = 0x03 |
neilt6 | 8:2b797c309258 | 201 | }; |
neilt6 | 8:2b797c309258 | 202 | |
neilt6 | 8:2b797c309258 | 203 | //Member variables |
neilt6 | 8:2b797c309258 | 204 | I2C m_I2C; |
neilt6 | 8:2b797c309258 | 205 | int m_Addr; |
neilt6 | 8:2b797c309258 | 206 | |
neilt6 | 8:2b797c309258 | 207 | //Internal functions |
neilt6 | 8:2b797c309258 | 208 | char read8(char reg); |
neilt6 | 8:2b797c309258 | 209 | void write8(char reg, char data); |
neilt6 | 8:2b797c309258 | 210 | unsigned short read16(char reg); |
neilt6 | 8:2b797c309258 | 211 | void write16(char reg, unsigned short data); |
neilt6 | 8:2b797c309258 | 212 | float readAlertTempHelper(char reg); |
neilt6 | 8:2b797c309258 | 213 | void writeAlertTempHelper(char reg, float temp); |
neilt6 | 0:557a92280097 | 214 | }; |
neilt6 | 0:557a92280097 | 215 | |
neilt6 | 5:f8b36d66b768 | 216 | #endif |