如何在这段程序中添加外部中断
时间:10-02
整理:3721RD
点击:
抢答时利用外部中断响应
void main()
{
uchar ret;
WaitTime=EEPROM_Read(0x2000);
if((WaitTime==0)||(WaitTime>99))
{
WaitTime=30;
}
AnswerTime=EEPROM_Read(0x2001);
if((AnswerTime==0)||(AnswerTime>99))
{
AnswerTime=15;
}
TimerInit();
Buff[0]=Array[0]; //数码管刷新
Buff[1]=Array[0];
Buff[2]=Array[0];
Buff[3]=Array[0];
while(1) // 设置时间
{
SetTime();
ret=KeyScanf();
if(ret!=0)
{
Buff[0]=Array[ret];
Buff[1]=Array[16];
Buff[2]=Array[12];
Buff[3]=Array[12];
bp=0;
while(1);
}
Init_INT0();
if(kStart==0)
{
break;
}
}
DelayMs(10);
while(!kStart);
DelayMs(10);
Buff[0]=Array[17];
Buff[1]=Array[17];
Buff[2]=Array[WaitTime/10];
Buff[3]=Array[WaitTime%10];
TR1 = 1;
while(1)
{
if(Count>=20)
{
WaitTime--;
Buff[2]=Array[WaitTime/10];
Buff[3]=Array[WaitTime%10];
Count=0;
if(WaitTime<6)
{
bp=0;
DelayMs(100);
bp=1;
}
}
if(WaitTime==0)
{
TR1=0;
bp=0;
Buff[0]=Array[16];
Buff[1]=Array[16];
Buff[2]=Array[16];
Buff[3]=Array[16];
while(1);
}
ret=KeyScanf();
if(ret!=0)
{
break;
}
}
Buff[0]=Array[ret];
Buff[1]=Array[16];
Buff[2]=Array[AnswerTime/10];
Buff[3]=Array[AnswerTime%10];
Count=0;
while(1)
{
if(Count>=20)
{
AnswerTime--;
Buff[2]=Array[AnswerTime/10];
Buff[3]=Array[AnswerTime%10];
Count=0;
if(AnswerTime<6)
{
bp=0;
DelayMs(100);
bp=1;
}
}
if(AnswerTime==0)
{
TR1=0;
bp=0;
Buff[0]=Array[16];
Buff[1]=Array[16];
Buff[2]=Array[16];
Buff[3]=Array[16];
while(1);
}
}
}
void Timer0(void) interrupt 1
{
TH0 = 248;
TL0 = 48;
P0=0x00;
w1=1;
w2=1;
w3=1;
w4=1;
if(ShowID==1)
{
w1=0;
P0=Buff[0];
}
if(ShowID==2)
{
w2=0;
P0=Buff[1];
}
if(ShowID==3)
{
w3=0;
P0=Buff[2];
}
if(ShowID==4)
{
w4=0;
P0=Buff[3];
}
ShowID++;
if(ShowID==5)
ShowID=1;
}
void Timer1(void) interrupt 3
{
TH1 = 60;
TL1 = 176;
Count++;
}
void INT0_ISR(void) interrupt 0
{
}
void main()
{
uchar ret;
WaitTime=EEPROM_Read(0x2000);
if((WaitTime==0)||(WaitTime>99))
{
WaitTime=30;
}
AnswerTime=EEPROM_Read(0x2001);
if((AnswerTime==0)||(AnswerTime>99))
{
AnswerTime=15;
}
TimerInit();
Buff[0]=Array[0]; //数码管刷新
Buff[1]=Array[0];
Buff[2]=Array[0];
Buff[3]=Array[0];
while(1) // 设置时间
{
SetTime();
ret=KeyScanf();
if(ret!=0)
{
Buff[0]=Array[ret];
Buff[1]=Array[16];
Buff[2]=Array[12];
Buff[3]=Array[12];
bp=0;
while(1);
}
Init_INT0();
if(kStart==0)
{
break;
}
}
DelayMs(10);
while(!kStart);
DelayMs(10);
Buff[0]=Array[17];
Buff[1]=Array[17];
Buff[2]=Array[WaitTime/10];
Buff[3]=Array[WaitTime%10];
TR1 = 1;
while(1)
{
if(Count>=20)
{
WaitTime--;
Buff[2]=Array[WaitTime/10];
Buff[3]=Array[WaitTime%10];
Count=0;
if(WaitTime<6)
{
bp=0;
DelayMs(100);
bp=1;
}
}
if(WaitTime==0)
{
TR1=0;
bp=0;
Buff[0]=Array[16];
Buff[1]=Array[16];
Buff[2]=Array[16];
Buff[3]=Array[16];
while(1);
}
ret=KeyScanf();
if(ret!=0)
{
break;
}
}
Buff[0]=Array[ret];
Buff[1]=Array[16];
Buff[2]=Array[AnswerTime/10];
Buff[3]=Array[AnswerTime%10];
Count=0;
while(1)
{
if(Count>=20)
{
AnswerTime--;
Buff[2]=Array[AnswerTime/10];
Buff[3]=Array[AnswerTime%10];
Count=0;
if(AnswerTime<6)
{
bp=0;
DelayMs(100);
bp=1;
}
}
if(AnswerTime==0)
{
TR1=0;
bp=0;
Buff[0]=Array[16];
Buff[1]=Array[16];
Buff[2]=Array[16];
Buff[3]=Array[16];
while(1);
}
}
}
void Timer0(void) interrupt 1
{
TH0 = 248;
TL0 = 48;
P0=0x00;
w1=1;
w2=1;
w3=1;
w4=1;
if(ShowID==1)
{
w1=0;
P0=Buff[0];
}
if(ShowID==2)
{
w2=0;
P0=Buff[1];
}
if(ShowID==3)
{
w3=0;
P0=Buff[2];
}
if(ShowID==4)
{
w4=0;
P0=Buff[3];
}
ShowID++;
if(ShowID==5)
ShowID=1;
}
void Timer1(void) interrupt 3
{
TH1 = 60;
TL1 = 176;
Count++;
}
void INT0_ISR(void) interrupt 0
{
}
很久没玩过51了,51的外部中断源有两个:INT0、INT1。如果是需要4个中断源的话,只能另寻它法。如果就加入外部中断到这段代码来说,可以使用51支持的两级中断服务嵌套形式加入中断。或者用软件的方法交替开关中断都可以。
另外你的定时器0的if扫描语句可以使用switch,这样执行效率会高一点。就这些吧,希望能对你有所帮助。