mbed library to use a SENSIRION SHT1x/SHT7x sensor humidity and temperature

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Thu Jun 21 03:56:43 2012 +0000
Commit message:

Changed in this revision

SHT1x.cpp Show annotated file Show diff for this revision Revisions of this file
SHT1x.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r ad72ec3d85e8 SHT1x.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SHT1x.cpp	Thu Jun 21 03:56:43 2012 +0000
@@ -0,0 +1,117 @@
+/*
+ * mbed library to use a SENSIRION SHT1x/SHT7x sensor
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+ 
+/** @file
+ * @brief mbed library to use a SENSIRION SHT1x/SHT7x sensor
+ * humidity/temperature sensor SHT11, SHT15, SHT71, SHT75 (SENSIRION) Ver.4
+ * interface: I2C digital
+ */
+
+#include "mbed.h"
+#include "SHT1x.h"
+
+enum SHT_cmd {
+    SHT_cmd_temperature = 0x03,
+    SHT_cmd_humidity = 0x05,
+    SHT_cmd_read_status = 0x07,
+    SHT_cmd_write_status = 0x06,
+    SHT_cmd_reset = 0x1e
+};
+
+SHT1x::SHT1x (PinName p_sda, PinName p_scl, SHT_acc p_acc, SHT_vdd p_vdd) : i2c(p_sda, p_scl) {
+    init(p_acc, p_vdd);
+}
+
+SHT1x::SHT1x (I2C& p_i2c, SHT_acc p_acc, SHT_vdd p_vdd) : i2c(p_i2c) {
+    init(p_acc, p_vdd);
+}
+
+float SHT1x::get_temperature() {
+    return temperature;
+}
+
+float SHT1x::get_humidity() {
+    return humidity;
+}
+
+void SHT1x::update () {
+    float t, rhl, rht;
+    unsigned short sot, sorh;
+
+    sot = twi_readshort(SHT_cmd_temperature, (accuracy == SHT_low ? 80 : 320));
+    sorh = twi_readshort(SHT_cmd_humidity, (accuracy == SHT_low ? 20 : 80));
+    if (sot == 0xffff || sorh == 0xffff) return;
+
+    if (accuracy == SHT_low) {
+        sot &= 0x0fff; // 12bit
+        sorh &= 0x00ff; // 8bit
+    } else {
+        sot &= 0x3fff; // 14bit
+        sorh &= 0x0fff; // 12bit
+    }    
+
+    t = d1 + d2 * (float)sot;
+    temperature = t;
+
+    rhl = c1 + c2 * (float)sorh + c3 * (float)(sorh * sorh);
+    rht = (t - 25.0) * (t1 + t2 * (float)sorh) + rhl;
+    if (rht > 99.0) rht = 100.0;
+    humidity = rht;
+}
+
+void SHT1x::init (SHT_acc p_acc, SHT_vdd p_vdd) {
+
+    humidity = 0;
+    temperature = 0;
+
+    if (p_vdd == SHT_3V3) {
+        d1 = -39.66; // 3.3V
+    } else {
+        d1 = -40.1; // 5V
+    }
+
+    accuracy = p_acc;
+    if (accuracy == SHT_low) {
+        d2 = 0.04;
+        c1 = -2.0468; c2 = 0.5872; c3 = -4.0845/10000.0;
+        t1 = 0.01; t2 = 0.00128;
+        twi_writechar(SHT_cmd_write_status, 1);
+    } else {
+        d2 = 0.01;
+        c1 = -2.0468; c2 = 0.0367; c3 = -1.5955/1000000.0;
+        t1 = 0.01; t2 = 0.00008;
+        twi_writechar(SHT_cmd_write_status, 0);
+    }    
+}
+
+unsigned short SHT1x::twi_readshort (int addr, int wait) {
+    unsigned short i;
+
+    i2c.start();
+    i2c.stop();
+
+    i2c.start();
+    i2c.write(addr);
+    wait_ms(wait);
+
+    i = i2c.read(1) << 8;
+    i |= i2c.read(1);
+    i2c.read(0);
+    i2c.stop();
+
+    return i;
+}
+
+void SHT1x::twi_writechar (int addr, int dat) {
+
+    i2c.start();
+    i2c.stop();
+
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(dat);
+    i2c.stop();
+}
diff -r 000000000000 -r ad72ec3d85e8 SHT1x.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SHT1x.h	Thu Jun 21 03:56:43 2012 +0000
@@ -0,0 +1,83 @@
+/*
+ * mbed library to use a SENSIRION SHT1x/SHT7x sensor
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+ 
+/** @file
+ * @brief mbed library to use a SENSIRION SHT1x/SHT7x sensor
+ * humidity/temperature sensor SHT11, SHT15, SHT71, SHT75 (SENSIRION)
+ * interface: I2C digital
+ */
+ 
+#ifndef SHT1x_H
+#define SHT1x_H
+
+#include "mbed.h"
+
+/**
+ * @brief resolution setting
+ */
+enum SHT_acc {
+    SHT_low = 0, ///< RH 8bit, temp 12bit
+    SHT_high = 1, ///< RH 10bit, temp 14bit
+};
+
+enum SHT_vdd {
+    SHT_3V3 = 0,
+    SHT_5V = 1,
+};
+
+/**
+ * @brief SHT1x class
+ */
+class SHT1x : public Base {
+public:
+    /**
+     * @brief Initializes interface (private I2C)
+     * @param p_sda port of I2C SDA
+     * @param p_scl port of I2C SCL
+     * @param p_acc resolution setting
+     * @param p_vdd power
+     */
+    SHT1x(PinName p_sda, PinName p_scl, SHT_acc p_acc = SHT_high, SHT_vdd p_vdd = SHT_3V3);
+
+    /**
+     * @brief Initializes interface (public I2C)
+     * @param p_i2c instance of I2C class
+     * @param p_acc resolution setting
+     * @param p_vdd power
+     */
+    SHT1x(I2C& p_i2c, SHT_acc p_acc = SHT_high, SHT_vdd p_vdd = SHT_3V3);
+
+    /**
+     * @brief Get temperature
+     * @return temperature (`C)
+     */
+    float get_temperature();
+
+    /**
+     * @brief Get humidity
+     * @return humidity (%)
+     */
+    float get_humidity();
+
+    /**
+     * @brief Update results
+     */
+    void update();
+
+protected:
+    void init(SHT_acc p_acc, SHT_vdd p_vdd);
+    unsigned short twi_readshort (int addr, int wait);
+    void twi_writechar (int, int);
+
+    I2C i2c;
+    float humidity, temperature;
+
+private:
+    float d1, d2, c1, c2, c3, t1, t2;
+    enum SHT_acc accuracy;
+};
+
+#endif
\ No newline at end of file