同一个按键短按与长按的区别触发
#include "REG52.H"
#define const_voice_short 20 //蜂鸣器短叫的持续时间
#define const_voice_long 140 //蜂鸣器长叫的持续时间
#define const_key_time1_short1 20 //短按的按键去抖动延时时间
#define const_key_time_long1 400 //长按的按键去抖动延时时间
#define const_key_time1_short2 20
#define const_key_time_long2 400
#define const_voice_short 20 //蜂鸣器短叫的持续时间
#define const_voice_long 140 //蜂鸣器长叫的持续时间
#define const_key_time1_short1 20 //短按的按键去抖动延时时间
#define const_key_time_long1 400 //长按的按键去抖动延时时间
#define const_key_time1_short2 20
#define const_key_time_long2 400
void initial_myself();
void initial_peripheral();
void delay_long(unsigned int uiDelayLong);
void T0_time();
void key_service();
void key_scan();
void initial_peripheral();
void delay_long(unsigned int uiDelayLong);
void T0_time();
void key_service();
void key_scan();
sbit key_sr1=P0^0;
sbit key_sr2=P0^1;
sbit key_gnd_dr=P0^4;
sbit beep_dr=P1^5;
sbit key_sr2=P0^1;
sbit key_gnd_dr=P0^4;
sbit beep_dr=P1^5;
unsigned char ucKeySec=0;
unsigned int uiKeyTimeCnt1=0; //按键去抖动延时计数器
unsigned char ucKeyLock1=0; //按键触发后自锁的变量标志
unsigned char ucShortTouchFlag1=0; //短按的触发标志
unsigned int uiKeyTimeCnt2=0;
unsigned char ucKeyLock2=0;
unsigned char ucShortTouchFlag2=0;
unsigned int uiVoiceCnt=0; //蜂鸣器鸣叫的持续时间计数器
unsigned int uiKeyTimeCnt1=0; //按键去抖动延时计数器
unsigned char ucKeyLock1=0; //按键触发后自锁的变量标志
unsigned char ucShortTouchFlag1=0; //短按的触发标志
unsigned int uiKeyTimeCnt2=0;
unsigned char ucKeyLock2=0;
unsigned char ucShortTouchFlag2=0;
unsigned int uiVoiceCnt=0; //蜂鸣器鸣叫的持续时间计数器
void main()
{
initial_myself();
delay_long(100);
initial_peripheral();
while(1)
{
key_service();
}
}
{
initial_myself();
delay_long(100);
initial_peripheral();
while(1)
{
key_service();
}
}
void key_scan() //按键扫描函数,放在定时中断里
{
if(key_sr1==1)
{
ucKeyLock1=0;
uiKeyTimeCnt1=0;
if(ucShortTouchFlag1==1) //短按触发标志
{
ucShortTouchFlag1=0;
ucKeySec=1; //触发一号键的短按
}
}
else if(ucKeyLock1==0) //有按键按下,且第一次被按下
{
uiKeyTimeCnt1++;
if(uiKeyTimeCnt1>const_key_time1_short1)
ucShortTouchFlag1=1; //激活按键短按的有效标志
if(uiKeyTimeCnt1>const_key_time_long1)
{
ucShortTouchFlag1=0; //清除按键短按的有效标志
uiKeyTimeCnt1=0;
ucKeyLock1=1; //自锁按键置位,避免一直触发
ucKeySec=2; //触发1号键的长按
}
}
if(key_sr2==1)
{
ucKeyLock2=0;
uiKeyTimeCnt2=0;
if(ucShortTouchFlag2==1)
{
ucShortTouchFlag2=0;
ucKeySec=3; //触发2号按键的短按
}
}
else if(ucKeyLock2==0)
{
uiKeyTimeCnt2++; //累加定时中断次数
if(uiKeyTimeCnt2>const_key_time1_short2)
ucShortTouchFlag2=1;
if(uiKeyTimeCnt2>const_key_time_long2)
{
ucShortTouchFlag2=0;
uiKeyTimeCnt2=0;
ucKeyLock2=1;
ucKeySec=4;
}
}
}
{
if(key_sr1==1)
{
ucKeyLock1=0;
uiKeyTimeCnt1=0;
if(ucShortTouchFlag1==1) //短按触发标志
{
ucShortTouchFlag1=0;
ucKeySec=1; //触发一号键的短按
}
}
else if(ucKeyLock1==0) //有按键按下,且第一次被按下
{
uiKeyTimeCnt1++;
if(uiKeyTimeCnt1>const_key_time1_short1)
ucShortTouchFlag1=1; //激活按键短按的有效标志
if(uiKeyTimeCnt1>const_key_time_long1)
{
ucShortTouchFlag1=0; //清除按键短按的有效标志
uiKeyTimeCnt1=0;
ucKeyLock1=1; //自锁按键置位,避免一直触发
ucKeySec=2; //触发1号键的长按
}
}
if(key_sr2==1)
{
ucKeyLock2=0;
uiKeyTimeCnt2=0;
if(ucShortTouchFlag2==1)
{
ucShortTouchFlag2=0;
ucKeySec=3; //触发2号按键的短按
}
}
else if(ucKeyLock2==0)
{
uiKeyTimeCnt2++; //累加定时中断次数
if(uiKeyTimeCnt2>const_key_time1_short2)
ucShortTouchFlag2=1;
if(uiKeyTimeCnt2>const_key_time_long2)
{
ucShortTouchFlag2=0;
uiKeyTimeCnt2=0;
ucKeyLock2=1;
ucKeySec=4;
}
}
}
void key_service()
{
switch(ucKeySec)
{
case 1:
uiVoiceCnt=const_voice_short;
ucKeySec=0;
break;
case 2:
uiVoiceCnt=const_voice_long;
ucKeySec=0;
break;
case 3:
uiVoiceCnt=const_voice_short;
ucKeySec=0;
break;
case 4:
uiVoiceCnt=const_voice_long;
ucKeySec=0;
break;
}
}
{
switch(ucKeySec)
{
case 1:
uiVoiceCnt=const_voice_short;
ucKeySec=0;
break;
case 2:
uiVoiceCnt=const_voice_long;
ucKeySec=0;
break;
case 3:
uiVoiceCnt=const_voice_short;
ucKeySec=0;
break;
case 4:
uiVoiceCnt=const_voice_long;
ucKeySec=0;
break;
}
}
void T0_time() interrupt 1
{
TF0=0;
TR0=0;
key_scan();
if(uiVoiceCnt!=0)
{
uiVoiceCnt--;
beep_dr=0;
}
else
{
;
beep_dr=1;
}
TH0=0xf8;
TL0=0x2f;
TR0=1;
}
{
TF0=0;
TR0=0;
key_scan();
if(uiVoiceCnt!=0)
{
uiVoiceCnt--;
beep_dr=0;
}
else
{
;
beep_dr=1;
}
TH0=0xf8;
TL0=0x2f;
TR0=1;
}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i=0;i<uiDelayLong;i++)
for(j=0;j<500;j++)
;
}
{
unsigned int i;
unsigned int j;
for(i=0;i<uiDelayLong;i++)
for(j=0;j<500;j++)
;
}
void initial_myself()
{
key_gnd_dr=0;
beep_dr=1;
TMOD=0x01;
TH0=0xf8;
TL0=0x2f;
}
{
key_gnd_dr=0;
beep_dr=1;
TMOD=0x01;
TH0=0xf8;
TL0=0x2f;
}
void initial_peripheral()
{
EA=1;
ET0=1;
TR0=1;
}
{
EA=1;
ET0=1;
TR0=1;
}