SHT V4 5V UART

Dependencies:   mbed

Fork of SHT_v1 by Stephen McGarry

Files at this revision

API Documentation at this revision

Comitter:
fblanc
Date:
Tue Feb 23 08:10:54 2016 +0000
Parent:
0:df1c8f2961a1
Commit message:
SHT V4 5V;

Changed in this revision

SHT.cpp Show annotated file Show diff for this revision Revisions of this file
SHT.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/SHT.cpp	Sun Jan 24 22:00:35 2010 +0000
+++ b/SHT.cpp	Tue Feb 23 08:10:54 2016 +0000
@@ -6,6 +6,7 @@
 #include "SHT.h"
 
 #include "mbed.h"
+#define MICROSECONDE 10//1
 
 SHT::SHT(PinName p_sclk, PinName p_data, SHT_acc p_accuracy) : sclk(p_sclk), data(p_data), accuracy(p_accuracy) {
     sclk=0;
@@ -13,7 +14,7 @@
     data.mode(PullUp);    // with the pull up internally active
     data.input();        // with line released to go high
     temperature = humidity = dewpoint=0.0f;
-   printf("constructor\n");
+  
 }
 
 char SHT::write_byte(byte value)
@@ -26,16 +27,17 @@
     for (i=0x80;i>0;i/=2) {           //shift bit for masking
         if (i & value) data.input();  //masking value with i , write to SENSI-BUS
         else data.output();
-        wait_us(1);                   //ensure sclk is low for min time
+        wait_us(MICROSECONDE);                   //ensure sclk is low for min time
+        
         sclk=1;                       //clk for SENSI-BUS
-        wait_us(1);                   //pulsewith approx. 2 us
+        wait_us(MICROSECONDE);                   //pulsewith approx. 2 us
         sclk=0;
     }
     data.input();                     //release DATA-line
-    wait_us(1);                       //ensure sclk is low for min time
+    wait_us(MICROSECONDE);                       //ensure sclk is low for min time
     sclk=1;                           //clk #9 for ack
     error=data;                       //check ack (DATA will be pulled down by SHT11)
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=0;
     return error;                     //error=1 in case of no acknowledge
 }
@@ -47,16 +49,16 @@
     byte i,val=0;
     data.input();                     //release DATA-line
     for (i=0x80;i>0;i/=2) {           //shift bit for masking
-        wait_us(1);
+        wait_us(MICROSECONDE);
         sclk=1;                       //clk for SENSI-BUS
         if (data) val=(val | i);      //read bit
-        wait_us(1);
+        wait_us(MICROSECONDE);
         sclk=0;
     }
-    wait_us(1);
+    wait_us(MICROSECONDE);
     if (send_ack) data.output();      // if ack needed then drive data low
     sclk=1;                           //clk #9 for ack
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=0;
     data.input();                     //release DATA-line
     return val;
@@ -72,19 +74,19 @@
 {
     data.input();
     sclk=0;                   //Initial state
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=1;
-    wait_us(1);
+    wait_us(MICROSECONDE);
     data.output();            // data low
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=0;
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=1;
-    wait_us(1);
+    wait_us(MICROSECONDE);
     data.input();            // allow data to high
-    wait_us(1);
+    wait_us(MICROSECONDE);
     sclk=0;
-    wait_us(1);
+    wait_us(MICROSECONDE);
 }
 
 void SHT::connection_reset(void)
@@ -99,12 +101,13 @@
     data.input();            // allow data high
     sclk=0;                  // and clk low
     for (i=0;i<9;i++) {      // 9 SCK cycles
-        wait_us(1);
+        wait_us(MICROSECONDE);
         sclk=1;
-        wait_us(1);
+        wait_us(MICROSECONDE);
         sclk=0;
     }
 }
+
 char SHT::soft_reset(void)
 //----------------------------------------------------------------------------------
 // resets the sensor by a softreset
@@ -168,21 +171,23 @@
 
 void SHT::calculate()
 //----------------------------------------------------------------------------------------
-// calculates temperature [°C] and humidity [%RH]
+// calculates temperature [�C] and humidity [%RH]
 // input :  hum  [Ticks] (12 bit)
 //          temp [Ticks] (14 bit)
 // output:  humidity [%RH]
