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

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

LM75B.h

Committer:
neilt6
Date:
2013-08-01
Revision:
2:9ecc39b2ca70
Parent:
1:3da8df4319e8
Child:
3:9d68eed28bfb

File content as of revision 2:9ecc39b2ca70:

/* 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
 * furnished to do so, subject to the following conditions:
 *
 * 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,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef LM75B_H
#define LM75B_H

#include "mbed.h"

//i2c register definitions
#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.
 *
 * Example:
 * @code
 * #include "mbed.h"
 * #include "LM75B.h"
 *
 * Serial pc(USBTX, USBRX);
 * LM75B sensor(p28, p27, LM75B::ADDRESS_0);
 *
 * int main() {
 *     while (1) {
 *         //Read the temperature
 *         float temp = sensor.getTemp();
 *
 *         //Print the temperature
 *         pc.printf("Temp = %.3f\n", temp);
 *
 *         //Sleep for 0.5 seconds
 *         wait(0.5);
 *     }
 * }
 * @endcode
 */
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, Address addr);

    /** Get the current power mode of the LM75B
     *
     * @returns The current power mode as an LM75B_OS_Mode_t enum.
     */
    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(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::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(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::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(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::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(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.
     */
    float getTemp(void);

private:
    I2C _i2c;
    int _addr;
    char _read8(char reg);
    void _write8(char reg, char data);
    unsigned short _read16(char reg);
    void _write16(char reg, unsigned short data);
    float _readTempHelper(char reg);
    void _writeTempHelper(char reg, float temp);
};

#endif