for (i=0x80;i>0;i>>=1) //shift bit for masking
{
if (i & value)
SDA=1; //masking value with i , write to SENSI-BUS
else
SDA=0;
ShortDelay(150);
SCL=1;
ShortDelay(150);
SCL=0;
ShortDelay(150);
}
SDA=1;// release SDA line
SCL=1; //clk #9 for ack
error=SDA;
SCL=0;
return error;
}
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
SDA=1; //release SDA-line
for (i=0x80;i>0;i>>=1) //shift bit for masking
{
ShortDelay(150);
SCL=1;
ShortDelay(150); //clk for SENSI-BUS
if (SDA) val=(val | i); //read bit
SCL=0;
}
SDA=!ack; //in case of "ack==1" pull down SDA-Line
ShortDelay(150);
SCL=1; //clk #9 for ack
ShortDelay(150); //pulswith approx. 5 us
SCL=0;
ShortDelay(150);
//release SDA-line
return val;
}
void s_transstart(void)
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [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
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [C]
t_C=t*0.01 - 40; //calc. Temperature from ticks to [C]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [C]
*p_humidity=rh_true; //return humidity[%RH]
}
void SHT11(void)
{
unsigned int humi,temp;
value humi_val,temp_val;
//float dew_point;
unsigned char error,checksum;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0)
s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
temp=temp_val.f*10;
humi=humi_val.f*10;
wendu[0]=(temp/1000)+'0'; //温度百位
wendu[1]=(temp%1000)/100+'0'; //温度十位
wendu[2]=(temp%100)/10+'0'; //温度个位
wendu[3]=0x2E; //小数点
wendu[4]=temp%10+'0'; //温度小数点后第一位
displaywendu();
shidu[0]=humi/1000+'0'; //湿度百位
shidu[1]=(humi%1000)/100+'0'; //湿度十位
shidu[2]=(humi%100)/10+'0'; //湿度个位
shidu[3]=0x2E; //小数点
shidu[4]=(humi%10)+'0'; //湿度小数点后第一位
displayshidu();
}
LongDelay(2000);//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
//(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------