A feature complete driver for the LM75B temperature sensor from NXP.
Dependents: app-board-TempAlarm LM75B IoTWorkshopSensors EduRobot ... more
LM75B.h
00001 /* LM75B Driver Library 00002 * Copyright (c) 2013 Neil Thiessen 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef LM75B_H 00018 #define LM75B_H 00019 00020 #include "mbed.h" 00021 00022 /** LM75B class. 00023 * Used for controlling an LM75B temperature sensor connected via I2C. 00024 * 00025 * Example: 00026 * @code 00027 * #include "mbed.h" 00028 * #include "LM75B.h" 00029 * 00030 * //Create an LM75B object at the default address (ADDRESS_0) 00031 * LM75B sensor(p28, p27); 00032 * 00033 * int main() 00034 * { 00035 * //Try to open the LM75B 00036 * if (sensor.open()) { 00037 * printf("Device detected!\n"); 00038 * 00039 * while (1) { 00040 * //Print the current temperature 00041 * printf("Temp = %.3f\n", (float)sensor); 00042 * 00043 * //Sleep for 0.5 seconds 00044 * wait(0.5); 00045 * } 00046 * } else { 00047 * error("Device not detected!\n"); 00048 * } 00049 * } 00050 * @endcode 00051 */ 00052 class LM75B 00053 { 00054 public: 00055 /** Represents the different I2C address possibilities for the LM75B 00056 */ 00057 enum Address { 00058 ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */ 00059 ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */ 00060 ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */ 00061 ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */ 00062 ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */ 00063 ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */ 00064 ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */ 00065 ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */ 00066 }; 00067 00068 /** Represents the power mode of the LM75B 00069 */ 00070 enum PowerMode { 00071 POWER_NORMAL, /**< Chip is enabled and samples every 100ms */ 00072 POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */ 00073 }; 00074 00075 /** Represents OS pin mode of the LM75B 00076 */ 00077 enum OSMode { 00078 OS_COMPARATOR, /**< OS is asserted when the temperature reaches the alert threshold, and de-asserted when the temperature drops below the alert hysteresis threshold */ 00079 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 */ 00080 }; 00081 00082 /** Represents OS pin polarity of the LM75B 00083 */ 00084 enum OSPolarity { 00085 OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */ 00086 OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */ 00087 }; 00088 00089 /** Represents OS pin fault queue length of the LM75B 00090 */ 00091 enum OSFaultQueue { 00092 OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */ 00093 OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */ 00094 OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */ 00095 OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */ 00096 }; 00097 00098 /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address 00099 * 00100 * @param sda The I2C data pin. 00101 * @param scl The I2C clock pin. 00102 * @param addr The I2C slave address (defaults to ADDRESS_0). 00103 * @param hz The I2C bus frequency (defaults to 400kHz). 00104 */ 00105 LM75B(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000); 00106 00107 /** Probe for the LM75B and indicate if it's present on the bus 00108 * 00109 * @returns 00110 * 'true' if the device exists on the bus, 00111 * 'false' if the device doesn't exist on the bus. 00112 */ 00113 bool open(); 00114 00115 /** Get the current power mode of the LM75B 00116 * 00117 * @returns The current power mode as a PowerMode enum. 00118 */ 00119 LM75B::PowerMode powerMode(); 00120 00121 /** Set the power mode of the LM75B 00122 * 00123 * @param mode The new power mode as a PowerMode enum. 00124 */ 00125 void powerMode(PowerMode mode); 00126 00127 /** Get the current OS pin mode of the LM75B 00128 * 00129 * @returns The current OS pin mode as an OSMode enum. 00130 */ 00131 LM75B::OSMode osMode(); 00132 00133 /** Set the OS pin mode of the LM75B 00134 * 00135 * @param mode The new OS pin mode as an OSMode enum. 00136 */ 00137 void osMode(OSMode mode); 00138 00139 /** Get the current OS pin polarity of the LM75B 00140 * 00141 * @returns The current OS pin polarity as an OSPolarity enum. 00142 */ 00143 LM75B::OSPolarity osPolarity(); 00144 00145 /** Set the OS pin polarity of the LM75B 00146 * 00147 * @param polarity The new OS pin polarity as an OSPolarity enum. 00148 */ 00149 void osPolarity(OSPolarity polarity); 00150 00151 /** Get the current OS pin fault queue length of the LM75B 00152 * 00153 * @returns The current OS pin fault queue length as an OSFaultQueue enum. 00154 */ 00155 LM75B::OSFaultQueue osFaultQueue(); 00156 00157 /** Set the OS pin fault queue length of the LM75B 00158 * 00159 * @param queue The new OS pin fault queue length as an OSFaultQueue enum. 00160 */ 00161 void osFaultQueue(OSFaultQueue queue); 00162 00163 /** Get the current alert temperature threshold of the LM75B 00164 * 00165 * @returns The current alert temperature threshold in °C. 00166 */ 00167 float alertTemp(); 00168 00169 /** Set the alert temperature threshold of the LM75B 00170 * 00171 * @param temp The new alert temperature threshold in °C. 00172 */ 00173 void alertTemp(float temp); 00174 00175 /** Get the current alert temperature hysteresis threshold of the LM75B 00176 * 00177 * @returns The current alert temperature hysteresis threshold in °C. 00178 */ 00179 float alertHyst(); 00180 00181 /** Set the alert temperature hysteresis threshold of the LM75B 00182 * 00183 * @param temp The new alert temperature hysteresis threshold in °C. 00184 */ 00185 void alertHyst(float temp); 00186 00187 /** Get the current temperature measurement of the LM75B 00188 * 00189 * @returns The current temperature measurement in °C. 00190 */ 00191 float temp(); 00192 00193 #ifdef MBED_OPERATORS 00194 /** A shorthand for temp() 00195 * 00196 * @returns The current temperature measurement in °C. 00197 */ 00198 operator float(); 00199 #endif 00200 00201 private: 00202 //I2C register addresses 00203 enum Register { 00204 REG_TEMP = 0x00, 00205 REG_CONF = 0x01, 00206 REG_THYST = 0x02, 00207 REG_TOS = 0x03 00208 }; 00209 00210 //Member variables 00211 I2C m_I2C; 00212 const int m_ADDR; 00213 00214 //Internal functions 00215 char read8(char reg); 00216 void write8(char reg, char data); 00217 unsigned short read16(char reg); 00218 void write16(char reg, unsigned short data); 00219 float readAlertTempHelper(char reg); 00220 void writeAlertTempHelper(char reg, float temp); 00221 }; 00222 00223 #endif
Generated on Tue Jul 12 2022 18:33:55 by
