这有一个只用外部中断来计算外部脉冲数的程序,用数码管显示。经实验没有任何问题。
#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) //延时函数
{
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口输出数字1的段码
wei1=0; //锁存
delay(1); //延时10
wei1=1;
delay(1); //延时10
P0=0xff; //消隐
if(qian==0&&bai==0)
wei2=1;
else
P0=table[bai];
wei2=0; //锁存
delay(1); //延时10
wei2=1;
delay(1); //延时10
P0=0xff; //消隐
if(qian==0&&bai==0&&shi==0)
wei3=1;
else
P0=table[shi];
wei3=0; //锁存
delay(1); //延时10
wei3=1;
delay(1); //延时10
P0=0xff; //消隐
P0=table[ge];
wei4=0; //锁存
delay(1); //延时10
wei4=1;
delay(1); //延时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();
}
}
|