《单片机设计温湿度传感器的设计程序.docx》由会员分享,可在线阅读,更多相关《单片机设计温湿度传感器的设计程序.docx(8页珍藏版)》请在优知文库上搜索。
1、GDAIB小远制作Reg5l.h#ifndef_REG51_H_#define_.REG5LH_*BYTERegister*/sfrPO:=0x80;sfrPl:=0x90;sfrP2=OxAO;sfrP3=OxBO;sfrPSW=OxDO;SfrACC=OxEO;sfrB=OxFO;sfrSP=0x81;sfrDPL=0x82;sfrDPH=0x83;sfrPCON=0x87;SfrTCON=0x88;sfrTMOD=0x89;SfrTLO=0x8A;SfrTLl=0x8B;SfrTHO=0x8C;SfrTHl=0x8D;sfrIE=0xA8;sfrIP=0xB8;sfrSCON=0x98;
2、sfrSBUF=0x99;*BITRegister*/*PSW*/sbitCY=0xD7;sbitAC=0xD6;SbitFO=OxD5;sbitRSI=OxD4;sbitRSO=OxD3;sbitOV=0xD2;SbitP=OxDO;*TCON*/SbitTFl=0x8F;SbitTRl=0x8E;sbitTFO=0x8D;sbitTRO=0x8C;sbitIEl=0x8B;sbitITI=0x8A;sbitIEO=0x89;sbitITO=0x88;*IE*/sbitEA=OXAF;sbitES=OxAC;sbitETl=OxAB;sbitEXl=OxAA;sbitETO=0xA9;sbi
3、tEXO=0xA8;*IP*/sbitPS=OxBC;sbitPTl=OxBB;sbitPXl=OxBA;sbitPTO=0xB9;sbitPXO=0xB8;*P3:*/sbitRD=0xB7;sbitWR=0xB6;SbitTl=0xB5;sbitTO=OxB4;sbitINTl=0xB3;sbitINTO=0xB2;SbitTXD=OxBl;sbitRXD=OxBO;*SCON*/sbitSMO=0x9F;sbitSMl=0x9E;sbitSM2=0x9D;sbitREN=0x9C;sbitTB8=0x9B;sbitRB8=0x9A;sbitTI=0x99;sbitRI=0x98;#end
4、ifMain.h#ifndef_MAIN_H_#define_MAIN_H_#include#defineucharunsignedchar#defineuintunsignedintvoiddelayIm(uintx);voidRH_founction();/温湿度读取子程序voidinit602(),初始化1602函数voidDHTll_display(ucharadd,ucharshi,ucharge);温湿度显示函数voidkey_shidu();voidkey_wendu();voidalarm_function();voidw_string(ucharadd,uchar*p);#e
5、ndifMain.c温湿度实时监测显示,超限报警,温度测量范围为0+50C;湿度测量范围为20%90%;#includemain.hMSbitSI=PIA4;温度sbits2=P13;湿度sbits3=P12;/+sbits4=P11;/-sbits5=P1O;确定sbitalarm=Pl5;蜂鸣器报警externUCharTH,RH;温度整数,湿度整数局部ucharcodewordl16=Myowndesign!);ucharcodeword216=,Tem=CHum=%);/word24word25Cword213word214%ucharcodehehe16=SetTemperature
6、!);设置温度上下限界面ucharcodehaha16J=SetHumidity!);设置湿度上下限界面ucharcodeword316=up=Cdown=C);ucharcodeword416=up=%down=%;ucharw_up=30;w_down=20;温度上下限uchars_up=60;s_down=40;湿度上下限voidmain()(uinti;init_1602();delaylm(500);延时等待1602稳定RH_founction();while(l)key_wendu();温度上下限调整key_shidu();湿度上下限调整i+;if(i2000)动态延时读取温湿度值
7、(i=0;RHJbUnCtion();读取温湿度JDHTlLdiSPlay(OXCO+4,TH10+0x30,TH%10+0x30);显示温度DHTl1_display(OxcO+13,RH10+0x30,RH%10+0x30);显示湿度if(THw_up)|(THs_up)|(RH4000)counter=0;清零if(counter2000)DHTlLdiSPIay(dzwei,0x20,0x20);显示空格if(counter1)(wei=0;)if(!s3)/加1while(!s3);bufwei+;if(bufweiJ50)buflwei=0;if(!s4)减1while(!s4);
8、bufwei-;if(bufwei=Oxff)bufwei=50;if(buf4000)counter=0;/清零if(counter2000)DHTILdiSPlay(dzwei,0x20,0x20);显示空格if(counterl)(wei=0;)if(!s3)/加1(while(!s3);bufwei+;if(bufwei90)(bufwei=0;)if(!s4)减1(while(!s4);bufwei-;if(bufwei20)(bufwei=90;)if(buf0=buHl)bufl0=6(kbuf11=40;/如果湿度上限不大于下限,回到初始状态while(s5);按下s5设置确定
9、s_up=buf0;s_down=bufl;w_string(0x80,Word1);w_string(OXCo,word2)J/发送字符串到LCD第行/=ms级延时函数=voiddelaylm(uintx)uintk,j;for(k=0;kx;k+)连数X次,约XmSfor(j=0;j120;j+);数120次,约ImS)1602.c#includemain.h#defineshujuP0数据脚sbitrs=P2A7;数据命令选择端sbitrw=P2A6;读写选择端sbiten=P251602使能端sbitbusy=P07;/检测为1,说明1602忙externucharcodeword116;externucharcodeword216;/=读繁忙标记函数=voidread_busy()(shuju=OXff;准备读数据do(rs=0;读状态rw=1;读en=0;en=1;使能1602数据读出)while(busy);en=0;)=写命令函数=voidWrit