#include <STC15F2K60S2.H>
#include"intrins.h"
sbit key=P3^5;
sbit LED=P3^4;
unsigned char L,value,time,num;
bit S,flag,d;
void delay_ms(unsigned char z)
{
unsigned int x,y;
for(x=z; x>0; x--)
for(y=848; y>0; y--);
}
void HY()
{ if(!S)
{
switch(L)
{
case 0: LED=1;break;
case 1: LED=0;delay_ms(2);LED=1;delay_ms(8);break;
case 2: LED=0;delay_ms(4);LED=1;delay_ms(6);break;
case 3: LED=0;delay_ms(6);LED=1;delay_ms(4);break;
case 4: LED=0;break;
}
}
if(S)
{
switch(L)
{
case 0: LED=1;break;
case 1: LED=0;delay_ms(45);LED=1;delay_ms(255);break;
case 2: LED=0;delay_ms(80);LED=1;delay_ms(220);break;
case 3: LED=0;delay_ms(150);LED=1;delay_ms(150);break;
case 4: LED=0;delay_ms(10);LED=1;delay_ms(90);break;
}
}
}
void Timer0Init(void) //5毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0xEE; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA = 1;
ET0 = 1;
}
unsigned char getkey()
{
static unsigned char lp=0;
switch(lp)
{
case 0:
if(!key)lp++;
else if(flag)
{
if(++time>50)
{
value=1; //单击
time=0,
flag=0;
}
}
break;
case 1:if(!key)lp++;else lp=0;break; // 消抖动
case 2:
if(key)
{
lp++;
if(flag)
{
value=2; //连击
flag=0;
}
else flag=1;
}
else if(++num>250)
{
flag=0;
value=3; //长按
lp++;
}
break;
case 3:if(key){lp=0;num=0;time=0;}break;
}
return value;
}
main()
{
LED=1;
L=0;S=0;flag=0;value=0,d=0;
Timer0Init();
while(1)
{
if(d==0&&key==1)
{
LED=1;
ET0 = 0;
INT_CLKO |= 0x20; //INT3 Enable
PCON = 0x02; //MCU进入掉电模式
_nop_();
_nop_();
_nop_();
_nop_();
}
HY();
}
}
void timer()interrupt 1
{
switch(getkey())
{
case 1:
L++;
value=0;
if(L>4) L=1;
break;
case 2: S=~S;L=1;value=0;break;
case 3: L=0;value=0;d=0; break;
}
}
void INT3_int (void) interrupt 11
{
INT_CLKO &= 0xDF;
delay_ms(30);
if(key==0)
{d=1;
L=1;
ET0 = 1;
}
}
|