⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 +

📁 基于51单片机的智能温湿度报警系统
💻
📖 第 1 页 / 共 2 页
字号:
#include <REGX52.h>
#include <intrins.h> 
typedef unsigned char  U8;     
typedef signed   char  S8;   
typedef unsigned int   U16;     
typedef signed   int   S16;     
typedef unsigned long  U32;     
typedef signed   long  S32;        
typedef float          F32;      
typedef double         F64;      

sbit  DHT1  = P3^2;
sbit  DHT2  = P3^3;
U8 FLAG1,k1,FLAG2,k2;
U8 count1,temp1, count2,temp2;
U8 TZ1,TX1,HZ1,HX1,check1,TZ2,TX2,HZ2,HX2,check2;
U8 TH_1,TL_1,HH_1,HL_1,checkdata1,comdata1, TH_2,TL_2,HH_2,HL_2,checkdata2 ,comdata2;
U8 str[11]={"RS232"}, str2[5]={"rs322"};
U8 AA=0,n=0;DD=0;
U16 TEMP_H1=30,TEMP_L1=5,HUM_H1=70,HUM_L1=40;	
U16 TEMP_H2=30,TEMP_L2=5,HUM_H2 =70,HUM_L2=40;
sbit RS=P2^7;										
sbit RW=P2^6;
sbit E=P2^5;
sbit hum_alarm_led1= P2^0; 
sbit temp_alarm_led1= P2^1; 
sbit temp_alarm_led2=P2^4; 
sbit hum_alarm_led2 = P2^3; 
sbit K1=P1^0;     
sbit K2=P1^1;   
sbit K3=P1^2;    
sbit K4=P1^3;  
sbit K5=P1^4;     
sbit K6=P1^5;     
sbit K7=P1^6;      
sbit K8=P1^7;  
U8 LCD_51,LCD_41,LCD_31,LCD_21,LCD_11; 
U8 LCD_52,LCD_42,LCD_32,LCD_22,LCD_12;
U8 code  LCDData[] ="0123456789"; 	
U8 xdata first_line[16] ="X  %    Y  %    ";  		
U8 xdata second_line[16]="      :  :      ";     
U8 timedata[3]={0x00,0x00,0x00};

//char data dis[8]={0x00,0x00,0x00,0x00,0x00,0x00};	 
U16 con=0,con1=0,con2=0; 	  
U8 xdata shuju[5][32];
U16 p=0,M=0;
U8 xdata  a1,a11,b1,b11,c1,c11,c111; 


																																				   
void delay1ms(U16 t)	 
{U16 i,j;
  for(i=0;i<t;i++)
    for(j=0;j<120;j++)
	;
} 

void  Delay_10us(void)
      {
        U8 i;
   i--;i--;i--;
   i--;i--;i--;
       }
