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

📄 +++

📁 可设置温度上下限的温控
💻
字号:
#include"STC89C51RC.H"
#include<absacc.h>
#include"ds18b20.h"
#include"lcd1602.h"
#include"ds1302.h"
#include"2402.h"	
uchar data datadis[]={0x00,0x00,0x00,0x00,0x00};
uint temp=0;
sbit K1=P3^5;
sbit K2=P3^4;
sbit K3=P3^3;
sbit K4=P3^2;
sbit K5=P3^0;
sbit L1=P1^6;
sbit speaker=P1^0;
uchar keynum=1,flag,flag1,flag2,adder,q;
uint k,count,th,tl;
uchar th1=8;
uchar th2=5;
uchar th3=2;
uchar th4=0;
uchar tab,w;
void delay(uchar z)
{
	uchar x,y;
	for(x=z;x>0;x--)
	  for(y=110;y>0;y--);
}
uchar key4()
{
	if(K4==0)
	  {
	  	delay(5);
		if(K4==0)
		  {
		  	 while(!K4);
			 keynum++;
			 if(keynum==5)keynum=1;
		  }
	  }
	return keynum;
}
void key_lcd()		
{
 if(flag1==1&&k==3)			   //k一定不能掉,不然会在其他菜单里出现乱码
 {
   if(K3==0)					//按键判断,选择设置温度的光标
	  {
	  	delay(5);
		if(K3==0)
		  {
		  	 while(!K3);
			 flag++;		   //按键标识位
			 if(flag==5)flag=1;
		  }
	  }
   switch(flag)
     {
	 	case 1:
		   {
			 if(K1==0)
			  {
			  	delay(5);
				if(K1==0)
				  {	 
				  	 th1++;
				  	 while(!K1);
					 if(th1==10)th1=0;
				  }
			  }
			if(K2==0)
			  {
			  	delay(5);
				if(K2==0)
				  {	 
				  	 th1--;
				  	 while(!K2);
					 if(th1==-1)th1=9;
				  }
			  }
			 write_cmd(0xc3);
			 write_dat(th1+0x30);
		   }break;
		case 2:
		   {
		   	 if(K1==0)
			  {
			  	delay(5);
				if(K1==0)
				  {	 
				  	 th2++;
				  	 while(!K1);
					 if(th2==10)th2=0;
				  }
			  }
			if(K2==0)
			  {
			  	delay(5);
				if(K2==0)
				  {	 
				  	 th2--;
				  	 while(!K2);
					 if(th2==-1)th2=9;
				  }
			  }
		     write_cmd(0xc4);
			 write_dat(th2+0x30);
		   }break;
		case 3:
		   {
		     if(K1==0)
			  {
			  	delay(5);
				if(K1==0)
				  {	 
				  	 th3++;
				  	 while(!K1);
					 if(th3==10)th3=0;
				  }
			  }
			if(K2==0)
			  {
			  	delay(5);
				if(K2==0)
				  {	 
				  	 th3--;
				  	 while(!K2);
					 if(th3==-1)th3=9;
				  }
			  }
			 write_cmd(0xcb);
			 write_dat(th3+0x30);
		   }break;
		case 4:
		   {
		     if(K1==0)
			  {
			  	delay(5);
				if(K1==0)
				  {	 
				  	 th4++;
				  	 while(!K1);
					 if(th4==10)th4=0;
				  }
			  }
			if(K2==0)
			  {
			  	delay(5);
				if(K2==0)
				  {	 
				  	 th4--;
				  	 while(!K2);
					 if(th4==-1)th4=9;
				  }
			  }
			 write_cmd(0xcc);
			 write_dat(th4+0x30);
		   }break;
		default:break;
	 }
   }
  else flag1=0;        //m一定不能掉,否则在调节温度的时候会影响其他菜单
  th=th1*10+th2;   //获取设置温度的高位
  tl=th3*10+th4;   //获取设置温度的低位
}

void alarm_speaker()		   //报警系统与当前温度进行比较并处理
{	
	if(temp>(th*10)||temp<(tl*10))
	  {
	  	speaker=~speaker;
		L1=0;
		adder++;
		tab=temp/10;
		write_add(adder,tab);
	  }	
	else 
	  {
	  	speaker=1;
		L1=1;
	  }
}

void main()
{	
	systemtime realtime;
	init_18b20();
	init_lcd();
	init_2402();
	speaker=1;
	while(1)
	 {		
		k=key4(); 
		get_ds1302(&realtime);//取时间
		temp=read_temp();	   //取温度
		delay(200);			  //延时 保持数据稳定 避免数据刷新
		switch(k)
		 {
		   case 1:			   //菜单一,显示时间
		     {
				write_cmd(0x80);
			    print(" Time: ");
			    print(realtime.TimeString);
				write_cmd(0xc0);
			    print(" Date: "); 
			    print(realtime.DateString);	 			   
			 } break;	 
		   case 2:			  //菜单二,显示温度
		     {	 
			    write_cmd(0x80);
			    print(" DS18B20 is OK  ");
				write_cmd(0xc0);
		        print("NOW TEMP:  . C ");	
				if(flagt==1)datadis[0]=0x2d;
				else datadis[0]=temp/1000+0x30;
				datadis[1]=temp/100%10+0x30;
				datadis[2]=temp/10%10+0x30;
				datadis[3]=0x2e;
				datadis[4]=temp%10+0x30;
				write_cmd(0xca);
				print(datadis);	
		      }break; 	
		   case 3:				   //菜单三,显示温度设置值
		    {
				write_cmd(0x80);
				print(" change temp:   ");
				write_cmd(0xc0);
				print("TH=     TL=     ");
				write_cmd(0xc3);
				write_dat(th1+0x30);
				write_cmd(0xc4);
				write_dat(th2+0x30);
				write_cmd(0xc5);
				write_dat(0xdf);		 //摄氏度远点
				write_cmd(0xc6);
				write_dat(0x43);
				write_cmd(0xcb);
				write_dat(th3+0x30);
				write_cmd(0xcc);
				write_dat(th4+0x30);
				write_cmd(0xcd);		 //摄氏度符号
				write_dat(0xdf);		 
				write_cmd(0xce);
				write_dat(0x43);
				flag1=1;  
			}break;
		  case 4:				   //菜单四,记录报警温度
		    {
				write_cmd(0x80);
				print("  ALARM TEMP     ");
				write_cmd(0xc0);
				print("  RECORD:        ");
				write_cmd(0xcb);
				write_dat(0xdf);		 
				write_cmd(0xcc);
				write_dat(0x43);
				flag2=1;
			}break;	 
		  default:break;	  
		 }
		key_lcd();				//菜单三中温度设置的按键扫描
		alarm_speaker();  		//报警系统通过2402存储超出的温度,并记录
	    if(K5==0)				//按下K5,就显示当前报警的温度
		  {
		  	delay(5);
		    if(K5==0)
			  {
			  	while(!K5);
				q=read_add(adder);
			  }
		  };
		if(flag2==1&&k==4)		   //k一定不能掉,不然会在其他菜单里出现乱码,菜单四中需要显示存储的报警数据
		 {								
		 	write_cmd(0xc9);
			write_dat(q/10%10+0x30);
			write_cmd(0xca);
			write_dat(q%10+0x30);
		 }
			 
  	  }
}

⌨️ 快捷键说明

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