仅供学习师兄参考,文件中包括原理图和程序,有问题评论
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "common.h"
- #include "lcd1602.h"
- #include "dht.h"
- extern unsigned char U8RH_data_H;
- extern unsigned char U8T_data_H;
- sbit key1 = P1^0;//设置键
- sbit key2 = P1^1;//加键
- sbit key3 = P1^2;//减键
- sbit relay1 = P1^6;//加热继电器
- sbit relay2 = P1^7;//降温继电器
- sbit relay3 = P3^6;//加湿继电器
- sbit relay4 = P3^7;//除湿继电器
- sbit beep = P2^0;//蜂鸣器
- unsigned char tl = 10,th = 35,hl = 15,hh = 70;//温湿度上下限初始值
- unsigned char setn = 0;//设置标志
- void display_alarm()//显示报警值
- {
- lcd_write_char(4,0,tl/10+0x30);
- lcd_write_char(5,0,tl%10+0x30);
- lcd_write_char(13,0,th/10+0x30);
- lcd_write_char(14,0,th%10+0x30);
- lcd_write_char(4,1,hl/10+0x30);
- lcd_write_char(5,1,hl%10+0x30);
- lcd_write_char(13,1,hh/10+0x30);
- lcd_write_char(14,1,hh%10+0x30);
- }
- void keyscan()
- {
- if(key1 == 0)//设置键按下
- {
- delay_ms(10);
- if(key1 == 0)
- {
- beep = 0;
- delay_ms(100);
- beep = 1;
- while(key1 == 0);//等待按键松开
- setn++;
- if(setn > 4)//按下次数超过4次,退出设置
- {
- setn = 0;
- lcd_write_com(0x0c);//关闭光标
- lcd_write_str(0,0,"Tem: C Hum: %");
- lcd_write_str(0,1," ");
- }
- if(setn == 1)//设置温度下限
- {
- lcd_write_str(0,0,"Tml: C Tmh: C");
- lcd_write_str(0,1,"Hml: % Hmh: %");
- display_alarm();
- lcd_write_com(0x80+5);
- lcd_write_com(0x0f);
- }
- if(setn == 2)//温度上限
- {
- lcd_write_com(0x80+14);
- lcd_write_com(0x0f);
- }
- if(setn == 3)//湿度下限
- {
- lcd_write_com(0x80+0x40+5);
- lcd_write_com(0x0f);
- }
- if(setn == 4)//湿度上限
- {
- lcd_write_com(0x80+0x40+14);
- lcd_write_com(0x0f);
- }
- }
- }
- if(key2 == 0)//加键按下
- {
- delay_ms(10);
- if(key2 == 0)
- {
- beep = 0;
- delay_ms(100);
- beep = 1;
- while(key2 == 0);
- if(setn == 1)
- {
- if(th - tl > 1)//上限必须大于下限,下限才能加
- tl ++;
- display_alarm();
- lcd_write_com(0x80+5);
- lcd_write_com(0x0f);
- }
- if(setn == 2)
- {
- if(th < 99)//最大到99
- th++;
- display_alarm();
- lcd_write_com(0x80+14);
- lcd_write_com(0x0f);
- }
- if(setn == 3)
- {
- if(hh - hl > 1)//上限必须大于下限,下限才能加
- hl++;
- display_alarm();
- lcd_write_com(0x80+0x40+5);
- lcd_write_com(0x0f);
- }
- if(setn == 4)
- {
- if(hh < 99)//最大到99
- hh++;
- display_alarm();
- lcd_write_com(0x80+0x40+14);
- lcd_write_com(0x0f);
- }
- }
- }
- if(key3 == 0)//减键按下
- {
- delay_ms(10);
- if(key3 == 0 )
- {
- beep = 0;
- delay_ms(100);
- beep = 1;
- while(key3 == 0);
- if(setn == 1)
- {
- if(tl > 0)//最小到0
- tl --;
- display_alarm();
- lcd_write_com(0x80+5);
- lcd_write_com(0x0f);
- }
- if(setn == 2)
- {
- if(th - tl > 1)//上限必须大于下限,上限才能减
- th--;
- display_alarm();
- lcd_write_com(0x80+14);
- lcd_write_com(0x0f);
- }
- if(setn == 3)
- {
- if(hl > 0)
- hl--;
- display_alarm();
- lcd_write_com(0x80+0x40+5);
- lcd_write_com(0x0f);
- }
- if(setn == 4)
- {
- if(hh - hl > 1)//上限必须大于下限,上限才能减
- hh--;
- display_alarm();
- lcd_write_com(0x80+0x40+14);
- lcd_write_com(0x0f);
- }
- }
- }
- }
- void main()
- {
- unsigned int count;
- short temperature;
- short humidity;
- lcd_init();//1602液晶初始化
- lcd_write_str(0,0,"Tem: C Hum: %");
- lcd_write_str(0,1," ");
- do
- {
- RH();delay_ms(100);
- }while(U8RH_data_H==0&&U8T_data_H==0);
- while(1)
- {
- keyscan();//按键扫描
- if(count++>=10 && setn == 0)
- {
- count=0;
- RH();
-
- //读出温湿度,只取整数部分
- humidity = U8RH_data_H;
- temperature = U8T_data_H;
-
- lcd_write_char(4,0,temperature/10+0x30);
- lcd_write_char(5,0,temperature%10+0x30);
- lcd_write_char(13,0,humidity/10+0x30);
- lcd_write_char(14,0,humidity%10+0x30);
- if(humidity>=hh)//湿度高于上限,除湿继电器开启。否则关闭
- relay4 = 0;
- else
- relay4 = 1;
- if(humidity<=hl)//湿度低于下限,加湿继电器开启。否则关闭
- relay3 = 0;
- else
- relay3 = 1;
- if(temperature>=th)//温度高于上限,降温继电器开启。否则关闭
- relay2 = 0;
- else
- relay2 = 1;
- if(temperature<=tl)//温度低于下限,加热继电器开启。否则关闭
- relay1 = 0;
- else
- relay1 = 1;
- if(humidity>=hh || humidity<=hl || temperature>=th || temperature<=tl)//温湿度超限
- {
- beep = ~beep;//蜂鸣器报警
- delay_ms(100);
- }
- else
- {
- beep = 1;
- }
- }
- }
- }
复制代码
所有资料51hei提供下载:
仿真.rar
(75.32 KB, 下载次数: 258)
|