单片机课程设计——基于51单片机温湿度检测系统的设计与实现

  1 #include <reg52.h>
  2 #include "1602.h"
  3 #include "dht.h"
  4 #include "2402.h"
  5 
  6 //定义三个LED灯
  7 sbit Led_qushi=P1^6;        //去湿灯
  8 sbit Led_jiangwen=P1^5;        //降温灯
  9 sbit Led_shengwen=P1^4;        //升温灯
 10 sbit Key_TH1 = P3^2;
 11 sbit Key_TH2 = P3^3;
 12 sbit Key_HH1 = P3^4;
 13 sbit Key_HH2 = P3^5;
 14 
 15 //定义标识
 16 volatile bit FlagStartRH = 0;  //开始温湿度转换标志
 17 volatile bit FlagKeyPress = 0; //有键按下
 18 
 19 
 20 //定义温湿度传感器用外部变量
 21 extern U8  U8FLAG,k;
 22 extern U8  U8count,U8temp;
 23 extern U8  U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
 24 extern U8  U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
 25 extern U8  U8comdata;
 26 extern U8  count, count_r;
 27 
 28 U16 temp;
 29 S16 temperature, humidity;
 30 S16 idata TH, HH;  //温度上限和湿度上限
 31 char * pSave;
 32 U8 keyvalue, keyTH1, keyTH2, keyHH1, keyHH2;
 33 
 34 //定义变量
 35 U16 RHCounter;
 36 
 37 
 38 //数据初始化
 39 void Data_Init()
 40 {
 41    RHCounter = 0;
 42    Led_qushi = 1;
 43    Led_jiangwen = 1;
 44    Led_shengwen = 1;
 45    TH = 40;
 46    HH = 85;
 47    keyvalue = 0;
 48    keyTH1 = 1;
 49    keyTH2 = 1;
 50    keyHH1 = 1;
 51    keyHH2 = 1;
 52 
 53 }
 54 
 55 //定时器0初始化
 56 void Timer0_Init()
 57 {
 58     ET0 = 1;        //允许定时器0中断
 59     TMOD = 1;       //定时器工作方式选择
 60     TL0 = 0x06;     
 61     TH0 = 0xf8;     //定时器赋予初值
 62     TR0 = 1;        //启动定时器
 63 }
 64 
 65 //定时器0中断
 66 void Timer0_ISR (void) interrupt 1 using 0
 67 {
 68     TL0 = 0x06;
 69     TH0 = 0xf8;     //定时器赋予初值
 70 
 71     //每2秒钟启动一次温湿度转换
 72     RHCounter ++;
 73     if (RHCounter >= 1000)
 74     {
 75        FlagStartRH = 1;
 76        RHCounter = 0;
 77     }
 78 }
 79 
 80 //存入设定值、
 81 void Save_Setting()
 82 {
 83    pSave =  (char *)&TH;      //地址低位对应低8位,高位对应高8位
 84    wrteeprom(0, *pSave);      //存温度上限值TH低8位
 85    DELAY(500);
 86    pSave ++;
 87    wrteeprom(1, *pSave);      //存温度上限值TH高8位
 88    DELAY(500);
 89    pSave =  (char *)&HH;
 90    wrteeprom(2, *pSave);      //存湿度上限值RH低8位
 91    DELAY(500);
 92    pSave ++;
 93    wrteeprom(3, *pSave);      //存湿度上限值RH高8位
 94    DELAY(500);
 95 }
 96 
 97 //载入设定值、
 98 void Load_Setting()
 99 {
100    pSave =  (char *)&TH;
101    *pSave++ = rdeeprom(0);
102    *pSave = rdeeprom(1);
103    pSave = (char *)&HH;
104    *pSave++ = rdeeprom(2);
105    *pSave = rdeeprom(3);
106    if ((TH>99)||(TH<0)) TH = 40;
107    if ((HH>99)||(HH<0)) HH = 85;
108 }
109 
110 void KeyProcess(uint num)
111 {
112    switch (num)
113    {
114       case 1:
115          if (TH<99) TH++;
116          L1602_char(1, 15, TH/10+48);
117          L1602_char(1, 16, TH%10+48);
118          break;
119       case 2:
120          if (TH>1) TH--;
121          L1602_char(1, 15, TH/10+48);
122          L1602_char(1, 16, TH%10+48);
123          break;
124       case 3:
125          if (HH<99) HH++;
126          L1602_char(2, 15, HH/10+48);
127          L1602_char(2, 16, HH%10+48);
128          break;
129       case 4:
130          if (HH>1) HH--;
131          L1602_char(2, 15, HH/10+48);
132          L1602_char(2, 16, HH%10+48);
133          break;
134       default:
135          break;
136    }
137    Save_Setting();
138 }
139 
140 /********************************************************************
141 * 名称 : Main()
142 * 功能 : 主函数
143 ***********************************************************************/
144 void main()
145 {
146     U16 i, j, testnum;
147 
148     EA = 0;
149 
150     Timer0_Init();  //定时器0初始化
151 
152     Data_Init();
153     EA = 1;
154 
155     L1602_init();
156     L1602_string(1,1," Welcome to T&H   ");
157     L1602_string(2,1," Control System!  ");
158     //延时
159     for (i=0;i<500;i++)
160        for (j=0;j<1000;j++)
161        {;}
162     //清屏
163     L1602_string(1,1,"                ");
164     L1602_string(2,1,"                ");
165     L1602_string(1,1,"Tem:    C  TH:");
166     L1602_string(2,1,"Hum:    %  HH:");
167     
168     //载入温度上限和湿度上限设定值
169     Load_Setting();
170     L1602_char(1, 15, TH/10+48);
171     L1602_char(1, 16, TH%10+48);
172     L1602_char(2, 15, HH/10+48);
173     L1602_char(2, 16, HH%10+48);
174 
175 
176     while(1)
177     {
178        //温湿度转换标志检查
179          if (FlagStartRH == 1)
180          {
181              TR0 = 0;
182              testnum = RH();
183               FlagStartRH = 0;
184              TR0 = 1;
185              //读出温湿度,只取整数部分
186              humidity = U8RH_data_H;
187              temperature = U8T_data_H;
188               //显示温湿度
189              L1602_int(1,5,temperature);
190              L1602_int(2,5,humidity);    
191         }
192         //温湿度控制
193         if (temperature > TH) Led_jiangwen = 0;
194         else Led_jiangwen = 1;                    //降温
195         if (humidity > HH) Led_qushi = 0;
196         else Led_qushi = 1;                        //去湿
197          
198         //键盘查询,在弹起时响应
199         if ((Key_TH1)&&(keyTH1==0)) {FlagKeyPress = 1; keyvalue = 1;}
200         else if ((Key_TH2)&&(keyTH2==0)) {FlagKeyPress = 1; keyvalue = 2;}
201         else if ((Key_HH1)&&(keyHH1==0)) {FlagKeyPress = 1; keyvalue = 3;}
202         else if ((Key_HH2)&&(keyHH2==0)) {FlagKeyPress = 1; keyvalue = 4;}
203         if (FlagKeyPress == 1)
204         {
205            KeyProcess(keyvalue);
206            FlagKeyPress = 0;           
207         }
208         if (!Key_TH1) keyTH1 = 0;
209         else keyTH1 = 1;
210         if (!Key_TH2) keyTH2 = 0;
211         else keyTH2 = 1;
212         if (!Key_HH1) keyHH1 = 0;
213         else keyHH1 = 1;
214         if (!Key_HH2) keyHH2 = 0;
215         else keyHH2 = 1;
216     }    
217 }

猜你喜欢

转载自www.cnblogs.com/wenzhixin/p/10798748.html