#ifndef__TOU_H__//如果没有被编译过#define__TOU_H__//那么标记为编译过#includereg52.h#includeintrins.h#includemath.h#includestring.h#defineuintunsignedint#defineucharunsignedcharenum{TEMP,HUMI};sbitDATA=P2^5;//温湿度传感器位定义sbitSCK=P2^4;voids_connectionreset(void);/********SHT11函数声明********/chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode);voidcalc_sth10(float*p_humidity,float*p_temperature);floatcalc_dewpoint(floath,floatt);#endif//结束#ifndef,如果已经编译过就跳过之间的部分ucharcodetable1[]=00:00:00;ucharcodetable2[]=00:00:00RH:00.0;sbitshui1=P1^0;sbitshui2=P1^1;sbitshi_s1=P1^2;sbitshi_s2=P1^3;sbitshi_s3=P1^4;sbitled=P1^6;sbitsound=P1^7;sbitlcden=P2^2;sbitlcdrw=P2^1;sbitlcdrs=P2^0;sbitrelay=P2^3;sbits1=P2^7;uchardatadisdata[4];ucharnum,count,s1num;charshi,fen,miao,shi1,fen1,miao1;uintbian,i,j;voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}//SHT11程序(SHT11.c):#definenoACK0//继续传输数据,用于判断是否结束通讯#defineACK1//结束数据传输;//地址命令读/写#defineSTATUS_REG_W0x06//00000110#defineSTATUS_REG_R0x07//00000111#defineMEASURE_TEMP0x03//00000011#defineMEASURE_HUMI0x05//00000101#defineRESET0x1e//00011110//写字节程序chars_write_byte(unsignedcharvalue){unsignedchari,error=0;for(i=0x80;i0;i=1)//高位为1,循环右移{if(i&value)DATA=1;//和要发送的数相与,结果为发送的位elseDATA=0;SCK=1;_nop_();_nop_();_nop_();//延时3usSCK=0;}DATA=1;//释放数据线SCK=1;error=DATA;//检查应答信号,确认通讯正常_nop_();_nop_();_nop_();SCK=0;DATA=1;returnerror;//error=1通讯错误}//读字节程序chars_read_byte(unsignedcharack){unsignedchari,val=0;DATA=1;//释放数据线for(i=0x80;i0;i=1)//高位为1,循环右移{SCK=1;if(DATA)val=(val|i);//读一位数据线的值SCK=0;}DATA=!ack;//如果是校验,读取完后结束通讯;SCK=1;_nop_();_nop_();_nop_();//延时3usSCK=0;_nop_();_nop_();_nop_();DATA=1;//释放数据线returnval;}//启动传输voids_transstart(void){DATA=1;SCK=0;_nop_();SCK=1;_nop_();DATA=0;_nop_();SCK=0;_nop_();_nop_();_nop_();SCK=1;_nop_();DATA=1;_nop_();SCK=0;}voids_connectionreset(void){unsignedchari;DATA=1;SCK=0;//准备for(i=0;i9;i++)//DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位{SCK=1;SCK=0;}s_transstart();//启动传输}//温湿度测量chars_measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode)//进行温度或者湿度转换,由参数mode决定转换内容;{unsignederror=0;unsignedinti;s_transstart();//启动传输switch(mode)//选择发送命令{caseTEMP:error+=s_write_byte(MEASURE_TEMP);break;//测量温度caseHUMI:error+=s_write_byte(MEASURE_HUMI);break;//测量湿度default:break;}for(i=0;i65535;i++)if(DATA==0)break;//等待测量结束if(DATA)error+=1;//如果长时间数据线没有拉低,说明测量错误*(p_value)=s_read_byte(ACK);//读第一个字节,高字节(MSB)*(p_value+1)=s_read_byte(ACK);//读第二个字节,低字节(LSB)*p_checksum=s_read_byte(noACK);//readCRC校验码returnerror;//error=1通讯错误}//温湿度值标度变换及温度补偿voidcalc_sth10(float*p_humidity,float*p_temperature){constfloatC1=-4.0;//12位湿度精度修正公式constfloatC2=+0.0405;//12位湿度精度修正公式constfloatC3=-0.0000028;//12位湿度精度修正公式constfloatT1=0.46;//14位温度精度5V条件修正公式constfloatT2=+0.00008;//14位温度精度5V条件修正公式floatrh=*p_humidity;//rh:12位湿度floatt=*p_temperature;//t:14位温度floatrh_lin;//rh_lin:湿度linear值floatrh_true;//rh_true:湿度ture值floatt_C;//t_C:温度℃t_C=t*0.01-40;//补偿温度rh_lin=C3*rh*rh+C2*rh+C1;//相对湿度非线性补偿rh_true=(t_C-33)*(T1+T2*rh)+rh_lin;//相对湿度对于温度依赖性补偿if(rh_true100)rh_true=100;//湿度最大修正if(rh_true0.1)rh_true=0.1;//湿度最小修正*p_temperature=t_C;//返回温度结果*p_humidity=rh_true;//返回湿度结果}//从相对温度和湿度计算露点floatcalc_dewpoint(floath,floatt){floatlogEx,dew_point;logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);dew_point=(0.66077-logEx)*237.3/(logEx-8.16077);returndew_point;}typedefunion//定义共用同类型{unsignedinti;floatf;}value;voidwrite_com(ucharcom)//lcd部分{lcdrs=0;lcdrw=0;delay(5);lcden=1;P0=com;delay(5);lcden=0;}voidwrite_data(uchardate){lcdrs=1;lcden=1;delay(5);P0=date;delay(5);lcden=0;}voidlcd_init(){lcdrw=0;lcden=0;delay(15);write_com(0x38);delay(5);write_com(0x38);delay(5);write_com(0x38);write_com(0x38);write_com(0x08);write_com(0x01);write_com(0x06);write_com(0x0c);write_com(0x80+1);TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;write_com(0x80+1);for(num=0;num8;num++){write_data(table1[num]);delay(20);}write_com(0x80+0x40+1);for(num=0;num16;num++){write_data(table2[num]);delay(20);}}voidwrite_sfm(ucharadd,uchardate){ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge);}voidwrite_shedingsfm(ucharadd1,uchardate1){ucharshi1,ge1;shi1=date1/10;ge1=date1%10;write_com(0x80+add1);write_data(0x30+shi1);write_data(0x30+ge1);}voidkeyscan(){if(shi_s1==0){delay(5);if(shi_s1==0){s1num++;while(!shi_s1);}}if(s1num==1){write_com(0x80+0x40+8);write_com(0x0f);}if(s1num==2){write_com(0x80+0x40+5);write_com(0x0f);}if(s1num==3){write_com(0x80+0x40+2);write_com(0x0f);}if(s1num==4){TR0=1;write_com(0x80+8);write_com(0x0f);}if(s1num==5){write_com(0x80+5);write_com(0x0f);}if(s1num==6){write_com(0x80+2);write_com(0x0f);}if(s1num==7){s1num=0;write_com(0x0c);}if(s1num!=0){if(shi_s2==0){delay(5);if(shi_s2==0){while(!shi_s2);if(s1num==1){miao++;if(miao==60){miao=0;}write_sfm(7,miao);write_com(0x80+0x40+8);}if(s1num==2){fen++;if(fen==60){fen=0;}write_sfm(4,fen);write_com(0x80+0x40+5);}if(s1num==3){shi++;if(shi==24){shi=0;}write_sfm(1,shi);write_com(0x80+0x40+2);}if(s1num==4){TR0=1;miao1++;if(miao1==60