/*名称:INT0及INT1中断计数
说明:每次按下第1个计数键时,
第1组计数值累加并显示在右边3只数
码管上,每次按下第2个计数键时,第
2组计数值累加并显示在左边3只数码管上,后两个按键分别清零。
*/
#include<reg51.h>
#defineucharunsignedchar
#defineuintunsignedint
sbitK3=P3^4;//2个清零键
sbitK4=P3^5;
//数码管段码与位码
ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
ucharcodeDSY_Scan_Bits[]={0x20,0x10,0x08,0x04,0x02,0x01};
//2组计数的显示缓冲,前3位一组,后3位一组
uchardataBuffer_Counts[]={0,0,0,0,0,0};
uintCount_A,Count_B=0;
//延时
voidDelayMS(uintx)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//数据显示
voidShow_Counts()
{
uchari;
Buffer_Counts[2]=Count_A/100;
Buffer_Counts[1]=Count_A%100/10;
Buffer_Counts[0]=Count_A%10;
{
if(Buffer_Counts[2]==0)
Buffer_Counts[2]=0x0a;
if(Buffer_Counts[1]==0)
}
Buffer_Counts[1]=0x0a;
Buffer_Counts[5]=Count_B/100;
Buffer_Counts[4]=Count_B%100/10;
Buffer_Counts[3]=Count_B%10;
if(Buffer_Counts[5]==0)
{
Buffer_Counts[5]=0x0a;
if(Buffer_Counts[4]==0)
Buffer_Counts[4]=0x0a;
}
for(i=0;i<6;i++)
{
P2=DSY_Scan_Bits[i];
P1=DSY_CODE[Buffer_Counts[i]];
DelayMS(1);
}
}
//主程序
voidmain()
{
IE=0x85;
PX0=1;中断优先
IT0=1;
IT1=1;
while(1)
{
if(K3==0)Count_A=0;
if(K4==0)Count_B=0;
Show_Counts();
}
}
//INT0中断函数
voidEX_INT0()interrupt0
{
Count_A++;
}
//INT1中断函数
voidEX_INT1()interrupt2
{
Count_B++;
}