单片机C语言程序设计:INT0 及 INT1 中断计数
扫描二维码
随时随地手机看文章
/* 名称: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中断控制LED