/* 名称:INT0及INT1中断计数
说明:每次按下第1个计数键时,第1组计数值累加并显示在右边3只数码管上, 每次按下第2个计数键时,第2组计数值累加并显示在左边3只数码管上,后两个按键分别清零。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit K3=P3^4; //2个清零键
sbit K4=P3^5;
//数码管段码与位码
uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar code DSY_Scan_Bits[]={0x20,0x10,0x08,0x04,0x02,0x01};
//2组计数的显示缓冲,前3位一组,后3位一组
uchar data Buffer_Counts[]={0,0,0,0,0,0};
uint Count_A,Count_B=0;
//延时
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++);
}
//数据显示
void Show_Counts()
{
uchar i;
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);
}
}
//主程序
void main()
{
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中断函数
void EX_INT0() interrupt 0
{
Count_A++;
}
//INT1中断函数
void EX_INT1() interrupt 2
{
Count_B++;
}
INT0及INT1中断计数
最新推荐文章于 2023-06-30 12:52:45 发布