Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LM75B by
Revision 2:cc474eead249, committed 2016-02-24
- Comitter:
- co838_ok87
- Date:
- Wed Feb 24 15:50:09 2016 +0000
- Parent:
- 1:6a70c9303bbe
- Commit message:
- ok87
; changes to lm75b from chris styles.
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 |
diff -r 6a70c9303bbe -r cc474eead249 LM75B.cpp --- a/LM75B.cpp Fri Oct 26 21:40:51 2012 +0000 +++ b/LM75B.cpp Wed Feb 24 15:50:09 2016 +0000 @@ -1,26 +1,283 @@ +/* LM75B Driver Library + * Copyright (c) 2013 Neil Thiessen + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "LM75B.h" -LM75B::LM75B(PinName sda, PinName scl) : i2c(sda, scl) +LM75B::LM75B(PinName sda, PinName scl, Address addr) : m_I2C(sda, scl) +{ + //Set the internal device address + m_Addr = (int)addr; +} + +bool LM75B::open(void) +{ + //Probe for the LM75B using a Zero Length Transfer + if (!m_I2C.write(m_Addr, NULL, 0)) { + //Reset the device to default configuration + write8(REG_CONF, 0x00); + write16(REG_THYST, 0x4B00); + write16(REG_TOS, 0x5000); + + //Return success + return true; + } else { + //Return failure + return false; + } +} + +LM75B::PowerMode LM75B::powerMode(void) +{ + //Read the 8-bit register value + char value = read8(REG_CONF); + + //Return the status of the SHUTDOWN bit + if (value & (1 << 0)) + return POWER_SHUTDOWN; + else + return POWER_NORMAL; +} + +void LM75B::powerMode(PowerMode mode) +{ + //Read the current 8-bit register value + char value = read8(REG_CONF); + + //Set or clear the SHUTDOWN bit + if (mode == POWER_SHUTDOWN) + value |= (1 << 0); + else + value &= ~(1 << 0); + + //Write the value back out + write8(REG_CONF, value); +} + +LM75B::OSMode LM75B::osMode(void) { - char cmd[2]; - cmd[0] = LM75B_Conf; - cmd[1] = 0x0; - i2c.write( LM75B_ADDR, cmd, 2); + //Read the 8-bit register value + char value = read8(REG_CONF); + + //Return the status of the OS_COMP_INT bit + if (value & (1 << 1)) + return OS_INTERRUPT; + else + return OS_COMPARATOR; +} + +void LM75B::osMode(OSMode mode) +{ + //Read the current 8-bit register value + char value = read8(REG_CONF); + + //Set or clear the OS_COMP_INT bit + if (mode == OS_INTERRUPT) + value |= (1 << 1); + else + value &= ~(1 << 1); + + //Write the value back out + write8(REG_CONF, value); +} + +LM75B::OSPolarity LM75B::osPolarity(void) +{ + //Read the 8-bit register value + char value = read8(REG_CONF); + + //Return the status of the OS_POL bit + if (value & (1 << 2)) + return OS_ACTIVE_HIGH; + else + return OS_ACTIVE_LOW; +} + +void LM75B::osPolarity(OSPolarity polarity) +{ + //Read the current 8-bit register value + char value = read8(REG_CONF); + + //Set or clear the OS_POL bit + if (polarity == OS_ACTIVE_HIGH) + value |= (1 << 2); + else + value &= ~(1 << 2); + + //Write the value back out + write8(REG_CONF, value); +} + +LM75B::OSFaultQueue LM75B::osFaultQueue(void) +{ + //Read the 8-bit register value + char value = read8(REG_CONF); + + //Return the status of the OS_F_QUE bits + if ((value & (1 << 3)) && (value & (1 << 4))) + return OS_FAULT_QUEUE_6; + else if (!(value & (1 << 3)) && (value & (1 << 4))) + return OS_FAULT_QUEUE_4; + else if ((value & (1 << 3)) && !(value & (1 << 4))) + return OS_FAULT_QUEUE_2; + else + return OS_FAULT_QUEUE_1; } +void LM75B::osFaultQueue(OSFaultQueue queue) +{ + //Read the current 8-bit register value + char value = read8(REG_CONF); + //Clear the old OS_F_QUE bits + value &= ~(3 << 3); + + //Set the new OS_F_QUE bits + if (queue == OS_FAULT_QUEUE_2) + value |= (1 << 3); + else if (queue == OS_FAULT_QUEUE_4) + value |= (2 << 3); + else if (queue == OS_FAULT_QUEUE_6) + value |= (3 << 3); + + //Write the value back out + write8(REG_CONF, value); +} + +float LM75B::alertTemp(void) +{ + //Use the 9-bit helper to read the TOS register + return readAlertTempHelper(REG_TOS); +} + +void LM75B::alertTemp(float temp) +{ + //Use the 9-bit helper to write to the TOS register + return writeAlertTempHelper(REG_TOS, temp); +} -LM75B::~LM75B() +float LM75B::alertHyst(void) +{ + //Use the 9-bit helper to read the THYST register + return readAlertTempHelper(REG_THYST); +} + +void LM75B::alertHyst(float temp) +{ + //Use the 9-bit helper to write to the THYST register + return writeAlertTempHelper(REG_THYST, temp); +} + +float LM75B::temp(void) { + //Signed return value + short value; + //Read the 11-bit raw temperature value + value = read16(REG_TEMP) >> 5; + + //Sign extend negative numbers + if (value & (1 << 10)) + value |= 0xFC00; + + //Return the temperature in °C + return value * 0.125; +} + +char LM75B::read8(char reg) +{ + //Select the register + m_I2C.write(m_Addr, ®, 1); + + //Read the 8-bit register + m_I2C.read(m_Addr, ®, 1); + + //Return the byte + return reg; } -float LM75B::read() +void LM75B::write8(char reg, char data) +{ + //Create a temporary buffer + char buff[2]; + + //Load the register address and 8-bit data + buff[0] = reg; + buff[1] = data; + + //Write the data + m_I2C.write(m_Addr, buff, 2); +} + +unsigned short LM75B::read16(char reg) +{ + //Create a temporary buffer + char buff[2]; + + //Select the register + m_I2C.write(m_Addr, ®, 1); + + //Read the 16-bit register + m_I2C.read(m_Addr, buff, 2); + + //Return the combined 16-bit value + return (buff[0] << 8) | buff[1]; +} + +void LM75B::write16(char reg, unsigned short data) { - char cmd[2]; - cmd[0] = LM75B_Temp; - - i2c.write( LM75B_ADDR, cmd, 1); // Send command string - i2c.read( LM75B_ADDR, cmd, 2); // Send command string - return ( float((cmd[0]<<8)|cmd[1]) / 256.0 ); + //Create a temporary buffer + char buff[3]; + + //Load the register address and 16-bit data + buff[0] = reg; + buff[1] = data >> 8; + buff[2] = data; + + //Write the data + m_I2C.write(m_Addr, buff, 3); } + +float LM75B::readAlertTempHelper(char reg) +{ + //Signed return value + short value; + + //Read the 9-bit raw temperature value + value = read16(reg) >> 7; + + //Sign extend negative numbers + if (value & (1 << 8)) + value |= 0xFF00; + + //Return the temperature in °C + return value * 0.5; +} + +void LM75B::writeAlertTempHelper(char reg, float temp) +{ + //Range limit temp + if (temp < -55.0) + temp = -55.0; + else if (temp > 125.0) + temp = 125.0; + + //Extract and shift the signed integer + short value = temp * 2; + value <<= 7; + + //Send the new value + write16(reg, value); +}
diff -r 6a70c9303bbe -r cc474eead249 LM75B.h --- a/LM75B.h Fri Oct 26 21:40:51 2012 +0000 +++ b/LM75B.h Wed Feb 24 15:50:09 2016 +0000 @@ -1,19 +1,17 @@ -/* Copyright (c) 2012 cstyles, MIT License +/* LM75B Driver Library + * Copyright (c) 2013 Neil Thiessen * - * 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: + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. + * http://www.apache.org/licenses/LICENSE-2.0 * - * 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. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef LM75B_H @@ -21,44 +19,206 @@ #include "mbed.h" -// LM75B IIC address -#define LM75B_ADDR 0x90 - -// LM75B registers -#define LM75B_Conf 0x01 -#define LM75B_Temp 0x00 -#define LM75B_Tos 0x03 -#define LM75B_Thyst 0x02 - -//!Library for the LM75B temperature sensor. -/*! -The LM75B is an I2C digital temperature sensor, with a range of -55C to +125C and a 0.125C resolution. -*/ +/** LM75B class. + * Used for controlling an LM75B temperature sensor connected via I2C. + * + * Example: + * @code + * #include "mbed.h" + * #include "LM75B.h" + * + * //Create an LM75B object at the default address (ADDRESS_0) + * LM75B sensor(p28, p27); + * + * int main() + * { + * //Try to open the LM75B + * if (sensor.open()) { + * printf("Device detected!\n"); + * + * while (1) { + * //Print the current temperature + * printf("Temp = %.3f\n", (float)sensor); + * + * //Sleep for 0.5 seconds + * wait(0.5); + * } + * } else { + * error("Device not detected!\n"); + * } + * } + * @endcode + */ class LM75B { public: - //!Creates an instance of the class. - /*! - Connect module at I2C address addr using I2C port pins sda and scl. - LM75B - */ - LM75B(PinName sda, PinName scl); - - /*! - Destroys instance. - */ - ~LM75B(); - - //!Reads the current temperature. - /*! - Reads the temperature register of the LM75B and converts it to a useable value. - */ - float read(); - + /** 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 The I2C data pin. + * @param scl The I2C clock pin. + * @param addr The I2C slave address (defaults to ADDRESS_0). + */ + LM75B(PinName sda, PinName scl, Address addr = ADDRESS_0); + + /** Probe for the LM75B and reset it to default configuration if present + * + * @returns + * 'true' if the device exists on the bus, + * 'false' if the device doesn't exist on the bus. + */ + bool open(void); + + /** Get the current power mode of the LM75B + * + * @returns The current power mode as a PowerMode enum. + */ + LM75B::PowerMode powerMode(void); + + /** Set the power mode of the LM75B + * + * @param mode The new power mode as a PowerMode enum. + */ + void powerMode(PowerMode mode); + + /** Get the current OS pin mode of the LM75B + * + * @returns The current OS pin mode as an OSMode enum. + */ + LM75B::OSMode osMode(void); + + /** Set the OS pin mode of the LM75B + * + * @param mode The new OS pin mode as an OSMode enum. + */ + void osMode(OSMode mode); + + /** Get the current OS pin polarity of the LM75B + * + * @returns The current OS pin polarity as an OSPolarity enum. + */ + LM75B::OSPolarity osPolarity(void); + + /** Set the OS pin polarity of the LM75B + * + * @param polarity The new OS pin polarity as an OSPolarity enum. + */ + void osPolarity(OSPolarity polarity); + + /** Get the current OS pin fault queue length of the LM75B + * + * @returns The current OS pin fault queue length as an OSFaultQueue enum. + */ + LM75B::OSFaultQueue osFaultQueue(void); + + /** Set the OS pin fault queue length of the LM75B + * + * @param queue The new OS pin fault queue length as an OSFaultQueue enum. + */ + void osFaultQueue(OSFaultQueue queue); + + /** Get the current alert temperature threshold of the LM75B + * + * @returns The current alert temperature threshold in °C. + */ + float alertTemp(void); + + /** Set the alert temperature threshold of the LM75B + * + * @param temp The new alert temperature threshold in °C. + */ + void alertTemp(float temp); + + /** Get the current alert temperature hysteresis threshold of the LM75B + * + * @returns The current alert temperature hysteresis threshold in °C. + */ + float alertHyst(void); + + /** Set the alert temperature hysteresis threshold of the LM75B + * + * @param temp The new alert temperature hysteresis threshold in °C. + */ + void alertHyst(float temp); + + /** Get the current temperature measurement of the LM75B + * + * @returns The current temperature measurement in °C. + */ + float temp(void); + +#ifdef MBED_OPERATORS + /** A shorthand for temp() + * + * @returns The current temperature measurement in °C. + */ + operator float() { + return temp(); + } +#endif + private: - - I2C i2c; + //I2C register addresses + enum Register { + REG_TEMP = 0x00, + REG_CONF = 0x01, + REG_THYST = 0x02, + REG_TOS = 0x03 + }; + //Member variables + I2C m_I2C; + int m_Addr; + + //Internal functions + char read8(char reg); + void write8(char reg, char data); + unsigned short read16(char reg); + void write16(char reg, unsigned short data); + float readAlertTempHelper(char reg); + void writeAlertTempHelper(char reg, float temp); }; -#endif \ No newline at end of file +#endif