程序如下:
#include <REG52.H>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar led_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar led_bit[]={0x01,0x02,0x04,0x08,0x10,0x20};
uint over_count=0,cap_count=0;
ulong sig_t=0;
void time2isr(void)interrupt 5 using 2
{
if(EXF2==1)
{
EXF2=0;
cap_count++;
if(cap_count==1)
{
TH2=0;
TL2=0;
RCAP2H=0;
RCAP2L=0;
over_count=0;
return;
}
sig_t=RCAP2H*256+RCAP2L+over_count*65536+30;
TL2=0;
RCAP2H=0;
RCAP2L=0;
over_count=0;
cap_count=0;
}
else
{
over_count++;
TF2=0;
}
}
void display(ulong tempdata)
{
uchar led_data[6];
uchar i;
uint k;
for(i=0;i<6;i++)
{
led_data[5-i]=tempdata%10;
tempdata=tempdata/10;
}
for(i=0;i<6;i++)
{
P2=0;
P0=led_code[led_data[i]];
P2=led_bit[i];
for(k=0;k<1000;k++);
}
}
void main(void)
{
TH2=0;
TL2=0;
RCAP2H=0x00;
RCAP2L=0x00;
T2CON=0x0D;
EA=1;
ET2=1;
while(1)
{
display(sig_t);
}
}