温室控制仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include"sht10.h"
- #include"ds1302.h"
- #include"lcd1602.h"
- unsigned char TempBuffer[4];
- unsigned char humBuffer[4];
- unsigned char set[4];
- unsigned char set_temp=20,set_hum=20;
- unsigned char mode=6;
- //unsigned char Key3_val=0;
- sbit JIARE=P2^5;//定义加热 降温 加湿 干燥四个端口
- sbit JIANGWEN=P2^4;
- sbit JIASHI=P2^6;
- sbit GANZAO=P2^7;
- sbit JIARE_FLAG=P1^5;//定义模式标志位
- sbit JIANGWEN_FLAG=P1^6;
- sbit JIASHI_FLAG=P1^7;
- sbit GANZAO_FLAG=P3^6;
- sbit ZIDONG_FLAG=P3^7;
- sbit KEY0=P3^2;//定义按键
- sbit KEY1=P3^3;
- sbit KEY2=P3^4;
- void delay_ms(unsigned char k)//延时k ms
- {
- unsigned char i,j;
- for(i=0;i<k;i++)
- for(j=0;j<110;j++);
- }
- //INT 数据转字符串
- /*t为int型数据用n个字节存放*/
- void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
- {
- unsigned char a[5]; char i, j;
- a[0]=(t/10000)%10;
- a[1]=(t/1000)%10;
- a[2]=(t/100)%10;
- a[3]=(t/10)%10;
- a[4]=(t/1)%10;
-
- for(i=0; i<5; i++)
- a[i]=a[i]+'0';
- for(i=0; a[i]=='0' && i<=3; i++);
- for(j=5-n; j<i; j++)
- { *str=' '; str++; }
- for(; i<5; i++)
- { *str=a[i]; str++; }
- *str='\0';
- }
- void Int_init(void)//初始化外部中断
- {
- IT0=1;
- EX0=1;
- IT1=1;
- EX1=1;
- PX1=1;
- EA=1;
- }
- void TH_set(void)//显示设定的温湿度
- {
- GotoXY(6,1);
- IntToStr(set_temp,&set[0],2);
- Print(set);
- GotoXY(10,1);
- IntToStr(set_hum,&set[0],2);
- Print(set);
- GotoXY(15,1);
- IntToStr(mode,&set[0],1);
- Print(set);
- }
- void Key2Scan(void)//按键扫描
- {
- static unsigned char key_status=1;
- if(key_status&&(KEY2==0))
- {
- delay_ms(5);
- if(KEY2==0)
- { key_status=0;
- if(mode==6){set_temp--;if(set_temp<16)set_temp++;}
- else if(mode==7){set_hum--;if(set_hum<20)set_hum++;}
- }
- }else if(KEY2)key_status=1;
- }
- void LED_show_mode(void)//模式指示灯显示
- {
- if(mode==1){JIARE_FLAG=0;JIANGWEN_FLAG=1;JIASHI_FLAG=1;GANZAO_FLAG=1;ZIDONG_FLAG=1;}
- else if(mode==2){JIARE_FLAG=1;JIANGWEN_FLAG=0;JIASHI_FLAG=1;GANZAO_FLAG=1;ZIDONG_FLAG=1;}
- else if(mode==3){JIARE_FLAG=1;JIANGWEN_FLAG=1;JIASHI_FLAG=0;GANZAO_FLAG=1;ZIDONG_FLAG=1;}
- else if(mode==4){JIARE_FLAG=1;JIANGWEN_FLAG=1;JIASHI_FLAG=1;GANZAO_FLAG=0;ZIDONG_FLAG=1;}
- else if(mode==5){JIARE_FLAG=1;JIANGWEN_FLAG=1;JIASHI_FLAG=1;GANZAO_FLAG=1;ZIDONG_FLAG=0;}
- else if(mode==6){JIARE_FLAG=1;JIANGWEN_FLAG=1;JIASHI_FLAG=1;GANZAO_FLAG=1;ZIDONG_FLAG=1;}
- else if(mode==7){JIARE_FLAG=1;JIANGWEN_FLAG=1;JIASHI_FLAG=1;GANZAO_FLAG=1;ZIDONG_FLAG=1;}
- }
- void ModeNot5_handler(void)//模式1到4的处理函数
- {
- if(mode==1){JIARE=1;JIANGWEN=0;JIASHI=0;GANZAO=0;}
- else if(mode==2){JIARE=0;JIANGWEN=1;JIASHI=0;GANZAO=0;}
- else if(mode==3){JIARE=0;JIANGWEN=0;JIASHI=1;GANZAO=0;}
- else if(mode==4){JIARE=0;JIANGWEN=0;JIASHI=0;GANZAO=1;}
- // else if(mode==6){JIARE=0;JIANGWEN=0;JIASHI=0;GANZAO=0;}
- // else if(mode==7){JIARE=0;JIANGWEN=0;JIASHI=0;GANZAO=0;}
- }
- void Mode5handler(void)//模式5到7的处理函数
- {
- if(mode>=5)
- {
- if(shidu>set_hum){JIASHI=0;GANZAO=1;}
- else if(shidu<set_hum){JIASHI=1;GANZAO=0;}
- else if(shidu==set_hum){JIASHI=0;GANZAO=0;}
-
- if(wendu>set_temp){JIARE=0;JIANGWEN=1;}
- else if(wendu<set_temp){JIARE=1;JIANGWEN=0;}
- else if(wendu==set_temp){JIARE=0;JIANGWEN=0;}
- }
- }
- void main()
- {
- JIARE=JIANGWEN=JIASHI=GANZAO=0;
- Int_init();
- LCD_Initial();
- s_connectionreset();//sht10的初始化
- GotoXY(0,0);
- Print("Real:T: R: %");
- GotoXY(0,1);
- Print("Set:T: R: %M: ");
- TH_set();
- while(1)
- {
-
- Get_TH();//获取温湿度
- GotoXY(7,0);
- IntToStr(wendu,&TempBuffer[0],2);
- Print(TempBuffer);
- GotoXY(12,0);
- IntToStr(shidu,&humBuffer[0],2);
- Print(humBuffer);
-
- TH_set();
- LED_show_mode();
- ModeNot5_handler();
- Mode5handler();
- Key2Scan();
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
智能温室控制系统.rar
(193.99 KB, 下载次数: 132)
动态图片演示.rar
(8.41 MB, 下载次数: 82)
proteus8链接:游客,本帖隐藏的内容需要积分高于 10 才可浏览,您当前积分为 0
|