A library for STMicroelectronics STTS751 I2C temperature sensor
Diff: STTS751.h
- Revision:
- 3:f9d3008f7e8f
- Parent:
- 2:3116fe4a0079
diff -r 3116fe4a0079 -r f9d3008f7e8f STTS751.h --- a/STTS751.h Sun Jan 19 03:13:38 2014 +0000 +++ b/STTS751.h Wed Feb 05 04:24:36 2014 +0000 @@ -20,6 +20,7 @@ #include "mbed.h" /** A library for STMicroelectronics STTS751 I2C temperature sensor + * * http://www.st.com/web/catalog/sense_power/FM89/SC294/PF220116 * * Example: @@ -28,33 +29,34 @@ * #include "STTS751.h" * * // I2C pins: p9 = sda, p10 = scl - * STTS751 temp(p9, p10); + * STTS751 sensor(p9, p10); * * // You can specify an I2C object instead. * // I2C i2c(p2, p10); - * // STTS751 temp(i2c); + * // STTS751 sensor(i2c); * * int main() { * // set the temperature resolution to 12bits - * temp.setResolution(STTS751::RES_12); + * sensor.setResolution(STTS751::RES_12); * while (true) { - * printf("tmp: %.4f\n", (float)temp); + * printf("tmp: %.4f\n", (float)sensor); * wait(1); * } * } * @endcode */ -class STTS751 -{ +class STTS751 { public: /** Device models */ enum Model { - MODEL_0 = 0x00, /**< Model 0 (default) */ - MODEL_1 = 0x40 /**< Model 1 */ + MODEL_0 = 0x00, /**< Model 0 (default) */ + MODEL_1 = 0x40 /**< Model 1 */ }; - /** I2C address selectors + /** I2C slave address selectors + * The I2C slave address of the device is determined by the pull up register for Addr/~Therm pin. + * */ enum Address { ADDRESS_0 = (0x48 << 1), /**< pull up resistor = 7.5K */ @@ -66,34 +68,41 @@ /** Temperature resolutions */ enum Resolution { - RES_9 = 0x08, /**< 9 bits */ + RES_9 = 0x08, /**< 9 bits */ RES_10 = 0x00, /**< 10 bits (default) */ RES_11 = 0x04, /**< 11 bits */ - RES_12 = 0x0c /**< 12 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) + * @param sda I2C data pin + * @param scl I2C clock pin + * @param standby Standby mode (defaults to false) + * @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); + STTS751(PinName sda, PinName scl, + bool standby = false, 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) + * @param i2c I2C port + * @param standby Standby mode (defaults to false) + * @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); + STTS751(I2C &_i2c, + bool standby = false, Address addr = ADDRESS_3, Model model = MODEL_0); /** Initialize the device */ void init(); - /** The I2C address (8bit address) of the device + /** The I2C slave address (8bit) of the device + * + * @returns The I2C slave address (8bit) of the device */ int addr(); @@ -109,12 +118,45 @@ */ void setResolution(Resolution res = RES_10); + /** Get the current conversion rate + * + * @returns The current conversion rate + */ + int conversionRate(); + + /** Set the conversion rate + * Set the number of times the temperature value is updated each second. + * The conversion rate (times/second) can be calculated by 2^(rate - 4). + * Note that rate should be less than 8 if the temperature resolution is 12-bit and + * should be less than 9 if the temperature resolution is greater than 10-bit + * + * @param rate Conversion rate (0 .. 9) + */ + void setConversionRate(int rate); + + /** Set the device mode + * + * @param standby true : standby mode, false : continuous conversion mode + */ + void setStandbyMode(bool standby); + + /** Start a temperature conversion (in standby mode) + */ + void start(); + + /** Checks that a temperature conversion has finished and the temperature value is available + * + * @returns true if a temperature conversion has finished and the temperature value is available + */ + bool ready(); + /** Obtain the current temperature measurement * - * @returns The current temperature measurement in Celsius. + * @param nowait If true, read the temperature value without waiting for finishing a conversion (in standby mode) + * @returns The current temperature measurement in Celsius. */ - float temp(); - + float temp(bool nowait = false); + #ifdef MBED_OPERATORS /** A shorthand for temp() * @@ -124,6 +166,8 @@ #endif protected: + /** I2C registers + */ enum { REG_TEMPERATURE_H = 0x00, REG_STATUS = 0x01, @@ -141,11 +185,48 @@ REG_PRODUCT_ID = 0xfd, REG_MFG_ID = 0xfe, REG_REVISION_ID = 0xff, - RES_MASK = 0x0c + }; + + /** Configuration register (default 0x00) + * - b7 : MASK1 + * - b6 : ~RUN/STOP (0 : continuous conversion mode, 1 : standby mode) + * - b5 : 0 + * - b4 : RFU + * - b3 : Tres1 + * - b2 : Tres0 + * - b1-b0 : RFU + */ + enum { + CONF_MASK1 = 0x80, + CONF_RUNSTOP = 0x40, + CONF_RES_MASK = 0x0c, + }; + + /** Status Register (default undefined) + * - b7 : Busy + * - b6 : T_HIGH + * - b5 : T_LOW + * - b4-b1 : RFU + * - b0 : THRM + */ + enum { + STATUS_BUSY = 0x80, + STATUS_THIGH = 0x40, + STATUS_LOW = 0x20, + STATUS_THURM = 0x01 + }; + + /** Conversion Rate Register (default 4) + * - b7-b4: 0 + * - b3-b0: Conversion rate (0..9) + */ + enum { + CONV_RATE_MASK = 0x0f }; I2C _i2c; const int _addr; + bool _standby; char read8(char reg); void write8(char reg, char data);