|
#include<reg52.h>
#include <intrins.h> //庫文件
#include <math.h> //庫文件
#include <stdio.h> //庫文件
#include <1602.h> //lcd1602頭文件
#include <sht11.h> //sht11頭文件
#include <24c02.h> //24c02頭文件
#define uchar unsigned char
#define uint unsigned int
#define W_cmd 0xa0 //24c02寫指令
#define R_cmd 0xa1 //24c02讀指令
uchar temp,humi,gas; //定義溫度,濕度,氣體濃度
value humi_val,temp_val; //定義兩個共同體,一個用于濕度,一個用于溫度
uchar error; //檢驗是否出現錯誤
uchar checksum; //CRC
uchar nn; //用于蜂鳴器
uchar TEMP_data[7]; //用于記錄實測的溫度
uchar HUMI_data[6]; //用于記錄實測的濕度
uchar show_temp[10]; //保存設定的溫度
uchar show_humi[8]; //保存設定的濕度
uchar show_gas[8]; //保存設定的氣體濃度
uchar idata temp_humi_gas_cache[12]; //溫濕度和氣體濃度設置緩存
uchar temp_high; //溫度上限變量
uchar temp_low; //溫度下限變量
uchar humi_high; //濕度上限變量
uchar humi_low; //濕度下限變量
uchar gas_high; //氣體濃度上限變量
uchar gas_low; //氣體濃度下限變量
uchar presskeynum; //按鍵次數變量
uchar code word1[]={" a product of "};
uchar code word2[]={"husijia"};
uchar code word3[]={" Welcome"};
sbit k1=P3^2; //選擇按鍵
sbit k2=P3^3; //增加按鍵
sbit k3=P3^4; //減小按鍵
sbit k4=P3^5; //確認按鍵
sbit led1=P1^0; //溫度過低指示燈報警
sbit led2=P1^1; //溫度過高指示燈報警
sbit led3=P1^2; //濕度低于下限值,報警,加濕
sbit led4=P1^3; //濕度過高,報警,不調濕
sbit beep=P2^5; //定義蜂鳴器
sbit motor1=P1^4; //加熱電機
sbit motor2=P1^5; //降溫電機
sbit motor3=P1^6; //加濕電機
sbit motor4=P1^7; //干燥電機
sbit P2_6=P2^6;
/*************************************************************************/
//****************************延時函數************************************/
void delay(uint z) //z為毫秒數
{
int a,b;
for(a=z;a>0;a--)
for(b=120;b>0;b--);
}
/*************************************************************************/
//****************************按鍵函數************************************/
void key()
{
uchar m;
if (k1==0) //調整按鍵檢測
{
delay(10);
if (k1==0)
{
presskeynum++;
if(presskeynum==7)
presskeynum=0;
while(k1==0); //若一直按下,循環
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
}
}
if(presskeynum==1) //第一次按下溫度上限設置
{
if(k2==0) //數字加
{
delay(300); //延時
temp_high++;
if (temp_high>90)
temp_high=0;
if (temp_high<0)
temp_high=90;
while(k2==0); //按下后松開才做處理
}
if(k3==0) //數字減
{
delay(300);
temp_high--;
if (temp_high>90)
temp_high=0;
if (temp_high<0)
temp_high=90;
while(k3==0); //按下后松開才做處理
}
}
if(presskeynum==2) //第二次按下溫度下限設置
{
if(k2==0) //數字加
{
delay(300);
temp_low++;
if (temp_low>90)
temp_low=0;
if (temp_low<0)
temp_low=90;
while(k2==0);
}
if(k3==0) //數字減
{
delay(300);
temp_low--;
if (temp_low>90)
temp_low=0;
if (temp_low<0)
temp_low=90;
while(k3==0);
}
}
if(presskeynum==3) //第三次按下濕度上限設置
{
if(k2==0) //數字加
{
delay(300);
humi_high++;
if (humi_high>50)
humi_high=0;
if (humi_high<0)
humi_high=50;
while(k2==0);
}
if(k3==0) //數字減
{
delay(300);
humi_high--;
if (humi_high>50)
humi_high=0;
if (humi_high<0)
humi_high=50;
while(k3==0);
}
}
if(presskeynum==4) //第四次按下濕度下限設置
{
if(k2==0) //數字加
{
delay(300);
humi_low++;
if (humi_low>50)
humi_low=0;
if (humi_low<0)
humi_low=50;
while(k2==0);
}
if(k3==0) //數字減
{
delay(300);
humi_low--;
if (humi_low>50)
humi_low=0;
if (humi_low<0)
humi_low=50;
while(k3==0);
}
}
if(presskeynum==5) //第五次按下濃度上限設置
{
if(k2==0) //數字加
{
delay(300);
gas_high++;
if (gas_high>30) //因為空氣中二氧化碳占比0.3%,氧氣濃度占比21%,故上限值減少至30
gas_high=0;
if (gas_high<0)
gas_high=30;
while(k2==0);
}
if(k3==0) //數字減
{
delay(300);
gas_high--;
if (gas_high>30)
gas_high=0;
if (gas_high<0)
gas_high=30;
while(k3==0);
}
}
if(presskeynum==6) //第六次按下濃度下限設置
{
if(k2==0) //數字加
{
delay(300);
gas_low++;
if (gas_low>30)
gas_low=0;
if (gas_low<0)
gas_low=30;
while(k2==0);
}
if(k3==0) //數字減
{
delay(300);
gas_low--;
if (gas_low>30)
gas_low=0;
if (gas_low<0)
gas_low=30;
while(k3==0);
}
}
if(k4==0)// 退出鍵
{
delay(10);
if(k4==0)
{
while(k4==0);
presskeynum=0;
temp_humi_gas_cache[0]=temp_high/10; //將設定的溫度上限值十位進行緩存
temp_humi_gas_cache[1]=temp_high%10; //將設定的溫度上限值個位進行緩存
temp_humi_gas_cache[2]=temp_low/10; //將設定的溫度下限值十位進行緩存
temp_humi_gas_cache[3]=temp_low%10; //將設定的溫度下限值個位進行緩存
temp_humi_gas_cache[4]=humi_high/10; //將設定的濕度上限值十位進行緩存
temp_humi_gas_cache[5]=humi_high%10; //將設定的濕度上限值個位進行緩存
temp_humi_gas_cache[6]=humi_low/10; //將設定的濕度下限值十位進行緩存
temp_humi_gas_cache[7]=humi_low%10; //將設定的濕度下限值個位進行緩存
temp_humi_gas_cache[8]=gas_high/10; //將設定的濃度上限值十位進行緩存
temp_humi_gas_cache[9]=gas_high%10; //將設定的濃度上限值個位進行緩存
temp_humi_gas_cache[10]=gas_low/10; //將設定的濃度下限值十位進行緩存
temp_humi_gas_cache[11]=gas_low%10; //將設定的濃度下限值十位進行緩存
for(m=0;m<=7;m++)
{
WriteIIC(W_cmd,0x00+m,temp_humi_gas_cache[m]); //設定值寫入24c02
}
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
}
}
}
/*************************************************************************/
//****************************主函數************************************/
void main()
{
uchar t;
k1=k2=k3=k4=1;
motor1=motor2=motor3=motor4=0; //4個執行器初始化
P2_6=0;
for (t=0;t<12;t++)
temp_humi_gas_cache[t]=ReadIIC(W_cmd,0x00+t,R_cmd); //讀入24c02設定的值
temp_high=temp_humi_gas_cache[0]*10+temp_humi_gas_cache[1]; //溫度上限值=緩存0+緩存1
temp_low=temp_humi_gas_cache[2]*10+temp_humi_gas_cache[3]; //溫度下限值=緩存2+緩存3
humi_high=temp_humi_gas_cache[4]*10+temp_humi_gas_cache[5]; //濕度上限值=緩存4+緩存5
humi_low=temp_humi_gas_cache[6]*10+temp_humi_gas_cache[7]; //濕度下限值=緩存6+緩存7
gas_high=temp_humi_gas_cache[8]*10+temp_humi_gas_cache[9]; //濕度上限值=緩存8+緩存9
gas_low=temp_humi_gas_cache[10]*10+temp_humi_gas_cache[11]; //濕度下限值=緩存10+緩存11
LCD_Initial(); //液晶初始化
GotoXY(0,0);
Print_slow(word1); //lcd1602輸出word1內容
GotoXY(0,1);
Print_slow(word2); //lcd1602輸出word2內容
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
GotoXY(0,0);
Print_slow(word3); //lcd1602輸出word3內容
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
/******************************************************************************************************/
while(1)
{
key();
if(presskeynum==0) //測量溫濕度
{
s_connectionreset(); //啟動連接復位
error=0; //初始化error=0,即沒有錯誤
error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //溫度測量
error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //濕度測量
if(error!=0) //發生錯誤
s_connectionreset(); //如果發生錯誤,系統復位
else
{
temp_val.f=(float)temp_val.i; //轉換為浮點數
humi_val.f=(float)humi_val.i; //轉換為浮點數
calc_sth10(&humi_val.f,&temp_val.f); //修正相對濕度及溫度
temp=temp_val.f*10;
humi=humi_val.f*10;
GotoXY(0,0); //選擇溫度顯示位置
Print("Temp: C"); //5格空格
GotoXY(0,1); //選擇濕度顯示位置
Print("Humi: %"); //5格空格
GotoXY(5,0); //設置溫度顯示位置
TEMP_data[0]=temp/1000+'0'; //溫度百位
if (TEMP_data[0]==0x30) //如果實測溫度百位為0
TEMP_data[0]=0x20; //第一個緩存為空格
TEMP_data[1]=temp%1000/100+'0'; //溫度十位
if (TEMP_data[1]==0x30 && TEMP_data[0]!=0x30) //如果實測溫度十位為0且第一個緩存為0
TEMP_data[1]=0x20; //第二個緩存為空格
TEMP_data[2]=temp%100/10+'0'; //溫度個位
TEMP_data[3]=0x2e; //小數點
TEMP_data[4]=temp%10+'0'; //溫度小數點后第一位
TEMP_data[5]=0xdf; //顯示溫度符號℃
TEMP_data[6]='\0';
Print(TEMP_data); //輸出溫度
GotoXY(5,1); //設置濕度顯示位置
HUMI_data[0]=humi/1000+'0'; //濕度百位
if (HUMI_data[0]==0x30) //如果實測濕度百位為0
HUMI_data[0]=0x20; //第一個緩存輸出為0
HUMI_data[1]=humi%1000/100+'0'; //濕度十位
HUMI_data[2]=humi%100/10+'0'; //濕度個位
HUMI_data[3]='.' ; //小數點
HUMI_data[4]=humi%10+'0'; //濕度小數點后第一位
HUMI_data[5]='\0';
Print(HUMI_data); //輸出濕度
if ((temp/10)<temp_low) //如果溫度小于設定最小值
{
led1=0; //溫度低于設定下限值LED1點亮報警
motor1=0; //啟動加熱電機
led2=1; //LED2暗
motor2=1; //制冷電機停止
if((temp/10)==temp_low) //如果實際溫度等于設定溫度下限值
{
led1=1; //LED1暗
motor1=0; //啟動加熱電機
led2=1; //LED2暗
motor2=1; //制冷電機停止
}
}
if ((temp/10)>temp_high) //如果溫度大于設定最大值
{
led2=0; //溫度高于設定上限值LED2點亮報警
motor2=1; //降溫電機工作
led1=1; //LED1暗
motor1=0; //加熱電機停止
if ((temp/10)==temp_high)
{
led2=1; //LED2暗
motor2=0; //啟動降溫電機
led1=1; //LED1暗
motor1=1; //加熱電機停止
}
}
else if ((temp/10)>temp_low &&((temp/10)<temp_high)) //如果溫度在設定值之間
{
led1=1; //LED1暗
motor1=1; //加熱電機停止
led2=1; //LED2暗
motor2=1; //降溫電機停止
}
if ((humi/10)<humi_low)
{
led3=0; //濕度低于設定下限值LED3點亮報警
motor3=0; //啟動加濕電機
led4=1; //LED4暗
motor4=1; //干燥電機停止
if((humi/10)==humi_low) //如果實際濕度等于設定濕度下限值
{
led3=1; //LED3暗
motor3=0; //啟動加濕電機
led4=1; //LED4暗
motor4=1; //干燥電機停止
}
}
if ((humi/10)>humi_high)
{
led4=0; //濕度高于設定上限值LED4點亮報警
motor4=1; //干燥電機工作
led3=1; //LED3暗
motor3=0; //加濕電機停止
if ((humi/10)==humi_high)
{
led4=1; //LED4暗
motor4=0; //啟動干燥電機
led3=1; //LED3暗
motor3=1; //加濕電機停止
}
}
else if ((humi/10)>humi_low &&((humi/10)<humi_high)) //如果溫度在設定值之間
{
led3=1; //LED3暗
motor3=1; //加濕電機停止
led4=1; //LED4暗
motor4=1; //干燥電機停止
}
} //else
} //if
if (((temp/10)<temp_low)|((temp/10)>temp_high)|((humi/10)<humi_low))
{
for (nn=0;nn<50;nn++)
{
beep=0;
delay(1); //延時1ms
beep=1; //響0.5s
delay(1);
}
}
if ((humi/10)>humi_high)
{
for (nn=0;nn<10;nn++)
{
beep=0;
delay(1); //延時1ms
beep=1; //響0.1s
delay(1);
}
}
}
/******************************************************************************************************/
if(presskeynum==1) //設置溫度上限時顯示的內容
{
set_temp[0]=temp_high/10+0x30; //設定上限溫度十位保存于緩存
set_temp[1]=temp_high%10+0x30; //設定上限溫度個位保存于緩存
set_temp[2]=0xdf; //°
set_temp[3]='C'; //C
set_temp[4]='\0';
GotoXY (0,0); //第一行輸出SET TEMP HIGH:
Print("SET TEMP HIGH:");
GotoXY (0,1); //第二行輸出溫度值
Print(set_temp);
}
if(presskeynum==2) //設置溫度下限時顯示的內容
{
set_temp[5]=temp_low/10+0x30; //設定下限溫度十位保存于緩存
set_temp[6]=temp_low%10+0x30; //設定下限溫度個位保存于緩存
set_temp[7]=0xdf; //°
set_temp[8]='C'; //C
set_temp[9]='\0';
GotoXY (0,0); //第一行輸出SET TEMP LOW:
Print("SET TEMP LOW:");
GotoXY (0,1); //第二行輸出溫度值
Print(set_temp);
}
if(presskeynum==3) //設置濕度上限時顯示的內容
{
set_humi[0]=humi_high/10+0x30; //設定上限濕度十位保存于緩存
set_humi[1]=humi_high%10+0x30; //設定上限濕度個位保存于緩存
set_humi[2]='%'; //%
set_humi[3]='\0';
GotoXY (0,0); //第一行輸出SET HUMI HIGH:
Print("SET HUMI HIGH:");
GotoXY (0,1); //第二行輸出濕度值
Print(set_humi);
}
if(presskeynum==4) //設置濕度下限時顯示的內容
{
set_humi[4]=humi_low/10+0x30; //設定下限濕度十位保存于緩存
set_humi[5]=humi_low%10+0x30; //設定下限濕度個位保存于緩存
set_humi[6]='%'; //%
set_humi[7]='\0';
GotoXY (0,0); //第一行輸出SET HUMI LOW:
Print("SET HUMI LOW:");
GotoXY (0,1); //第二行輸出濕度值
Print(set_humi);
}
if(presskeynum==5) //設置空氣濃度下限時顯示的內容
{
set_gas[0]=gas_low/10+0x30; //設定上限濃度十位保存于緩存
set_gas[1]=gas_low%10+0x30; //設定上限濃度個位保存于緩存
set_gas[2]='%'; //%
set_gas[3]='\0';
GotoXY (0,0); //第一行輸出SET GAS LOW:
Print("SET GAS LOW:");
GotoXY (0,1); //第二行輸出空氣濃度值
Print(set_gas);
}
if(presskeynum==6) //設置空氣濃度上限時顯示的內容
{
set_gas[4]=gas_high/10+0x30; //設定下限濃度十位保存于緩存
set_gas[5]=gas_high%10+0x30; //設定下限濃度個位保存于緩存
set_gas[6]='%'; //%
set_gas[7]='\0';
GotoXY (0,0); //第一行輸出SET GAS HIGH:
Print("SET GAS HIGH:");
GotoXY (0,1); //第二行輸出空氣濃度值
Print(set_gas);
}
}
|
|