|
#include <stc15f2k60s2.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit dianji = P3^4;
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 pwn_value = 0;
unsigned char mode = 1; //1 睡眠風(fēng) 2 自然風(fēng) 3 常風(fēng) 4 溫度
unsigned int time = 0;
unsigned char dat_Led = 0;
bit flag1s = 0;
bit mode_T = 0; //溫度顯示位
void InitSystem();
void SMGWorking();
void Ledrunning();
void ScanKeys();
void Timer0Init(void); //1毫秒@12.000MHz
void Timer1Init(void); //10微秒@12.000MHz
extern bit Start18B20();
extern unsigned char Get18B20temp();
void main()
{
Timer0Init();
Timer1Init();
InitSystem();
while(1)
{
Ledrunning();
SMGWorking();
ScanKeys();
}
}
/******HC573選擇函數(shù)*******/
void Select74HC573(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()
{
Select74HC573(5);
P0 = 0x00;
Select74HC573(4);
P0 = 0xFF;
Select74HC573(0);
}
/******LED顯示函數(shù)*******/
void Ledrunning()
{
Select74HC573(4);
if(dat_Led<3)
{
P0 = ~(0x01<<dat_Led);
}
else
{
P0 = 0xFF; //風(fēng)扇停止轉(zhuǎn)動(dòng)
}
Select74HC573(0);
P0 = 0xFF;
}
/*******數(shù)碼管顯示函數(shù)*******/
void SMGWorking()
{
unsigned char temp;
if(flag1s)
{
flag1s = 0;
Start18B20();
temp = Get18B20temp();
}
if(mode_T==0)
{
LedBuff[0] = LedChar[16];
LedBuff[1] = LedChar[mode];
LedBuff[2] = LedChar[16];
LedBuff[3] = 0xFF;
LedBuff[4] = LedChar[time/1000];
LedBuff[5] = LedChar[time/100%10];
LedBuff[6] = LedChar[time/10%10];
LedBuff[7] = LedChar[time%10];
}
else
{
LedBuff[0] = LedChar[16];
LedBuff[1] = LedChar[4];
LedBuff[2] = LedChar[16];
LedBuff[3] = 0xFF;
LedBuff[4] = 0xFF;
LedBuff[5] = LedChar[temp/10];
LedBuff[6] = LedChar[temp%10];
LedBuff[7] = LedChar[12];
}
}
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
/*******按鍵相關(guān)函數(shù)*******/
void ScanKeys()
{
/******室溫按鍵******/
if(S7==0)
{
Delay1ms();
if(S7==0)
{
mode_T = ~mode_T;
while(S7==0)
{
SMGWorking();
}
}
}
/*******停止按鍵,清零工作時(shí)間*******/
if(S6==0)
{
Delay1ms();
if(S6==0)
{
time = 0;
dat_Led = 4; //關(guān)閉Led
while(S6==0)
{
SMGWorking();
}
}
}
/*******定時(shí)按鍵******/
if(S5==0)
{
Delay1ms();
if(S5==0)
{
time += 60;
while(S5==0)
{
SMGWorking();
}
}
}
/******工作模式切換*******/
if(S4==0)
{
Delay1ms();
if(S4==0)
{
switch(mode)
{
case 1:mode=2;dat_Led=1;pwn_value=30;break;
case 2:mode=3;dat_Led=2;pwn_value=70;break;
case 3:mode=1;dat_Led=0;pwn_value=20;break;
}
while(S4==0)
{
SMGWorking();
}
}
}
}
/******數(shù)碼管刷新*****/
void ScanSMG()
{
static unsigned char i = 0;
P0 = 0xFF;
Select74HC573(6);
P0 = 0x01<<i;
Select74HC573(7);
P0 = LedBuff[i++];
if(i>=8)
i = 0;
Select74HC573(0);
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定時(shí)器時(shí)鐘1T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TL0 = 0x20; //設(shè)置定時(shí)初值
TH0 = 0xD1; //設(shè)置定時(shí)初值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開始計(jì)時(shí)
ET0 = 1;
EA = 1;
}
/*******定時(shí)器0相關(guān)函數(shù)*******/
void InterruprTimer0() interrupt 1
{
static unsigned int cnt = 0;
ScanSMG();
cnt++;
if(cnt>=1000)
{
flag1s = 1;
cnt = 1;
if(time>0)
time--;
else
time = 0;
}
}
/*******定時(shí)器1相關(guān)函數(shù)*******/
void Timer1Init(void) //10微秒@12.000MHz
{
AUXR &= 0xBF; //定時(shí)器時(shí)鐘12T模式
TMOD &= 0x0F; //設(shè)置定時(shí)器模式
TL1 = 0xF6; //設(shè)置定時(shí)初值
TH1 = 0xFF; //設(shè)置定時(shí)初值
TF1 = 0; //清除TF1標(biāo)志
TR1 = 1; //定時(shí)器1開始計(jì)時(shí)
ET1 = 1;
EA = 1;
}
void InterruptTimer1() interrupt 3
{
static unsigned char pwn = 0;
pwn++;
if(pwn<=pwn_value)
{
dianji = 1;
}
else if(pwn>=100)
{
dianji = 1;
pwn = 0;
}
else
{
dianji = 0;
}
}
|
|