在定时器中断函数里执行独立按键的扫描程序
/*
上一例在主函数中利用累计定时器中断的次数来实现独立按键的检测,但是
如果在某些项目中,需要主函数里面间歇性地执行一些一气呵成的耗时任务,
当主函数正在执行一气呵成的耗时任务时(前提没有关闭定时器中断),这个时候
如果有按键按下来,就有可能没有被及时响应而遗漏了。
解决办法:在定时器里面处理独立按键的扫描程序,可以避免上述问题。
*/
#include "REG52.H"
#define const_voice_short 40
#define const_voice_long 200
#define const_key_time1 20
#define const_key_time2 20
void initial_myself();
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;
unsigned char ucKeySec=0;
unsigned int uiKeyTimeCnt1=0;
unsigned char ucKeyLock1=0;
上一例在主函数中利用累计定时器中断的次数来实现独立按键的检测,但是
如果在某些项目中,需要主函数里面间歇性地执行一些一气呵成的耗时任务,
当主函数正在执行一气呵成的耗时任务时(前提没有关闭定时器中断),这个时候
如果有按键按下来,就有可能没有被及时响应而遗漏了。
解决办法:在定时器里面处理独立按键的扫描程序,可以避免上述问题。
*/
#include "REG52.H"
#define const_voice_short 40
#define const_voice_long 200
#define const_key_time1 20
#define const_key_time2 20
void initial_myself();
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;
unsigned char ucKeySec=0;
unsigned int uiKeyTimeCnt1=0;
unsigned char ucKeyLock1=0;
unsigned int uiKeyTimeCnt2=0;
unsigned char ucKeyLock2=0;
unsigned int uiVoiceCnt=0;
unsigned char ucKeyLock2=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;
}
else if(ucKeyLock1==0)
{
uiKeyTimeCnt1++;
if(uiKeyTimeCnt1>const_key_time1)
{
uiKeyTimeCnt1=0;
ucKeyLock1=1;
ucKeySec=1;
}
}
if(key_sr2==1)
{
ucKeyLock2=0;
uiKeyTimeCnt2=0;
}
else if(ucKeyLock2==0)
{
uiKeyTimeCnt2++;
if(uiKeyTimeCnt2>const_key_time2)
{
uiKeyTimeCnt2=0;
ucKeyLock2=1;
ucKeySec=2;
}
}
}
{
if(key_sr1==1)
{
ucKeyLock1=0;
uiKeyTimeCnt1=0;
}
else if(ucKeyLock1==0)
{
uiKeyTimeCnt1++;
if(uiKeyTimeCnt1>const_key_time1)
{
uiKeyTimeCnt1=0;
ucKeyLock1=1;
ucKeySec=1;
}
}
if(key_sr2==1)
{
ucKeyLock2=0;
uiKeyTimeCnt2=0;
}
else if(ucKeyLock2==0)
{
uiKeyTimeCnt2++;
if(uiKeyTimeCnt2>const_key_time2)
{
uiKeyTimeCnt2=0;
ucKeyLock2=1;
ucKeySec=2;
}
}
}
void key_service()
{
switch(ucKeySec)
{
case 1:
uiVoiceCnt=const_voice_short;
ucKeySec=0;
break;
case 2:
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;
}
}
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;
}