#include "extern.h"
//8.192ms
#define C_DEBOUNCE_CNT 4 // 32 ms
#define C_STOP_CNT 25 // 200 ms
.ramadr 0
word Point;
.ramadr system
byte MsgInt;
byte KeyTmp,KeyBuf,KeyDebounce,DebounceCnt;
byte StopCnt;
byte LightFlag;
void Open_LightR(void)
{
LightFlag=1;
PA&=0b_1110_1110;
PA|=0b_0001_0000;
}
void Open_LightG(void)
{
LightFlag=2;
PA&=0b_1110_1110;
PA|=0b_0000_0001;
}
void Stop_Light(void)
{
LightFlag=0;
PA&=0b_1110_1110;
}
void Key_LightR(void)
{
if (LightFlag==1)
Stop_Light();
else
Open_LightR();
}
void Key_LightG(void)
{
if (LightFlag==2)
Stop_Light();
else
Open_LightG();
}
void Key_Scan(void)
{
KeyTmp=KeyDebounce;
KeyDebounce=0;
if (PA.6==0)
KeyDebounce.0=1;
if (PA.7==0)
KeyDebounce.1=1;
if (KeyTmp!=KeyDebounce)
{
DebounceCnt=C_DEBOUNCE_CNT;
StopCnt=C_STOP_CNT;
}
else
{
if (DebounceCnt)
{
DebounceCnt--;
if(DebounceCnt==0)
{
if (KeyBuf!=KeyDebounce)
{
KeyTmp=KeyBuf^KeyDebounce;
KeyBuf=KeyDebounce;
if (KeyTmp.0)
{
if (KeyBuf.0)
Key_LightR();
}
if (KeyTmp.1)
{
if (KeyBuf.1)
Key_LightG();
}
}
}
}
}
}
void Init_Port(void)
{
$ PADIER = 0b_1111_1001; //(1=en in)
PA = 0b_0000_0000; //(0=lo,1=hi)
PAC = 0b_0011_1111; //(0=in,1=out)
PAPH = 0b_1100_0000; //(0=floating,1=pull hi)
PAPL = 0b_0000_0000; //(0=floating,1=pull hi)
}
void Init_System(void)
{
.DISGINT;
INTRQ=0; //默认值不定,一定要设置哦
.WDRESET;
.CLKMD.En_WatchDog=1;
$ MISC Fast_Wake_Up, WDT_64K;
$ T16M IHRC, /16, BIT12; // 8192us Interrupt
INTEN.T16=1;
ENGINT;
StopCnt=C_STOP_CNT;
}
void Chk_Stop(void)
{
if (StopCnt==0&&LightFlag==0)
{
.disgint;
intrq = 0;
.clkmd.En_WatchDog = 0;
$ CLKMD ILRC/1, En_IHRC, En_ILRC;
$ CLKMD ILRC/1, En_ILRC;
nop;
nop;
stopsys;
nop;
nop;
$ CLKMD IHRC/8, En_IHRC, En_ILRC;
nop;
nop;
Init_System();
}
}
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/8, IHRC=16MHZ, VDD=3.0V;
.DISGINT;
.WDRESET;
.CLKMD.En_WatchDog=0;
Init_Port();
Point = _SYS(RAM_SIZE)-1; //得到 RAM 的最高字节的地址
A=0;
do //RAM值小于256
{
*Point=A;
}
while(--Point$0);
Init_Port();
Init_System();
while (1)
{
.WDRESET;
if (MsgInt)
{
MsgInt--;
if (StopCnt)
StopCnt--;
Key_Scan();
Chk_Stop();
}
}
}
void Interrupt (void)
{
pushaf;
if (INTRQ.T16)
{
INTRQ.T16=0;
MsgInt++;
}
popaf;
}