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

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

LM75B.cpp

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.
 */

#include "LM75B.h"
#include "mbed.h"

LM75B::LM75B(PinName sda, PinName scl, Address addr) : _i2c(sda, scl)
{
    //Set the internal device address
    _addr = (int)addr;
}

LM75B::PowerMode LM75B::getPowerMode(void)
{
    //Read the 8-bit register value
    char value = _read8(__LM75B_REG_CONF);

    //Return the status of the SHUTDOWN bit
    if (value & (1 << 0))
        return POWER_SHUTDOWN;
    else
        return POWER_NORMAL;
}

void LM75B::setPowerMode(PowerMode mode)
{
    //Read the current 8-bit register value
    char value = _read8(__LM75B_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(__LM75B_REG_CONF, value);
}

LM75B::OSMode LM75B::getOSMode(void)
{
    //Read the 8-bit register value
    char value = _read8(__LM75B_REG_CONF);

    //Return the status of the OS_COMP_INT bit
    if (value & (1 << 1))
        return OS_INTERRUPT;
    else
        return OS_COMPARATOR;
}

void LM75B::setOSMode(OSMode mode)
{
    //Read the current 8-bit register value
    char value = _read8(__LM75B_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(__LM75B_REG_CONF, value);
}

LM75B::OSPolarity LM75B::getOSPolarity(void)
{
    //Read the 8-bit register value
    char value = _read8(__LM75B_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::setOSPolarity(OSPolarity polarity)
{
    //Read the current 8-bit register value
    char value = _read8(__LM75B_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(__LM75B_REG_CONF, value);
}

LM75B::OSFaultQueue LM75B::getOSFaultQueue(void)
{
    //Read the 8-bit register value
    char value = _read8(__LM75B_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::setOSFaultQueue(OSFaultQueue queue)
{
    //Read the current 8-bit register value
    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 == 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(__LM75B_REG_CONF, value);
}

float LM75B::getAlertTemp(void)
{
    //Use the 9-bit helper to read the TOS register
    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);
}

float LM75B::getAlertHyst(void)
{
    //Use the 9-bit helper to read the THYST register
    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);
}

float LM75B::getTemp(void)
{
    //Signed return value
    short value;

    //Read the 11-bit raw temperature value
    value = _read16(__LM75B_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
    _i2c.write(_addr, &reg, 1);

    //Read the 8-bit register
    _i2c.read(_addr, &reg, 1);

    //Return the byte
    return reg;
}

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
    _i2c.write(_addr, buff, 2);
}

unsigned short LM75B::_read16(char reg)
{
    //Create a temporary buffer
    char buff[2];

    //Select the register
    _i2c.write(_addr, &reg, 1);

    //Read the 16-bit register
    _i2c.read(_addr, buff, 2);

    //Return the combined 16-bit value
    return (buff[0] << 8) | buff[1];
}

void LM75B::_write16(char reg, unsigned short data)
{
    //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
    _i2c.write(_addr, buff, 3);
}

float LM75B::_readTempHelper(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::_writeTempHelper(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);
}