|
#include <stc15f2k60s2.h>
#include <I2C.h>
#include <DS1302.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned char code LedChar[] = {
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xBF,0x7F
};
unsigned char LedBuff[8] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char time[7]; //獲取時鐘芯片時間
unsigned char value_fz = 50; //濕度閾值
unsigned char value_shidu; //濕度
bit mode_auto = 1; // 0 手動模式 1 自動模式
bit mode_buzz = 1; // 0 蜂鳴器關(guān)閉 1 蜂鳴器打開
bit mode_sys = 0; // 0 關(guān)閉灌溉系統(tǒng) 1 打開灌溉系統(tǒng)
bit set_value = 0; // 0 關(guān)閉濕度閾值設(shè)置 1 打開濕度閾值設(shè)置
bit flag500ms = 0;
void InitSystem();
void Timer0Init(void); //1毫秒@12.000MHz
void ScanKeys();
void ConfigSMG();
void SysWorking();
void Delay1ms(); //@12.000MHz
extern void I2CWrite(unsigned char addr,unsigned char dat);
extern unsigned char I2CRead(unsigned char addr);
extern unsigned char ReadRb2();
void main()
{
Timer0Init();
InitSystem();
InitDS1302(); //1302初始化
value_fz=I2CRead(0x03); //讀取濕度閾值
while(1)
{
ConfigSMG();
ScanKeys();
SysWorking();
}
}
/******HC573選擇函數(shù)*******/
void SeceltHC573(unsigned char channel)
{
switch(channel)
{
case 4:P2 = (P2&0x1F)|0x80;break;
case 5:P2 = (P2&0x1F)|0xA0;break;
case 6:P2 = (P2&0x1F)|0xC0;break;
case 7:P2 = (P2&0x1F)|0xE0;break;
default:P2 = (P2&0x1F)|0x00;break;
}
}
/*******關(guān)閉LED和蜂鳴器、繼電器******/
void InitSystem()
{
SeceltHC573(5);
P0 = 0x00;
SeceltHC573(4);
P0 = 0xFF;
SeceltHC573(0);
}
/******數(shù)碼管顯示********/
void ConfigSMG()
{
if(flag500ms)
{
value_shidu = ReadRb2(); //讀取濕度
}
if((mode_auto==1)&&(set_value==1)) //閾值設(shè)置界面
{
LedBuff[0] = LedChar[16];
LedBuff[1] = LedChar[16];
LedBuff[2] = 0xFF;
LedBuff[3] = 0xFF;
LedBuff[4] = 0xFF;
LedBuff[5] = 0xFF;
LedBuff[6] = LedChar[value_fz/10];
LedBuff[7] = LedChar[value_fz%10];
}
else //時間及濕度顯示界面
{
DS1302SingRead(time);
LedBuff[0] = LedChar[time[2]>>4];
LedBuff[1] = LedChar[time[2]&0x0F];
LedBuff[2] = LedChar[16];
LedBuff[3] = LedChar[time[1]>>4];
LedBuff[4] = LedChar[time[1]&0x0F];
LedBuff[5] = 0xFF;
LedBuff[6] = LedChar[value_shidu/10];
LedBuff[7] = LedChar[value_shidu%10];
}
}
/******按鍵相關(guān)函數(shù)*******/
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void ScanKeys()
{
if(S7==0) //系統(tǒng)工作狀態(tài)切換按鍵
{
Delay1ms();
if(S7==0)
{
mode_auto = ~mode_auto;
while(S7==0)
{
ConfigSMG();
}
}
}
if(S6==0)
{
Delay1ms();
if(S6==0)
{
if(mode_auto) //自動模式
{
set_value = ~set_value; //進入和退出濕度閾值調(diào)整界面
}
else //手動模式
{
mode_buzz = ~mode_buzz; //蜂鳴器打開和關(guān)閉
}
while(S6==0)
{
ConfigSMG();
}
}
}
if(S5==0)
{
Delay1ms();
if(S5==0)
{
if(mode_auto) //自動模式
{
if(set_value)
{
if(value_fz<99) //濕度閾值加1
{
value_fz++;
}
else
value_fz = 99;
}
}
else //手動模式
{
mode_sys = 1; //打開灌溉系統(tǒng)
}
while(S5==0)
{
ConfigSMG();
}
}
}
if(S4==0)
{
Delay1ms();
if(S4==0)
{
if(mode_auto) //自動模式
{
if(set_value)
{
if(value_fz>0) //濕度閾值減1
{
value_fz--;
}
else
value_fz = 0;
}
}
else //手動模式
{
mode_sys = 0; //關(guān)閉灌溉系統(tǒng)
}
while(S4==0)
{
ConfigSMG();
}
}
}
}
/*****系統(tǒng)工作狀態(tài)顯示******/
void SysWorking()
{
if(mode_auto) //自動模式
{
SeceltHC573(4); //點亮LED1
P0 = 0xFE;
SeceltHC573(0);
if(value_shidu<value_fz) //灌溉設(shè)備自動打開
{
EA = 0;
SeceltHC573(5);
P0 = 0x10;
SeceltHC573(0);
EA = 1;
}
else //灌溉設(shè)備自動關(guān)閉
{
EA = 0;
SeceltHC573(5);
P0 = 0x00;
SeceltHC573(0);
EA = 1;
}
}
else //手動模式
{
SeceltHC573(4); //點亮LED2
P0 = 0xFD;
SeceltHC573(0);
if(mode_sys) //灌溉設(shè)備打開
{
EA = 0;
SeceltHC573(5);
P0 = 0x10;
SeceltHC573(0);
EA = 1;
}
else //灌溉設(shè)備關(guān)閉
{
EA = 0;
SeceltHC573(5);
P0 = 0x00;
SeceltHC573(0);
EA = 1;
}
if(mode_buzz) //開啟蜂鳴器標志位
{
if(value_shidu<value_fz) //濕度小于閾值蜂鳴器打開
{
SeceltHC573(5);
P0 = 0x40;
SeceltHC573(0);
}
}
}
if(set_value) //保存濕度閾值
{
I2CWrite(0x03,value_fz);
Delay1ms();
}
}
/******數(shù)碼管動態(tài)刷新*****/
void ScanSMG()
{
static unsigned char i = 0;
P0 = 0xFF;
SeceltHC573(6);
P0 = 0x01<<i;
SeceltHC573(7);
P0 = LedBuff[i++];
if(i>=8)
i = 0;
SeceltHC573(0);
}
/******配置定時器0******/
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定時器時鐘12T模式
TMOD &= 0xF0; //設(shè)置定時器模式
TL0 = 0x18; //設(shè)置定時初值
TH0 = 0xFC; //設(shè)置定時初值
TF0 = 0; //清除TF0標志
TR0 = 1; //定時器0開始計時
ET0 = 1;
EA = 1;
}
void InterruptTimer0() interrupt 1
{
static unsigned int cnt = 0;
ScanSMG();
cnt++;
if(cnt>=500)
{
cnt = 0;
flag500ms = ~flag500ms;
}
}
|
|