A feature complete driver for the LM75B temperature sensor from NXP.
Dependents: app-board-TempAlarm LM75B IoTWorkshopSensors EduRobot ... more
Revision 2:9ecc39b2ca70, committed 2013-08-01
- Comitter:
- neilt6
- Date:
- Thu Aug 01 16:41:49 2013 +0000
- Parent:
- 1:3da8df4319e8
- Child:
- 3:9d68eed28bfb
- Commit message:
- Improved documentation, relocated enums, changed address macros to an enum
Changed in this revision
LM75B.cpp | Show annotated file Show diff for this revision Revisions of this file |
LM75B.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/LM75B.cpp Wed Jul 31 22:11:31 2013 +0000 +++ b/LM75B.cpp Thu Aug 01 16:41:49 2013 +0000 @@ -1,169 +1,170 @@ -/* Copyright (c) 2013 Neil Thiessen, MIT License +/* LM75B Driver Library + * Copyright (c) 2013 Neil Thiessen, MIT License * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all copies or + * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "LM75B.h" #include "mbed.h" -LM75B::LM75B(PinName sda, PinName scl, int addr) : _i2c(sda, scl) +LM75B::LM75B(PinName sda, PinName scl, Address addr) : _i2c(sda, scl) { //Set the internal device address - _addr = addr; + _addr = (int)addr; } -LM75B_Power_t LM75B::getPowerMode(void) +LM75B::PowerMode LM75B::getPowerMode(void) { //Read the 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Return the status of the SHUTDOWN bit if (value & (1 << 0)) - return LM75B_Power_Shutdown; + return POWER_SHUTDOWN; else - return LM75B_Power_Normal; + return POWER_NORMAL; } -void LM75B::setPowerMode(LM75B_Power_t mode) +void LM75B::setPowerMode(PowerMode mode) { //Read the current 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Set or clear the SHUTDOWN bit - if (mode == LM75B_Power_Shutdown) + if (mode == POWER_SHUTDOWN) value |= (1 << 0); else value &= ~(1 << 0); //Write the value back out - _write8(LM75B_REG_CONF, value); + _write8(__LM75B_REG_CONF, value); } -LM75B_OS_Mode_t LM75B::getOSMode(void) +LM75B::OSMode LM75B::getOSMode(void) { //Read the 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Return the status of the OS_COMP_INT bit if (value & (1 << 1)) - return LM75B_OS_Interrupt; + return OS_INTERRUPT; else - return LM75B_OS_Comparator; + return OS_COMPARATOR; } -void LM75B::setOSMode(LM75B_OS_Mode_t mode) +void LM75B::setOSMode(OSMode mode) { //Read the current 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Set or clear the OS_COMP_INT bit - if (mode == LM75B_OS_Interrupt) + if (mode == OS_INTERRUPT) value |= (1 << 1); else value &= ~(1 << 1); //Write the value back out - _write8(LM75B_REG_CONF, value); + _write8(__LM75B_REG_CONF, value); } -LM75B_OS_Polarity_t LM75B::getOSPolarity(void) +LM75B::OSPolarity LM75B::getOSPolarity(void) { //Read the 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Return the status of the OS_POL bit if (value & (1 << 2)) - return LM75B_OS_ActiveHigh; + return OS_ACTIVE_HIGH; else - return LM75B_OS_ActiveLow; + return OS_ACTIVE_LOW; } -void LM75B::setOSPolarity(LM75B_OS_Polarity_t polarity) +void LM75B::setOSPolarity(OSPolarity polarity) { //Read the current 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Set or clear the OS_POL bit - if (polarity == LM75B_OS_ActiveHigh) + if (polarity == OS_ACTIVE_HIGH) value |= (1 << 2); else value &= ~(1 << 2); //Write the value back out - _write8(LM75B_REG_CONF, value); + _write8(__LM75B_REG_CONF, value); } -LM75B_OS_FaultQueue_t LM75B::getOSFaultQueue(void) +LM75B::OSFaultQueue LM75B::getOSFaultQueue(void) { //Read the 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Return the status of the OS_F_QUE bits if ((value & (1 << 3)) && (value & (1 << 4))) - return LM75B_OS_FaultQueue_6; + return OS_FAULT_QUEUE_6; else if (!(value & (1 << 3)) && (value & (1 << 4))) - return LM75B_OS_FaultQueue_4; + return OS_FAULT_QUEUE_4; else if ((value & (1 << 3)) && !(value & (1 << 4))) - return LM75B_OS_FaultQueue_2; + return OS_FAULT_QUEUE_2; else - return LM75B_OS_FaultQueue_1; + return OS_FAULT_QUEUE_1; } -void LM75B::setOSFaultQueue(LM75B_OS_FaultQueue_t queue) +void LM75B::setOSFaultQueue(OSFaultQueue queue) { //Read the current 8-bit register value - char value = _read8(LM75B_REG_CONF); + char value = _read8(__LM75B_REG_CONF); //Clear the old OS_F_QUE bits value &= ~(3 << 3); //Set the new OS_F_QUE bits - if (queue == LM75B_OS_FaultQueue_2) + if (queue == OS_FAULT_QUEUE_2) value |= (1 << 3); - else if (queue == LM75B_OS_FaultQueue_4) + else if (queue == OS_FAULT_QUEUE_4) value |= (2 << 3); - else if (queue == LM75B_OS_FaultQueue_6) + else if (queue == OS_FAULT_QUEUE_6) value |= (3 << 3); //Write the value back out - _write8(LM75B_REG_CONF, value); + _write8(__LM75B_REG_CONF, value); } float LM75B::getAlertTemp(void) { //Use the 9-bit helper to read the TOS register - return _readTempHelper(LM75B_REG_TOS); + return _readTempHelper(__LM75B_REG_TOS); } void LM75B::setAlertTemp(float temp) { //Use the 9-bit helper to write to the TOS register - return _writeTempHelper(LM75B_REG_TOS, temp); + return _writeTempHelper(__LM75B_REG_TOS, temp); } float LM75B::getAlertHyst(void) { //Use the 9-bit helper to read the THYST register - return _readTempHelper(LM75B_REG_THYST); + return _readTempHelper(__LM75B_REG_THYST); } void LM75B::setAlertHyst(float temp) { //Use the 9-bit helper to write to the THYST register - return _writeTempHelper(LM75B_REG_THYST, temp); + return _writeTempHelper(__LM75B_REG_THYST, temp); } float LM75B::getTemp(void) @@ -172,7 +173,7 @@ short value; //Read the 11-bit raw temperature value - value = _read16(LM75B_REG_TEMP) >> 5; + value = _read16(__LM75B_REG_TEMP) >> 5; //Sign extend negative numbers if (value & (1 << 10))
--- a/LM75B.h Wed Jul 31 22:11:31 2013 +0000 +++ b/LM75B.h Thu Aug 01 16:41:49 2013 +0000 @@ -1,18 +1,19 @@ -/* Copyright (c) 2013 Neil Thiessen, MIT License +/* LM75B Driver Library + * Copyright (c) 2013 Neil Thiessen, MIT License * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all copies or + * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ @@ -21,47 +22,11 @@ #include "mbed.h" -//i2c address definitions -#define LM75B_A0 (0x48 << 1) -#define LM75B_A1 (0x49 << 1) -#define LM75B_A2 (0x4A << 1) -#define LM75B_A3 (0x4B << 1) -#define LM75B_A4 (0x4C << 1) -#define LM75B_A5 (0x4D << 1) -#define LM75B_A6 (0x4E << 1) -#define LM75B_A7 (0x4F << 1) - //i2c register definitions -#define LM75B_REG_TEMP 0x00 -#define LM75B_REG_CONF 0x01 -#define LM75B_REG_THYST 0x02 -#define LM75B_REG_TOS 0x03 - -//Power mode enumeration -typedef enum { - LM75B_Power_Normal, - LM75B_Power_Shutdown -} LM75B_Power_t; - -//OS mode enumeration -typedef enum { - LM75B_OS_Comparator, - LM75B_OS_Interrupt -} LM75B_OS_Mode_t; - -//OS polarity enumeration -typedef enum { - LM75B_OS_ActiveLow, - LM75B_OS_ActiveHigh -} LM75B_OS_Polarity_t; - -//OS fault queue enumeration -typedef enum { - LM75B_OS_FaultQueue_1, - LM75B_OS_FaultQueue_2, - LM75B_OS_FaultQueue_4, - LM75B_OS_FaultQueue_6 -} LM75B_OS_FaultQueue_t; +#define __LM75B_REG_TEMP 0x00 +#define __LM75B_REG_CONF 0x01 +#define __LM75B_REG_THYST 0x02 +#define __LM75B_REG_TOS 0x03 /** LM75B class. * Used for controlling an LM75B temperature sensor connected via I2C. @@ -72,7 +37,7 @@ * #include "LM75B.h" * * Serial pc(USBTX, USBRX); - * LM75B sensor(p28, p27, LM75B_A0); + * LM75B sensor(p28, p27, LM75B::ADDRESS_0); * * int main() { * while (1) { @@ -91,86 +56,129 @@ class LM75B { public: + /** Represents the different I2C address possibilities for the LM75B + */ + enum Address { + ADDRESS_0 = (0x48 << 1), /**< A[2:0] pins = 000 */ + ADDRESS_1 = (0x49 << 1), /**< A[2:0] pins = 001 */ + ADDRESS_2 = (0x4A << 1), /**< A[2:0] pins = 010 */ + ADDRESS_3 = (0x4B << 1), /**< A[2:0] pins = 011 */ + ADDRESS_4 = (0x4C << 1), /**< A[2:0] pins = 100 */ + ADDRESS_5 = (0x4D << 1), /**< A[2:0] pins = 101 */ + ADDRESS_6 = (0x4E << 1), /**< A[2:0] pins = 110 */ + ADDRESS_7 = (0x4F << 1) /**< A[2:0] pins = 111 */ + }; + + /** Represents the power mode of the LM75B + */ + enum PowerMode { + POWER_NORMAL, /**< Chip is enabled and samples every 100ms */ + POWER_SHUTDOWN /**< Chip is in low-power shutdown mode */ + }; + + /** Represents OS pin mode of the LM75B + */ + enum OSMode { + OS_COMPARATOR, /**< OS is asserted when the temperature reaches the alert threshold, and de-asserted when the temperature drops below the alert hysteresis threshold */ + 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 */ + }; + + /** Represents OS pin polarity of the LM75B + */ + enum OSPolarity { + OS_ACTIVE_LOW, /**< OS is a logic low when asserted, and a logic high when de-asserted */ + OS_ACTIVE_HIGH /**< OS is a logic high when asserted, and a logic low when de-asserted */ + }; + + /** Represents OS pin fault queue length of the LM75B + */ + enum OSFaultQueue { + OS_FAULT_QUEUE_1, /**< OS is asserted after 1 fault */ + OS_FAULT_QUEUE_2, /**< OS is asserted after 2 consecutive faults */ + OS_FAULT_QUEUE_4, /**< OS is asserted after 4 consecutive faults */ + OS_FAULT_QUEUE_6 /**< OS is asserted after 6 consecutive faults */ + }; + /** Create an LM75B object connected to the specified I2C pins with the specified I2C slave address * * @param sda I2C data pin * @param scl I2C clock pin * @param addr I2C slave address */ - LM75B(PinName sda, PinName scl, int addr); - + LM75B(PinName sda, PinName scl, Address addr); + /** Get the current power mode of the LM75B * * @returns The current power mode as an LM75B_OS_Mode_t enum. */ - LM75B_Power_t getPowerMode(void); - + LM75B::PowerMode getPowerMode(void); + /** Set the power mode of the LM75B * * @param mode The new power mode as an LM75B_OS_Mode_t enum. */ - void setPowerMode(LM75B_Power_t mode); - + void setPowerMode(PowerMode mode); + /** Get the current OS pin mode of the LM75B * * @returns The current OS pin mode as an LM75B_OS_Mode_t enum. */ - LM75B_OS_Mode_t getOSMode(void); - + LM75B::OSMode getOSMode(void); + /** Set the OS pin mode of the LM75B * * @param mode The new OS pin mode as an LM75B_OS_Mode_t enum. */ - void setOSMode(LM75B_OS_Mode_t mode); - + void setOSMode(OSMode mode); + /** Get the current OS pin polarity of the LM75B * * @returns The current OS pin polarity as an LM75B_OS_Polarity_t enum. */ - LM75B_OS_Polarity_t getOSPolarity(void); - + LM75B::OSPolarity getOSPolarity(void); + /** Set the OS pin polarity of the LM75B * * @param polarity The new OS pin polarity as an LM75B_OS_Polarity_t enum. */ - void setOSPolarity(LM75B_OS_Polarity_t polarity); - + void setOSPolarity(OSPolarity polarity); + /** Get the current OS pin fault queue length of the LM75B * * @returns The current OS pin fault queue length as an LM75B_OS_FaultQueue_t enum. */ - LM75B_OS_FaultQueue_t getOSFaultQueue(void); - + LM75B::OSFaultQueue getOSFaultQueue(void); + /** Set the OS pin fault queue length of the LM75B * * @param queue The new OS pin fault queue length as an LM75B_OS_FaultQueue_t enum. */ - void setOSFaultQueue(LM75B_OS_FaultQueue_t queue); - + void setOSFaultQueue(OSFaultQueue queue); + /** Get the current alert temperature threshold of the LM75B * * @returns The current alert temperature threshold as a float. */ float getAlertTemp(void); - + /** Set the alert temperature threshold of the LM75B * * @param temp The new alert temperature threshold as a float. */ void setAlertTemp(float temp); - + /** Get the current alert temperature hysteresis threshold of the LM75B * * @returns The current alert temperature hysteresis threshold as a float. */ float getAlertHyst(void); - + /** Set the alert temperature hysteresis threshold of the LM75B * * @param temp The new alert temperature hysteresis threshold as a float. */ void setAlertHyst(float temp); - + /** Get the current temperature measurement of the LM75B * * @returns The current temperature measurement as a float.