A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

Files at this revision

API Documentation at this revision

Comitter:
takuo
Date:
Wed Feb 05 04:24:36 2014 +0000
Parent:
2:3116fe4a0079
Commit message:
Standby mode is now supported.; Conversion rate can be changed.

Changed in this revision

STTS751.cpp Show annotated file Show diff for this revision Revisions of this file
STTS751.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3116fe4a0079 -r f9d3008f7e8f STTS751.cpp
--- a/STTS751.cpp	Sun Jan 19 03:13:38 2014 +0000
+++ b/STTS751.cpp	Wed Feb 05 04:24:36 2014 +0000
@@ -16,21 +16,24 @@
 
 #include "STTS751.h"
 
-#define SET_BITS(x,m,b) (((x) & !(m)) | (b))
+#define SET_BITS(x,m,b) (((x) & ~(m)) | (b))
 
-STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model)
-{
+STTS751::STTS751(PinName sda, PinName scl, bool standby, Address addr, Model model) :
+        _i2c(sda, scl), _addr(addr | model), _standby(standby) {
     init();
 }
 
-STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model)
-{
+STTS751::STTS751(I2C &i2c, bool standby, Address addr, Model model) :
+        _i2c(i2c), _addr(addr | model), _standby(standby) {
     init();
 }
 
-void STTS751::init()
-{
-    write8(REG_CONFIGURATION, 0x00);
+void STTS751::init() {
+    char conf = 0x00;
+    if (_standby)
+        conf |= CONF_RUNSTOP;
+    write8(REG_CONFIGURATION, conf);
+    // conversion rate = 1/sec
     write8(REG_CONV_RATE, 0x04);
 }
 
@@ -40,25 +43,57 @@
 
 STTS751::Resolution STTS751::resolution() {
     char conf = read8(REG_CONFIGURATION);
-    return (STTS751::Resolution)(conf & RES_MASK);
+    return (STTS751::Resolution) (conf & CONF_RES_MASK);
 }
 
 void STTS751::setResolution(STTS751::Resolution res) {
     char conf = read8(REG_CONFIGURATION);
-    conf = SET_BITS(conf, RES_MASK, res);
+    conf = SET_BITS(conf, CONF_RES_MASK, res);
     write8(REG_CONFIGURATION, conf);
 }
 
-float STTS751::temp()
-{
+int STTS751::conversionRate() {
+    char conv = read8(REG_CONV_RATE);
+    return conv & CONV_RATE_MASK;
+}
+
+void STTS751::setConversionRate(int rate) {
+    write8(REG_CONV_RATE, (char)(rate & CONV_RATE_MASK));
+}
+
+void STTS751::setStandbyMode(bool standby) {
+    _standby = standby;
+    char conf = read8(REG_CONFIGURATION);
+    if (_standby)
+        conf |= CONF_RUNSTOP;
+    else
+        conf &= ~CONF_RUNSTOP;
+    write8(REG_CONFIGURATION, conf);
+}
+
+void STTS751::start() {
+    if (ready())
+        write8(REG_ONESHOT, 1);
+}
+
+bool STTS751::ready() {
+    char status = read8(REG_STATUS);
+    return (status & STATUS_BUSY) == 0;
+}
+
+float STTS751::temp(bool nowait) {
+    if (_standby && !nowait) {
+        start();
+        while (!ready())
+            wait(0.01);
+    }
     signed char h = read8(REG_TEMPERATURE_H);
     unsigned char l = read8(REG_TEMPERATURE_L);
     return ((h << 8) | l) / 256.0;
 }
 
 #ifdef MBED_OPERATORS
-STTS751::operator float()
-{
+STTS751::operator float() {
     return temp();
 }
 #endif
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);