|
這有一個(gè)只用外部中斷來計(jì)算外部脈沖數(shù)的程序,用數(shù)碼管顯示。經(jīng)實(shí)驗(yàn)沒有任何問題。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit wei1 = P1^0;
sbit wei2 = P1^1;
sbit wei3 = P1^2;
sbit wei4 = P1^3;
uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90};
void initEx0();
uint count=0;
uchar ge,shi,bai,qian;
void delay (unsigned int z) //延時(shí)函數(shù)
{
uint x,y;
for (x=z;x>0;x--);
for(y=10;y>0;y--);
}
void xianshi()
{
P0=0xff; //消隱
wei1=1;
wei2=1;
wei3=1;
wei4=1;
if(qian==0)
wei1=1;
else
P0=table[qian]; //p0口輸出數(shù)字1的段碼
wei1=0; //鎖存
delay(1); //延時(shí)10
wei1=1;
delay(1); //延時(shí)10
P0=0xff; //消隱
if(qian==0&&bai==0)
wei2=1;
else
P0=table[bai];
wei2=0; //鎖存
delay(1); //延時(shí)10
wei2=1;
delay(1); //延時(shí)10
P0=0xff; //消隱
if(qian==0&&bai==0&&shi==0)
wei3=1;
else
P0=table[shi];
wei3=0; //鎖存
delay(1); //延時(shí)10
wei3=1;
delay(1); //延時(shí)10
P0=0xff; //消隱
P0=table[ge];
wei4=0; //鎖存
delay(1); //延時(shí)10
wei4=1;
delay(1); //延時(shí)10
P0=0xff; //消隱
}
void initEx0()
{
EA=1;
EX0=1;
IT0=1;
}
void ex0_intr()interrupt 0
{
count=count+1;
if(count>9999)
count=0;
}
void main()
{
initEx0();
while(1)
{
qian=count/1000;
bai=count/100-count/1000*10;
shi=count/10%10;
ge=count%10;
xianshi();
}
}
|
|