void check_busy(void) 
{
	while(1)
	{
	P0=0xff;
	E=0;_nop_();
	RS=0;_nop_();_nop_();
	RW=1;_nop_();_nop_();
	E=1;_nop_();_nop_();_nop_();nop_();
	if((P0&0x80)==0)
	{
		break;
	}
	E=0;
	}
}
void write_command(U8 tempdata)
{  	E=0;_nop_();_nop_(); 
	RS=0; //寄存器选择指令
	_nop_();_nop_();RW=0;
	P0=tempdata;
    _nop_();_nop_();E=1;  
	_nop_();_nop_();E=0; _nop_();
	check_busy();
}
void write_data(U8 tempdata)
{  	E=0;_nop_();_nop_();  
	RS=1;//寄存器选择数据 
    _nop_();_nop_();RW=0;
	P0=tempdata;
    _nop_();_nop_();E=1; 
    _nop_();_nop_();E=0; _nop_(); 
	check_busy(); //检查忙
}
void init_lcd1602()	 //LCD初始化
{
 write_command(0x01);							
 write_command(0x38);							
 write_command(0x0c);							
 write_command(0x06);             
 delay1ms(5);// Delay(5);        
}
void display(void)
{ 
      U8 i;
      write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(first_line[i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=0;i<16;i++)
		{
 write_data(second_line[i]);
		}
}
void display_pro(void)
{	 
       LCD_51=LCDData[(str[0]/10)%10];
       LCD_41=LCDData[str[0]%10];
       LCD_21=LCDData[(str[2]/10)%10];	
       LCD_11=LCDData[str[2]%10];          		 				
	   first_line[1]=LCD_51;	
	   first_line[2]=LCD_41;	   	  
	   first_line[5]=LCD_21;
	   first_line[6]=LCD_11;
	   second_line[4]=LCDData[str[5]];
	   second_line[5]=LCDData[str[6]];
	   second_line[7]=LCDData[str[7]];
	   second_line[8]=LCDData[str[8]];
	   second_line[10]=LCDData[str[9]];
	   second_line[11]=LCDData[str[10]]; 	
       LCD_52=LCDData[(str2[0]/10)%10];
       LCD_42=LCDData[str2[0]%10];
       LCD_22=LCDData[(str2[2]/10)%10];	
       LCD_12=LCDData[str2[2]%10];          				
	   first_line[9]=LCD_52;					
	   first_line[10]=LCD_42;
	   first_line[11]='%';
	   first_line[13]=LCD_22;
	   first_line[14]=LCD_12;
	      display();
}
 void display_tiaoshi(void)
{	        
	   str[5]=timedata[2]/10; 
	   str[6]=timedata[2]%10;
	   str[7]=timedata[1]/10; 
	   str[8]=timedata[1]%10; 
	   second_line[4]=LCDData[str[5]];
	   second_line[5]=LCDData[str[6]]; 
	   second_line[7]=LCDData[str[7]];
	  second_line[8]=LCDData[str[8]]; 	       
	   display();
}

void tiaoshi() 
{
 while(1)
{ display_tiaoshi();
 
  if(K5==0)
  {
    con++;
    }
   if(con==1) //shi
     {
	  
	  if(K6==0) timedata[2]++; display_tiaoshi();delay1ms(30);
	  if (timedata[2]==23) 
	   {display_tiaoshi();delay1ms(30);timedata[2]=0;display_tiaoshi();
	    timedata[2]++;display_tiaoshi();
	   }
      if(K7==0) timedata[2]--;display_tiaoshi();delay1ms(30);
	   if (timedata[2]==0) 
	   {timedata[2]=23;delay1ms(30);timedata[2]--;display_tiaoshi();
	   }
     }

  if(con==2) //fen
     {if(K6==0) timedata[1]++;display_tiaoshi();delay1ms(30);
	  if (timedata[1]==59) 
	  {display_tiaoshi();delay1ms(30); timedata[2]++;timedata[1]++;timedata[1]=0;timedata[1]++;
	  display_tiaoshi();delay1ms(30);
	   }
      if(K7==0) timedata[1]--;display_tiaoshi();delay1ms(30);
	   if (timedata[1]==0) 
	   {timedata[1]=59;delay1ms(30); timedata[1]--;display_tiaoshi();//delay1ms(30);
	   }
	 }	
  
  if(con==3)
     {con=0;EA=1;return;
	 }
 }
}
void  COM1()
   {
     U8 i;
     for(i=0;i<8;i++)	   
	    {
		 FLAG1=2;	
	   	while((!DHT1)&&FLAG1++);
			Delay_10us();
		    Delay_10us();
			Delay_10us();
	  		temp1=0;
	     if(DHT1)temp1=1;
		    FLAG1=2;
		 while((DHT1)&&FLAG1++);
          if(FLAG1==1)break;
		   comdata1<<=1; 
	   	   comdata1|=temp1;       
	     }
	  }
	void RH1()
	{
       DHT1=0;
	  delay1ms(20); 
	   DHT1=1;
       Delay_10us();
	   Delay_10us();
	   Delay_10us();
	   Delay_10us();
       DHT1=1;
 if(!DHT1)//是否响应信号		
	   {
	  FLAG1=2;
while((!DHT1)&&FLAG1++);
	   FLAG1=2;
while((DHT1)&&FLAG1++);
COM1();	
	  HH_1=comdata1;
	   COM1(); 
	   HL_1=comdata1;
	   COM1();	 
	  TH_1=comdata1;
	   COM1();
	   TL_1=comdata1;
	   COM1();
	   checkdata1=comdata1;
	   DHT1=1;
temp1=(TH_1+TL_1+HH_1+HL_1);
	   if(temp1==checkdata1)
	   {
	   	  HZ1=HH_1;
	   	  HX1=HL_1;
		  TZ1=TH_1;
	   	  TX1=TL_1;
	   	  check1=checkdata1;
	   }
	  }
}
        void  COM2()
      {
       U8 i;
          for(i=0;i<8;i++)	   
	    {
		FLAG2=2;	
	   	while((!DHT2)&&FLAG2++);
			Delay_10us();
		    Delay_10us();
			Delay_10us();
	  		temp2=0;
	     if(DHT2)temp2=1;
		    FLAG2=2;
		 while((DHT2)&&FLAG2++);
	 if(FLAG2==1)break;
	  comdata2<<=1;
	   	   comdata2|=temp2;     
	     }
	 }
void RH2()
	{
	  DHT2=0;
	 delay1ms(20); 
	   DHT2=1;
       Delay_10us();
	   Delay_10us();
	   Delay_10us();
	   Delay_10us();
       DHT2=1;
       if(!DHT2)		 
	{
	   FLAG2=2;
	while((!DHT2)&&FLAG2++);
	   FLAG2=2;
   while((DHT2)&&FLAG2++);
COM2();
	   HH_2=comdata2;
 COM2();
	   HL_2=comdata2;
COM2();
	   TH_2=comdata2;
 COM2();
	   TL_2=comdata2;
COM2();
	   checkdata2=comdata2;
	   DHT2=1;
temp2=(TH_2+TL_2+HH_2+HL_2);
	   if(temp2==checkdata2)
	   {
	   	  HZ2=HH_2;
	   	  HX2=HL_2;
		  TZ2=TH_2;
	   	  TX2=TL_2;
	   	  check2=checkdata2;
	   }
	   }
}
void cunchu()//
{
    a1=HZ1;a11=TZ1;b1=HZ2;b11=TZ2;c1=timedata[2];c11=timedata[1];c111=timedata[0];  
    shuju[p][0]='X';shuju[p][1]=LCDData[a1/10];shuju[p][2]=LCDData[a1%10];shuju[p][3]='%';
    shuju[p][4]=LCDData[a11/10];shuju[p][5]=LCDData[a11%10];shuju[p][6]=' ';shuju[p][7]=' ';
	shuju[p][8]=' ';shuju[p][9]=' ';shuju[p][10]='Y';
	shuju[p][11]=LCDData[b1/10];shuju[p][12]=LCDData[b1%10];shuju[p][13]='%';
	shuju[p][14]=LCDData[b11/10];;shuju[p][15]=LCDData[b11%10];
	shuju[p][17]=' ';;shuju[p][18]=' ';shuju[p][19]=' ';shuju[p][20]=' ';
    shuju[p][21]=LCDData[c1/10];shuju[p][22]=LCDData[c1%10];shuju[p][23]=':';
	shuju[p][24]=LCDData[c11/10];shuju[p][25]=LCDData[c11%10];shuju[p][26]=':';
	shuju[p][27]=LCDData[c111/10];shuju[p][28]=LCDData[c111%10];
	shuju[p][29]=' ';;shuju[p][30]=' ';shuju[p][31]=' ';	
 	p++;
	if(p>4) p=0;
 }
void  caxun()
{U8  i;	
 while(1)
 {
  if(K8==0)
    {
    DD++;
    }
  if(DD==1)
  {	 write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(shuju[0][i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=16;i<32;i++)
		{
    write_data(shuju[0][i]);
		}
	   delay1ms(30);
  
    }
  if(DD==2)
  {write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(shuju[1][i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=16;i<32;i++)
		{
    write_data(shuju[1][i]);
		}
   	   delay1ms(30);
    }
 if(DD==3)
  {	write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(shuju[2][i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=16;i<32;i++)
		{
    write_data(shuju[2][i]);
		}
   		delay1ms(30);
    }
  if(DD==4)
  {write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(shuju[3][i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=16;i<32;i++)
		{
    write_data(shuju[3][i]);
		}
   		delay1ms(30);
    }
  if(DD==5)
  {write_command(0x80);
	    for(i=0;i<16;i++)
		{
	write_data(shuju[4][i]);		  	  	              	  
		}
    write_command(0xc0);
	    for(i=16;i<32;i++)
		{
    write_data(shuju[4][i]);
		}
   	   delay1ms(30);
    } 
    
  if(DD==6)
     {DD=0;return;
	 }
	 }}
void BAOJING()
{  	 if((HUM_H1<str[0])||(str[0]<HUM_L1)) 
	 { hum_alarm_led1 = 0; }
	  else{hum_alarm_led1 = 1;}
     if((TEMP_H1<str[2])||(TEMP_H1<str[2]))
		 { temp_alarm_led1= 0;}
	   else {temp_alarm_led1=1;}
     if((HUM_H2<str2[0])||(str2[0]<HUM_L2))
	       { hum_alarm_led2=0;}
	   else {hum_alarm_led2=1;}
	  if((TEMP_H2<str2[2])||(str2[2]<TEMP_L2))
	    { temp_alarm_led2= 0;}
	   else
           {temp_alarm_led2=1;}

			  if(HUM_H1<str[0])	
			  first_line[4]='G';
			  else if(str[0]<HUM_L1)
			  first_line[4]='d';
			  else first_line[4]=' ';

			  if(TEMP_H1<str[2])
			  first_line[7]='G';
			  else if(str[2]<TEMP_L1)
			  first_line[7]='d';
			  else first_line[7]=' ';

			 if(HUM_H1<str2[0])	
			  first_line[12]='G';
			  else if(str2[0]<HUM_L1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -