51温度湿度c语言代码.docx
《51温度湿度c语言代码.docx》由会员分享,可在线阅读,更多相关《51温度湿度c语言代码.docx(20页珍藏版)》请在冰豆网上搜索。
51温度湿度c语言代码
#ifndef__TOU_H__//如果没有被编译过
#define__TOU_H__//那么标记为编译过
#include
#include
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
enum{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;x>0;x--)
for(y=110;y>0;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;i>0;i>>=1)//高位为1,循环右移
{
if(i&value)DATA=1;//和要发送的数相与,结果为发送的位
elseDATA=0;
SCK=1;
_nop_();_nop_();_nop_();//延时3us
SCK=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;i>0;i>>=1)//高位为1,循环右移
{
SCK=1;
if(DATA)val=(val|i);//读一位数据线的值
SCK=0;
}
DATA=!
ack;//如果是校验,读取完后结束通讯;
SCK=1;
_nop_();_nop_();_nop_();//延时3us
SCK=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;i<9;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;i<65535;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_true>100)rh_true=100;//湿度最大修正
if(rh_true<0.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;num<8;num++)
{
write_data(table1[num]);
delay(20);
}
write_com(0x80+0x40+1);
for(num=0;num<16;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)
{
miao1=0;
}
write_shedingsfm(7,miao1);
write_com(0x80+8);
}
if(s1num==5)
{
fen1++;
if(fen1==60)
{
fen1=0;
}
write_shedingsfm(4,fen1);
write_com(0x80+5);
}
if(s1num==6)
{
shi1++;
if(shi1==24)
{
shi1=0;
}
write_shedingsfm(1,shi1);
write_com(0x80+2);
}
}
}
}
if(shi_s3==0)
{
delay(5);
if(shi_s3==0)
{
while(!
shi_s3);
if(s1num==1)
{
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(7,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen--;
if(fen==-1)
{
fen=59;
}
write_sfm(4,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi--;
if(shi==-1)
{
shi=23;
}
write_sfm(1,shi);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
TR0=1;
miao1--;
if(miao1==-1)
{
miao1=59;
}
write_shedingsfm(7,miao1);
write_com(0x80+8);
}
if(s1num==5)
{
fen1--;
if(fen1==-1)
{
fen1=59;
}
write_shedingsfm(4,fen1);
write_com(0x80+5);
}
if(s1num==6)
{
shi1--;
if(shi1==-1)
{
shi1=23;
}
write_shedingsfm(1,shi1);
write_com(0x80+2);
}
}
}
}
voidbaojing()
{
if(shui1==0)
{
led=~led;
delay(150);
{
for(i=0;i<10;i++)
{
sound=0;
delay(20);
sound=1;
delay(20);
}
}
}
if(shui2==0)
relay=1;
}
voidlcd_dingshi()
{
keyscan();
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(1,shi);
}
write_sfm(4,fen);
}
write_sfm(7,miao);
}
}
voidmain()
{
unsignedinttemp,humi;
valuehumi_val,temp_val;//定义两个共同体,一个用于湿度,一个用于温度
floatdew_point;//用于记录露点值
unsignedcharerror;//用于检验是否出现错误
unsignedcharchecksum;//CRC
ucharn;n=0;
relay=1;
lcd_init();
P3=0;
led=0;
sound=1;
while
(1)
{
if(s1==0)//if语句内为关系运算符为==
{
delay(20);
if(s1==0)
{
n++;
while(!
s1);
}
}
if(n==5)
n=0;
switch(n)//使用多分支选择语句
{
case1:
P3=0x10;bian=500;//第一个LED亮
break;
case2:
P3=0x20;bian=450;//第二个LED亮
break;
case3:
P3=0x40;bian=550;//第三个LED亮
break;
case4:
P3=0x80;bian=600;//第四个LED亮
break;
default:
P3=0x00;
}
lcd_dingshi();
if((shi==shi1)&&(fen==fen1)&&(miao==miao1))
{
relay=0;
}
baojing();
if(miao==10)
{
s_connectionreset();//启动连接复位
error=0;//初始化error=0,即没有错误
error+=s_measure((unsignedchar*)&temp_val.i,&checksum,TEMP);//温度测量
error+=s_measure((unsignedchar*)&humi_val.i,&checksum,HUMI);//湿度测量
if(error!
=0)s_connectionreset();////如果发生错误,系统复位
humi_val.f=(float)humi_val.i;//转换为浮点数
temp_val.f=(float)temp_val.i;//转换为浮点数
calc_sth10(&humi_val.f,&temp_val.f);//修正相对湿度及温度
dew_point=calc_dewpoint(humi_val.f,temp_val.f);//计算edew_point
temp=temp_val.f*10;
humi=humi_val.f*10;
disdata[0]=humi%1000/100+0x30;
disdata[1]=humi%100/10+0x30;
disdata[2]=humi%10+0x30;
write_com(0x80+0x40+12);
write_data(disdata[0]);
write_com(0x80+0x40+13);
write_data(disdata[1]);
write_com(0x80+0x40+14);
write_data(0x2e);
write_com(0x80+0x40+15);
write_data(disdata[2]);
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(1,shi);
}
write_sfm(4,fen);
}
write_sfm(7,miao);
}
if(P3!
=0x00)
{
if(bian>humi){relay=0;}
else{relay=1;}
}
if(shui2==0){relay=1;}
}
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}