51单片机(AT89C52)一键多功能(短按、长按)

短按(按下持续时间小于3s)一次数码管显示加1,同时流水灯计数加1。

长按(按下持续时间大于3s)时,数码管显示每500ms加1,同时流水灯每500ms计数加1。

#include
typedef unsigned char uchar;
typedef unsigned int uint;
#define NOKEY 0
#define KEYDOWN 1
#define KEYUP 2
uchar keystate;
sbit key=P2^7;
void keyscan(void);
uchar timeflag;
uchar code table[]=
{
	0x80,0xf2,0x48,0x60,
	0x32,0x24,0x04,0xf0,
	0x00,0x20,0x10,0x06,
	0x8c,0x42,0x0c,0x1c
};
uchar i;
void main(void)
{
	TMOD=0x10;
	TH1=(65536-50000)/256;
	TL1=(65536-50000)%256;
	EA=1;
	ET1=1;
	TR1=1;
	while(1)
	{
		if(timeflag)
		{
			timeflag=0;
			keyscan();
		}
	}
}
void keyscan(void)
{
	static uchar iscontinue=0;
	P2 |=0x80;
	switch(keystate)
	{
		case NOKEY:
			if(key==0)
			{
				keystate=KEYDOWN;
				return;
			}
			else
			{
				keystate=NOKEY;			
				return;
			}
		case KEYDOWN:
			if(key==0)
			{
				keystate=KEYUP;
				return;
			}
			else
			{
				keystate=NOKEY;			
				return;
			}			
		case KEYUP:
			if(key==0)
			{
				keystate=KEYUP;
				iscontinue++;
				if(iscontinue==255)
					iscontinue=60;
				if(iscontinue>=60)
				{
					if(iscontinue%10==0)
					{
						P2+=1;
						P1=table[i++];
						if(i>=16)
							i=0;
					}
				}
				return;
			}
			else
			{
				keystate=NOKEY;
				if(iscontinue<60)
				{
					P2+=1;
					P1=table[i++];
					if(i>=16)
						i=0;					
				}
				iscontinue=0;			
				return;
			}
	}
}
void timer1isr() interrupt 3
{
	TH1=(65536-50000)/256;
	TL1=(65536-50000)%256;
	timeflag=1;
}


 

 


 

你可能感兴趣的:(51单片机,C语言,c,table,timer)