本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/
void calc_sth11(float *p_humidity ,float *p_temperature)
// calculates temperature [℃] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [℃]
{ 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 [℃]
t_C=t*0.01 - 40; //calc. temperature from ticks to [℃]
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 [℃]
*p_humidity=rh_true; //return humidity[%RH]
}
/*--------------------------------------
;模块名称:calc_dewpoint();
;功 能:露点,空气湿度达到饱和时的温度
;占用资源:--
;参数说明:--
;创建日期:2012.04.10
;版 本:FV1.0(函数版本Function Version)
;修改日期:--
;修改说明:--
;-------------------------------------*/
float calc_dewpoint(float h,float t)
// calculates dew point
// input: humidity [%RH], temperature [℃]
// output: dew point [℃]
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
/*****************主函数*********************/
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [℃]
// 4. calculate dew point [℃]
// 5. print temperature, humidity, dew point
{ value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
unsigned int i;
init_uart(); //串口初始化
s_connectionreset();
while(1)
{ error=0;
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
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
printf("Temp:%5.1f℃ Humi:%5.1f%% Dew point:%5.1f℃\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}
复制代码
[/code]
|