A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

STTS751.h

Committer:
takuo
Date:
2014-01-18
Revision:
0:4211e78bfa5d
Child:
1:b0a3645a3dba

File content as of revision 0:4211e78bfa5d:

/* STTS751 (I2C Temperature Sensor) Driver
 * 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"
 * 
 * // I2C Text LCD: http://mbed.org/users/takuo/code/ACM1602NI/
 * #include "ACM1602NI.h"
 * // I2C Temperature Sensor: http://mbed.org/users/takuo/code/STTS751/
 * #include "STTS751.h"
 *
 * // I2C pins: p9 = sda, p10 = scl
 * ACM1602NI lcd(p9, p10);
 * STTS751 temp(p9, p10);
 *
 * // You can specify an I2C object instead.
 * // I2C i2c(p2, p10);
 * // ACM1602NI lcd(i2c);
 * // STTS751 temp(i2c);
 * 
 * int main() {
 *     while (true) {
 *         lcd.locate(0, 0);
 *         lcd.printf("tmp: %.3f", (float)temp);
 *         wait(1);
 *     }
 * }
 * @endcode
 */
class STTS751
{
public:
    /** Device models
     */
    enum Model {
        MODEL_0 = 0x00,  /**< Model 0 (default) */
        MODEL_1 = 0x40   /**< Model 1 */
    };

    /** I2C addresses
     */
    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) */
    };

    /** 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);

    /** 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 Register {
        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
    };

    I2C _i2c;
    const int _addr;

    void init();
    char read8(char reg);
};

#endif