第一种情况:主函数中用定时初始化T0_init()来实现初始化,程序不能实现显示0-60
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit P15=P1^5;
sbit P20=P2^0;
sbit P21=P2^1;
uint Count=0,Second=0;
uchar code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
uchar dis[]={1,2};
void T0_init()
{
TMOD=0x01;
TH0=(65536-50000);
TL0=(65536-50000);
IE=0x82;
TR0=1;
}
void delay(uint x)
{ uint i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void Time0( ) interrupt 1
{
TH0=(65536-50000);
TL0=(65536-50000);
if(++Count==20)
{Count=0;
Second++;
if(Second==60)
Second=0;
}
//dis[0]=Second/10;
//dis[1]=Second%10;
}
/* 显示函数*/
void display()
{
uchar i,temp=0x01;
dis[1]=Second/10; //取计数值的整数位
dis[0]=Second%10; //取计数值的余数位
for(i=0;i<2;i++)
{
P1=TAB[dis[i]];
P2=temp;
delay(5);
P1=0xff;
P2=0xff;
temp<<=1;
}
}
main()
{
void T0_init();
while(1)
{
display();
}
}
第二种情况:初始化直接放在主函数中程序可以执行,显示0-60#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit P15=P1^5;
sbit P20=P2^0;
sbit P21=P2^1;
uint Count=0,Second=0;
uchar code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
uchar dis[]={1,2};
void delay(uint x)
{ uint i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void Time0( ) interrupt 1
{
TH0=(65536-50000);
TL0=(65536-50000);
if(++Count==20)
{Count=0;
Second++;
if(Second==60)
Second=0;
}
}
/* 显示函数*/
void display()
{
uchar i,temp=0x01;
dis[0]=Second/10; //取计数值的整数位
dis[1]=Second%10; //取计数值的余数位
for(i=0;i<2;i++)