-//          temperature [°C]
+//          temperature [�C]
 {
-    const float C1=-4.0;              // for 12 Bit
-    const float C2=+0.0405;           // for 12 Bit
-    const float C3=-0.0000028;        // for 12 Bit
-    //const float T1=+0.01;             // for 14 Bit @ 5V
-    //const float T2=+0.00008;           // for 14 Bit @ 5V
-
+    //Optimized V4 humidity conversion coefficients
+    const float C1=-2.0468;              // for 12 Bit
+    const float C2=0.0367;           // for 12 Bit
+    const float C3=-1.5955E-6;        // for 12 Bit
+    const float T1=0.01;             // for 12 
+    const float T2=0.00008;           // for 12 Bit 
+    const float D1= -40.1;             //for °C 5V
+    const float D2= 0.01 ;            //for °C for 14
     float rh;                         // rh:      Humidity [Ticks] 12 Bit
     float t;                          // t:       Temperature [Ticks] 14 Bit
-    //float rh_lin;                     // rh_lin:  Humidity linear
+    float rh_lin;                     // rh_lin:  Humidity linear
     if (accuracy==SHT_low) {
         rh=hum*16;                    // rescale to high accuracy values - 8 to 12 bits
         t=temp*4;                     // and 12 to 14 bits
@@ -191,9 +196,9 @@
         t=temp;
     }
 
-    temperature=t*0.01 - 40;                  //calc. temperature from ticks to [°C]
-    humidity=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
-    // =(temperature-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH] (ignore as never >0.25%)
+    temperature=D1 + D2*t ;                  //calc. temperature from ticks to [�C]
+     rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
+     humidity =(temperature-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH] (ignore as never >0.25%)
     if (humidity>100)humidity=100;      //cut if the value is outside of
     if (humidity<0.1)humidity=0.1;      //the physical possible range
 }
@@ -210,9 +215,10 @@
     return dewpoint;
 }
 
-void SHT::update(SHT_acc acc) {   // update stored values from sensor
+int SHT::update(SHT_acc acc) {   // update stored values from sensor
     int error=0;
-    connection_reset();
+    soft_reset();
+    //connection_reset();
     if (acc!=accuracy) {
         accuracy=acc;
         error+=write_status((acc==SHT_low)?0x01:0x00);    //set the status reg to high or low accuarcy
@@ -220,5 +226,5 @@
     error+=measure(temp,com_measure_temp);
     error+=measure(hum,com_measure_humid);
     if (!error) calculate();
-
+return error;
 }
--- a/SHT.h	Sun Jan 24 22:00:35 2010 +0000
+++ b/SHT.h	Tue Feb 23 08:10:54 2016 +0000
@@ -7,6 +7,7 @@
 
 #include "mbed.h"
 
+
 enum SHT_acc {
     SHT_low=0,
     SHT_high=1
@@ -14,7 +15,7 @@
 
 typedef unsigned char byte;
 
-class SHT : public Base {
+class SHT {
 public:
     /* Constructor: SHT
      *  Creates an SHT interface connected to specific pins.
@@ -27,7 +28,7 @@
     float get_temperature();     // get the most recent temp reading
     float get_humidity();        // get the most recent humidity reading
     float get_dewpoint();        // get the most recent dewpoint value
-    void update(SHT_acc accuracy); // update stored values from sensor
+    int update(SHT_acc accuracy); // update stored values from sensor
 
 protected:
     byte read_byte(bool send_ack);
--- a/main.cpp	Sun Jan 24 22:00:35 2010 +0000
+++ b/main.cpp	Tue Feb 23 08:10:54 2016 +0000
@@ -3,18 +3,49 @@
 #include "mbed.h"
 
 #include "SHT.h"
+const char VERSION[]="2014_11_14";
+Serial pc(USBTX, USBRX); // tx, rx
+//SHT sht(PTE2,PTE3,SHT_high); // sclock, data
+SHT sht(p19,p20,SHT_high); // sclock, data
+DigitalOut led1(LED1);
+Ticker flipper;
 
-SHT th1(p5,p6,SHT_high); // sclock, data
-
-DigitalOut led1(LED1);
+void flip()
+{
+  led1=!led1; 
+  sht.update(SHT_high);
+}
 
-int main() {
-    while(1) {
-        th1.update(SHT_high);
-        printf("t:%6.2fC h:%6.2f%%\n",th1.get_temperature(),th1.get_humidity());
-        led1=!led1;
-        wait(1);
+void pc_rx(void)
+{
+    char c;
+    c=pc.getc();
+    switch (c) {
+
+        case 'H':
+        case 'h':
+            pc.printf("Humdity     [ %3.2f %% ]\r\n", sht.get_humidity());
+            break;
+        case 'T':
+        case 't':
+            pc.printf("Temperature [ %3.2f C ]\r\n", sht.get_temperature());
+            break;
+        case 'V':
+        case 'v':
+            pc.printf("version %s\r\n",VERSION);
+            break;
+
     }
 
+
+}
+int main() {
+flipper.attach(&flip, 0.5);
+    while (1) {
+        if (pc.readable())
+            pc_rx();
+
+
+    }
 }
 
--- a/mbed.bld	Sun Jan 24 22:00:35 2010 +0000
+++ b/mbed.bld	Tue Feb 23 08:10:54 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0
+http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file