#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Key =P3^7;
uchar cnt,flashcnt;
sbit led1=P1^0;
sbit led2=P1^1;
bit bit_10ms=0,flag1,flag2;
uchar bdata flag_key_byte=0xff;
sbit keyflag=flag_key_byte^0;
sbit keyflag1=flag_key_byte^1;
/**********************************************************/
uchar keyscan(void)
{uchar kv=0;
keyflag1=keyflag;
keyflag=Key;
switch(flag_key_byte&0x03)
{
case 0:cnt++;break;
case 2:cnt++;break;
case 1:if(cnt>30)kv=2;
else if(cnt>5)kv=1;
else kv=0;
cnt=0;
flag_key_byte=0xff; break;
}
return kv;
}
/**********************************************************/
void main()
{
uchar kv=0;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
TMOD=0X01;
ET0=EA=TR0=1;
while(1)
{
if(bit_10ms)
{kv=keyscan();
bit_10ms=0;
if(kv==1) {flag1=!flag1;}
if(kv==2) {flag2=!flag2;}
if(!flag1) led1=1;
if(!flag2) led2=1;
flashcnt++;
if(flashcnt==10)
{
flashcnt=0;
if(flag1) led1=!led1;
if(flag2) led2=!led2;
}
}
}
}
/**********************************************************/
void DelayInt() interrupt 1
{TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
bit_10ms=1;
}
/**********************************************************/ |