系统通过SHT11温湿度传感器感应周围的环境的温度和湿度,通过单片机对采集到的数据进行读取处理,经过LCD1602显示模块实时显示温湿度数据,同时可以通过按键模块对温湿度报警上、下限值进行设定。当SHT11读取的温湿度值不再设定范围内时,报警模块LED灯指示故障信息,同时蜂鸣器报警;当温湿度读取数据正常后,LED灯熄灭,蜂鸣器关闭。
运行结果如下
SHT11程序:
- /*********************************************************/
- // SHT11温湿度检测
- /*********************************************************/
- char ShtMeasure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- {
- unsigned error=0;
- unsigned int i;
- ShtTransStart();
- switch(mode)
- {
- case 1 :
- error+=ShtWriteByte(0x03);
- break;
- case 2 :
- error+=ShtWriteByte(0x05);
- break;
- default:
- break;
- }
- for(i=0;i<65535;i++)
- if(Data_P==0)
- break;
- if(Data_P)
- error+=1;
- *(p_value) =ShtReadByte(1);
- *(p_value+1)=ShtReadByte(1);
- *p_checksum =ShtReadByte(0);
- return error;
- }
- /*********************************************************/
- // SHT11温湿度值标度变换及温度补偿
- /*********************************************************/
- void CalcSHT11(float *p_humidity ,float *p_temperature)
- {
- const float C1=-4.0;
- const float C2=+0.0405;
- const float C3=-0.0000028;
- const float T1=+0.01;
- const float T2=+0.00008;
- float rh=*p_humidity;
- float t=*p_temperature;
- float rh_lin;
- float rh_true;
- float t_C;
- t_C=t*0.01 - 40;
- rh_lin=C3*rh*rh + C2*rh + C1;
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;
- *p_humidity=rh_true;
- }
- /*********************************************************/
- // 温度校正
- /*********************************************************/
- unsigned char TempCorrect(int temp)
- {
- if(temp<0) temp=0;
- if(temp>970) temp=970;
- if(temp>235) temp=temp+10;
- if(temp>555) temp=temp+10;
- if(temp>875) temp=temp+10;
- temp=(temp%1000)/10;
- return temp;
- }
- /*********************************************************/
- // 湿度校正
- /*********************************************************/
- unsigned char HumiCorrect(unsigned int humi)
- {
- if(humi>999) humi=999;
- if((humi>490)&&(humi<951)) humi=humi-10;
- humi=(humi%1000)/10;
- return humi;
- }
- /*********************************************************/
- // 读取SHT11的温湿度数据
- /*********************************************************/
- void ReadShtData()
- {
- value humi_val,temp_val;
- unsigned char error;
- unsigned char checksum;
- unsigned int temp1,humi1;
- error=0;
- error+=ShtMeasure((unsigned char*)&temp_val.i,&checksum,1);
- error+=ShtMeasure((unsigned char*)&humi_val.i,&checksum,2);
- if(error!=0)
- ShtConnectReset();
- else
- {
- humi_val.f=(float)humi_val.i;
- temp_val.f=(float)temp_val.i;
- CalcSHT11(&humi_val.f,&temp_val.f);
- temp1=temp_val.f*10;
- temp=TempCorrect(temp1);
- humi=HumiCorrect(humi1);
- humi = humi + 2;
- }
- }
复制代码
|