|
#include <stc15f2k60s2.h>
#include <I2C.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
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 dat_rd1;
unsigned char money;
unsigned int shui_L = 0; //出水量
unsigned int flag_ns = 0;
bit shui = 0; // 0停止出水 1打開出水
void InitSystem();
void Timer0Init(void); //1毫秒@12.000MHz
void ConfigSMG();
void ConfigWorking();
void ScanKeys();
void ScanSMG();
extern unsigned char ReadRd1();
void main()
{
InitSystem();
Timer0Init();
while(1)
{
ConfigWorking();
ConfigSMG();
ScanKeys();
ScanSMG();
}
}
/******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;
case 0:P2 = (P2&0x1F)|0x00;break;
}
}
/*******關(guān)閉LED和蜂鳴器、繼電器******/
void InitSystem()
{
SeceltHC573(5);
P0 = 0x00;
SeceltHC573(4);
P0 = 0xFF;
SeceltHC573(0);
}
/******工作模式配置******/
void ConfigWorking()
{
dat_rd1 = ReadRd1(); //采集光敏電阻電壓
SeceltHC573(4);
if(dat_rd1>125) //判斷LED1亮滅
{
P0 = 0xFE;
}
else
{
P0 = 0xFF;
}
SeceltHC573(0);
shui_L = flag_ns;
money = shui_L*0.5;
if(shui_L>=999)
{
shui = 0;
flag_ns = 0;
}
if(shui)
{
EA = 0;
SeceltHC573(5); //打開繼電器
P0 = 0x10;
SeceltHC573(0);
EA = 1;
}
else
{
EA = 0;
SeceltHC573(5); //關(guān)閉繼電器
P0 = 0x00;
SeceltHC573(0);
EA = 1;
}
}
/******數(shù)碼管動(dòng)態(tài)顯示*******/
void ConfigSMG()
{
if(shui)
{
LedBuff[0] = 0xFF;
LedBuff[1] = (LedChar[0]&LedChar[17]);
LedBuff[2] = LedChar[5];
LedBuff[3] = LedChar[0];
LedBuff[4] = LedChar[shui_L/100];
LedBuff[5] = (LedChar[shui_L/10%10]&LedChar[17]);
LedBuff[6] = LedChar[shui_L%10];
LedBuff[7] = LedChar[0];
}
else
{
LedBuff[0] = 0xFF;
LedBuff[1] = (LedChar[0]&LedChar[17]);
LedBuff[2] = LedChar[5];
LedBuff[3] = LedChar[0];
LedBuff[4] = LedChar[money/100];
LedBuff[5] = (LedChar[money/10%10]&LedChar[17]);
LedBuff[6] = LedChar[money%10];
LedBuff[7] = LedChar[0];
}
}
/******按鍵相關(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)
{
Delay1ms();
if(S7==0)
{
flag_ns = 0;
shui = 1;
while(S7==0)
{
ScanSMG();
}
}
}
if(S6==0)
{
Delay1ms();
if(S6==0)
{
shui = 0;
while(S6==0)
{
ScanSMG();
}
}
}
}
/******數(shù)碼管動(dòng)態(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);
P0 = 0xFF;
Delay1ms();
}
/******配置定時(shí)器0******/
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定時(shí)器時(shí)鐘12T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TL0 = 0x18; //設(shè)置定時(shí)初值
TH0 = 0xFC; //設(shè)置定時(shí)初值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開始計(jì)時(shí)
ET0 = 1;
EA = 1;
}
void InterruptTimer0() interrupt 1
{
static unsigned int cnt = 0;
if(shui)
{
cnt++;
if(cnt>=1000)
{
cnt = 0;
flag_ns++;
}
}
else
{
cnt = 0;
}
}
|
|