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

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

Committer:
neilt6
Date:
Wed Jul 31 22:11:31 2013 +0000
Revision:
1:3da8df4319e8
Parent:
0:557a92280097
Child:
2:9ecc39b2ca70
Updated license to MIT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 1:3da8df4319e8 1 /* Copyright (c) 2013 Neil Thiessen, MIT License
neilt6 0:557a92280097 2 *
neilt6 1:3da8df4319e8 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
neilt6 1:3da8df4319e8 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
neilt6 1:3da8df4319e8 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
neilt6 1:3da8df4319e8 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
neilt6 0:557a92280097 7 * furnished to do so, subject to the following conditions:
neilt6 0:557a92280097 8 *
neilt6 1:3da8df4319e8 9 * The above copyright notice and this permission notice shall be included in all copies or
neilt6 1:3da8df4319e8 10 * substantial portions of the Software.
neilt6 0:557a92280097 11 *
neilt6 1:3da8df4319e8 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
neilt6 1:3da8df4319e8 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
neilt6 1:3da8df4319e8 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
neilt6 1:3da8df4319e8 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
neilt6 1:3da8df4319e8 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
neilt6 0:557a92280097 17 */
neilt6 0:557a92280097 18
neilt6 0:557a92280097 19 #ifndef LM75B_H
neilt6 0:557a92280097 20 #define LM75B_H
neilt6 0:557a92280097 21
neilt6 0:557a92280097 22 #include "mbed.h"
neilt6 0:557a92280097 23
neilt6 0:557a92280097 24 //i2c address definitions
neilt6 0:557a92280097 25 #define LM75B_A0 (0x48 << 1)
neilt6 0:557a92280097 26 #define LM75B_A1 (0x49 << 1)
neilt6 0:557a92280097 27 #define LM75B_A2 (0x4A << 1)
neilt6 0:557a92280097 28 #define LM75B_A3 (0x4B << 1)
neilt6 0:557a92280097 29 #define LM75B_A4 (0x4C << 1)
neilt6 0:557a92280097 30 #define LM75B_A5 (0x4D << 1)
neilt6 0:557a92280097 31 #define LM75B_A6 (0x4E << 1)
neilt6 0:557a92280097 32 #define LM75B_A7 (0x4F << 1)
neilt6 0:557a92280097 33
neilt6 0:557a92280097 34 //i2c register definitions
neilt6 0:557a92280097 35 #define LM75B_REG_TEMP 0x00
neilt6 0:557a92280097 36 #define LM75B_REG_CONF 0x01
neilt6 0:557a92280097 37 #define LM75B_REG_THYST 0x02
neilt6 0:557a92280097 38 #define LM75B_REG_TOS 0x03
neilt6 0:557a92280097 39
neilt6 0:557a92280097 40 //Power mode enumeration
neilt6 0:557a92280097 41 typedef enum {
neilt6 0:557a92280097 42 LM75B_Power_Normal,
neilt6 0:557a92280097 43 LM75B_Power_Shutdown
neilt6 0:557a92280097 44 } LM75B_Power_t;
neilt6 0:557a92280097 45
neilt6 0:557a92280097 46 //OS mode enumeration
neilt6 0:557a92280097 47 typedef enum {
neilt6 0:557a92280097 48 LM75B_OS_Comparator,
neilt6 0:557a92280097 49 LM75B_OS_Interrupt
neilt6 0:557a92280097 50 } LM75B_OS_Mode_t;
neilt6 0:557a92280097 51
neilt6 0:557a92280097 52 //OS polarity enumeration
neilt6 0:557a92280097 53 typedef enum {
neilt6 0:557a92280097 54 LM75B_OS_ActiveLow,
neilt6 0:557a92280097 55 LM75B_OS_ActiveHigh
neilt6 0:557a92280097 56 } LM75B_OS_Polarity_t;
neilt6 0:557a92280097 57
neilt6 0:557a92280097 58 //OS fault queue enumeration
neilt6 0:557a92280097 59 typedef enum {
neilt6 0:557a92280097 60 LM75B_OS_FaultQueue_1,
neilt6 0:557a92280097 61 LM75B_OS_FaultQueue_2,
neilt6 0:557a92280097 62 LM75B_OS_FaultQueue_4,
neilt6 0:557a92280097 63 LM75B_OS_FaultQueue_6
neilt6 0:557a92280097 64 } LM75B_OS_FaultQueue_t;
neilt6 0:557a92280097 65
neilt6 0:557a92280097 66 /** LM75B class.
neilt6 0:557a92280097 67 * Used for controlling an LM75B temperature sensor connected via I2C.
neilt6 0:557a92280097 68 *
neilt6 0:557a92280097 69 * Example:
neilt6 0:557a92280097 70 * @code
neilt6 0:557a92280097 71 * #include "mbed.h"
neilt6 0:557a92280097 72 * #include "LM75B.h"
neilt6 0:557a92280097 73 *
neilt6 0:557a92280097 74 * Serial pc(USBTX, USBRX);
neilt6 0:557a92280097 75 * LM75B sensor(p28, p27, LM75B_A0);
neilt6 0:557a92280097 76 *
neilt6 0:557a92280097 77 * int main() {
neilt6 0:557a92280097 78 * while (1) {
neilt6 0:557a92280097 79 * //Read the temperature
neilt6 0:557a92280097 80 * float temp = sensor.getTemp();
neilt6 0:557a92280097 81 *
neilt6 0:557a92280097 82 * //Print the temperature
neilt6 0:557a92280097 83 * pc.printf("Temp = %.3f\n", temp);
neilt6 0:557a92280097 84 *
neilt6 0:557a92280097 85 * //Sleep for 0.5 seconds
neilt6 0:557a92280097 86 * wait(0.5);
neilt6 0:557a92280097 87 * }
neilt6 0:557a92280097 88 * }
neilt6 0:557a92280097 89 * @endcode
neilt6 0:557a92280097 90 */
neilt6 0:557a92280097 91 class LM75B
neilt6 0:557a92280097 92 {
neilt6 0:557a92280097 93 public:
neilt6 0:557a92280097 94 /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address
neilt6 0:557a92280097 95 *
neilt6 0:557a92280097 96 * @param sda I2C data pin
neilt6 0:557a92280097 97 * @param scl I2C clock pin
neilt6 0:557a92280097 98 * @param addr I2C slave address
neilt6 0:557a92280097 99 */
neilt6 0:557a92280097 100 LM75B(PinName sda, PinName scl, int addr);
neilt6 0:557a92280097 101
neilt6 0:557a92280097 102 /** Get the current power mode of the LM75B
neilt6 0:557a92280097 103 *
neilt6 0:557a92280097 104 * @returns The current power mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 105 */
neilt6 0:557a92280097 106 LM75B_Power_t getPowerMode(void);
neilt6 0:557a92280097 107
neilt6 0:557a92280097 108 /** Set the power mode of the LM75B
neilt6 0:557a92280097 109 *
neilt6 0:557a92280097 110 * @param mode The new power mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 111 */
neilt6 0:557a92280097 112 void setPowerMode(LM75B_Power_t mode);
neilt6 0:557a92280097 113
neilt6 0:557a92280097 114 /** Get the current OS pin mode of the LM75B
neilt6 0:557a92280097 115 *
neilt6 0:557a92280097 116 * @returns The current OS pin mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 117 */
neilt6 0:557a92280097 118 LM75B_OS_Mode_t getOSMode(void);
neilt6 0:557a92280097 119
neilt6 0:557a92280097 120 /** Set the OS pin mode of the LM75B
neilt6 0:557a92280097 121 *
neilt6 0:557a92280097 122 * @param mode The new OS pin mode as an LM75B_OS_Mode_t enum.
neilt6 0:557a92280097 123 */
neilt6 0:557a92280097 124 void setOSMode(LM75B_OS_Mode_t mode);
neilt6 0:557a92280097 125
neilt6 0:557a92280097 126 /** Get the current OS pin polarity of the LM75B
neilt6 0:557a92280097 127 *
neilt6 0:557a92280097 128 * @returns The current OS pin polarity as an LM75B_OS_Polarity_t enum.
neilt6 0:557a92280097 129 */
neilt6 0:557a92280097 130 LM75B_OS_Polarity_t getOSPolarity(void);
neilt6 0:557a92280097 131
neilt6 0:557a92280097 132 /** Set the OS pin polarity of the LM75B
neilt6 0:557a92280097 133 *
neilt6 0:557a92280097 134 * @param polarity The new OS pin polarity as an LM75B_OS_Polarity_t enum.
neilt6 0:557a92280097 135 */
neilt6 0:557a92280097 136 void setOSPolarity(LM75B_OS_Polarity_t polarity);
neilt6 0:557a92280097 137
neilt6 0:557a92280097 138 /** Get the current OS pin fault queue length of the LM75B
neilt6 0:557a92280097 139 *
neilt6 0:557a92280097 140 * @returns The current OS pin fault queue length as an LM75B_OS_FaultQueue_t enum.
neilt6 0:557a92280097 141 */
neilt6 0:557a92280097 142 LM75B_OS_FaultQueue_t getOSFaultQueue(void);
neilt6 0:557a92280097 143
neilt6 0:557a92280097 144 /** Set the OS pin fault queue length of the LM75B
neilt6 0:557a92280097 145 *
neilt6 0:557a92280097 146 * @param queue The new OS pin fault queue length as an LM75B_OS_FaultQueue_t enum.
neilt6 0:557a92280097 147 */
neilt6 0:557a92280097 148 void setOSFaultQueue(LM75B_OS_FaultQueue_t queue);
neilt6 0:557a92280097 149
neilt6 0:557a92280097 150 /** Get the current alert temperature threshold of the LM75B
neilt6 0:557a92280097 151 *
neilt6 0:557a92280097 152 * @returns The current alert temperature threshold as a float.
neilt6 0:557a92280097 153 */
neilt6 0:557a92280097 154 float getAlertTemp(void);
neilt6 0:557a92280097 155
neilt6 0:557a92280097 156 /** Set the alert temperature threshold of the LM75B
neilt6 0:557a92280097 157 *
neilt6 0:557a92280097 158 * @param temp The new alert temperature threshold as a float.
neilt6 0:557a92280097 159 */
neilt6 0:557a92280097 160 void setAlertTemp(float temp);
neilt6 0:557a92280097 161
neilt6 0:557a92280097 162 /** Get the current alert temperature hysteresis threshold of the LM75B
neilt6 0:557a92280097 163 *
neilt6 0:557a92280097 164 * @returns The current alert temperature hysteresis threshold as a float.
neilt6 0:557a92280097 165 */
neilt6 0:557a92280097 166 float getAlertHyst(void);
neilt6 0:557a92280097 167
neilt6 0:557a92280097 168 /** Set the alert temperature hysteresis threshold of the LM75B
neilt6 0:557a92280097 169 *
neilt6 0:557a92280097 170 * @param temp The new alert temperature hysteresis threshold as a float.
neilt6 0:557a92280097 171 */
neilt6 0:557a92280097 172 void setAlertHyst(float temp);
neilt6 0:557a92280097 173
neilt6 0:557a92280097 174 /** Get the current temperature measurement of the LM75B
neilt6 0:557a92280097 175 *
neilt6 0:557a92280097 176 * @returns The current temperature measurement as a float.
neilt6 0:557a92280097 177 */
neilt6 0:557a92280097 178 float getTemp(void);
neilt6 0:557a92280097 179
neilt6 0:557a92280097 180 private:
neilt6 0:557a92280097 181 I2C _i2c;
neilt6 0:557a92280097 182 int _addr;
neilt6 0:557a92280097 183 char _read8(char reg);
neilt6 0:557a92280097 184 void _write8(char reg, char data);
neilt6 0:557a92280097 185 unsigned short _read16(char reg);
neilt6 0:557a92280097 186 void _write16(char reg, unsigned short data);
neilt6 0:557a92280097 187 float _readTempHelper(char reg);
neilt6 0:557a92280097 188 void _writeTempHelper(char reg, float temp);
neilt6 0:557a92280097 189 };
neilt6 0:557a92280097 190
neilt6 0:557a92280097 191 #endif