#include
bit int_flag; //定时器0 1S到标志位
unsigned char volatile int_count; //定时器0中断次数
unsigned char volatile T1count; //定时器1中断次数
unsigned char code dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 显示段码值0123456789
unsigned long sum; //1S内脉冲总个数
unsigned char led[6]; //LED显示缓存
///////////////软件延时/////////////
void delay(unsigned int cnt)
{
while(--cnt);
}
///定时器0初始化
void init_t0(void)
{
TMOD=(TMOD&0xF0)||0x01; //定时器0工作于方式1
TH0=(65536-50000)/256; //定时50ms
TL0=(65535-50000)%256;
}
// 定时器1初始化
void init_t1(void)
{
TMOD=(TMOD&0x0F)|0x50; //timer 1 for count
TH1 =0x00;
TL1 =0x00;
}
//显示
void disp(void)
{
unsigned char i;
for(i=0;i<6;i++)
{
P0=dofly[(led[i])];//取显示数据
P2=5-i; //取段码
delay(100); //扫描间隙延时,根据单片机调整,延时1ms即可
}
}
///////////////////////////////////////////////////////
void main (void)
{
EA=1; //开总中断
init_t0(); //初始化定时器
init_t1();
TR0=1; //定时器开始工作
TR1=1;
ET0=1; //开T0中断
while(1)
{
if(int_flag==1)
{
int_flag=0;
sum=TL1+TH1*256+T1count*65536; //计算1秒内的脉冲个数
//以下将数据格式化,转成LED可显示的BCD码
led[0]=sum%10; //最低位
sum=sum/10;
led[1]=sum%10; //第二位
sum=sum/10;
led[2]=sum%10;
sum=sum/10;
led[3]=sum%10;
sum=sum/10;
led[4]=sum%10;
led[5]=sum/10;
int_count=0x00;
T1count=0;
TH1=0x00;
TL1=0x00;
TR1=1;
}
disp();
}
}
//定时器0中断服务程序
void int_t0(void) interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
int_count++;
if(int_count==20)
{
TR1=0;
int_flag=1;
int_count=0x00;
}
}
//定时器1中断服务程序
void int_t1(void) interrupt 3
{
T1count++;
}