單片機C語言程序設(shè)計:INT0 及 INT1 中斷計數(shù)
/* 名稱:INT0 及 INT1 中斷計數(shù)
說明:每次按下第 1 個計數(shù)鍵時,
第 1 組計數(shù)值累加并顯示在右邊 3 只數(shù)
碼管上, 每次按下第 2 個計數(shù)鍵時,第
2 組計數(shù)值累加并顯示在左邊 3 只數(shù)碼管上,后兩個按鍵分別清零。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit K3=P3^4; //2 個清零鍵
sbit K4=P3^5;
//數(shù)碼管段碼與位碼
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 組計數(shù)的顯示緩沖,前 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++);
}
//數(shù)據(jù)顯示
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;中斷優(yōu)先
IT0=1;
IT1=1;
while(1)
{
if(K3==0) Count_A=0;
if(K4==0) Count_B=0;
Show_Counts();
}
}
//INT0 中斷函數(shù)
void EX_INT0() interrupt 0
{
Count_A++;
}
//INT1 中斷函數(shù)
void EX_INT1() interrupt 2
{
Count_B++;
}
擴展閱讀:外部INT0中斷控制LED
編輯:admin 最后修改時間:2018-05-19