A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

STTS751.h

Committer:
takuo
Date:
2014-01-19
Revision:
2:3116fe4a0079
Parent:
1:b0a3645a3dba
Child:
3:f9d3008f7e8f

File content as of revision 2:3116fe4a0079:

/* A library for STMicroelectronics STTS751 I2C temperature sensor
 * Copyright 2014, Takuo WATANABE (wtakuo)
 *
 * 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.
 */

#ifndef STTS751_H
#define STTS751_H

#include "mbed.h"

/** A library for STMicroelectronics STTS751 I2C temperature sensor
 * http://www.st.com/web/catalog/sense_power/FM89/SC294/PF220116
 *
 * Example:
 * @code
 * #include "mbed.h"
 * #include "STTS751.h"
 *
 * // I2C pins: p9 = sda, p10 = scl
 * STTS751 temp(p9, p10);
 *
 * // You can specify an I2C object instead.
 * // I2C i2c(p2, p10);
 * // STTS751 temp(i2c);
 * 
 * int main() {
 *     // set the temperature resolution to 12bits
 *     temp.setResolution(STTS751::RES_12);
 *     while (true) {
 *         printf("tmp: %.4f\n", (float)temp);
 *         wait(1);
 *     }
 * }
 * @endcode
 */
class STTS751
{
public:
    /** Device models
     */
    enum Model {
        MODEL_0 = 0x00,  /**< Model 0 (default) */
        MODEL_1 = 0x40   /**< Model 1 */
    };

    /** I2C address selectors
     */
    enum Address {
        ADDRESS_0 = (0x48 << 1), /**< pull up resistor = 7.5K */
        ADDRESS_1 = (0x49 << 1), /**< pull up resistor = 12K */
        ADDRESS_2 = (0x38 << 1), /**< pull up resistor = 20K */
        ADDRESS_3 = (0x39 << 1)  /**< pull up resistor = 33K or GND (default) */
    };

    /** Temperature resolutions
     */
    enum Resolution {
        RES_9  = 0x08, /**< 9 bits */
        RES_10 = 0x00, /**< 10 bits (default) */
        RES_11 = 0x04, /**< 11 bits */
        RES_12 = 0x0c  /**< 12 bits */
    };

    /** Create an STTS751 object connected to the specified I2C pins.
     *
     * @param sda    I2C data pin
     * @param scl    I2C clock pin
     * @param addr   I2C slave address (defaults to ADDRESS_3)
     * @param model  Device model (defaults to MODEL_0)
     */
    STTS751(PinName sda, PinName scl, Address addr = ADDRESS_3, Model model = MODEL_0);

    /** Create an STTS751 object connected to the specified I2C port.
     *
     * @param i2c    I2C port
     * @param addr   I2C slave address (defaults to ADDRESS_3)
     * @param model  Device model (defaults to MODEL_0)
     */
    STTS751(I2C &_i2c, Address addr = ADDRESS_3, Model model = MODEL_0);

    /** Initialize the device
     */
    void init();

    /** The I2C address (8bit address) of the device
     */
    int addr();

    /** Get the current temperature resolution
     *
     * @returns  The current temperature resolution
     */
    Resolution resolution();

    /** Set the temperature resolution
     *
     * @param res  Resolution (defaults to RES_10)
     */
    void setResolution(Resolution res = RES_10);

    /** Obtain the current temperature measurement
     *
     * @returns  The current temperature measurement in Celsius.
     */
    float temp();
    
#ifdef MBED_OPERATORS
    /** A shorthand for temp()
     *
     * @returns  The current temperature measurement in Celsius.
     */
    operator float();
#endif

protected:
    enum {
        REG_TEMPERATURE_H = 0x00,
        REG_STATUS = 0x01,
        REG_TEMPERATURE_L = 0x02,
        REG_CONFIGURATION = 0x03,
        REG_CONV_RATE = 0x04,
        REG_HIGH_LIMIT_H = 0x05,
        REG_HIGH_LIMIT_L = 0x06,
        REG_LOW_LIMIT_H = 0x07,
        REG_LOW_LIMIT_L = 0x08,
        REG_ONESHOT = 0x0f,
        REG_THERM = 0x20,
        REG_THERM_HYSTERESIS = 0x21,
        REG_SMBUS_TIMEOUT = 0x22,
        REG_PRODUCT_ID = 0xfd,
        REG_MFG_ID = 0xfe,
        REG_REVISION_ID = 0xff,
        RES_MASK = 0x0c
    };

    I2C _i2c;
    const int _addr;

    char read8(char reg);
    void write8(char reg, char data);
};

#endif