frederic blanc
/
SHTxx2013
SHT V4 5V UART
Fork of SHT_v1 by
Diff: SHT.cpp
- Revision:
- 1:0dbbb4802508
- Parent:
- 0:df1c8f2961a1
--- 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; }