同时仿真时的时间并不准确,实际10s在仿真中只过了四秒左右
```c
#include <reg51.h>
#include <intrins.h>
sbit w1 = P3^2; // 6位数码管位选端
sbit w2 = P3^3;
sbit w3 = P3^4;
sbit w4 = P3^5;
sbit w5 = P3^6;
sbit w6 = P3^7;
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极字符0~9
void Delay1500us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 17;
j = 31;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void) //定时器初始化
{ //TMOD初始为0x00
TMOD &=0x0F; //按位与,让TMOD高四位清零,只用定时器0,并保存低四位不变化
TMOD |=0x01; //按位或,让TMOD低四位其它位不变,选择定时器0工作方式为方式1,即16位定时器
//为了不影响其他函数调用TMOD的其他功能,第二次赋值不会覆盖第一次TMOD的值
TH0 = (65536 - 50000)/256; //定时0.05s=50000us=50ms
TL0 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
static unsigned int j = 0;
unsigned int s1 = 0,s2 = 0,m1 = 0,m2 = 0,h1 = 0,h2 = 0; //00:00:00 秒数的个位十位 分数的个位十位 时数的个位十位
void display()
{
w1=0;w2=1;w3=1;w4=1;w5=1;w6=1; //选中六位数码管中的第一位
P2 = table[h2]; //显示‘0’
Delay1500us();
w1=1;w2=0;w3=1;w4=1;w5=1;w6=1;
P2 = table[h1]+0x80; //
Delay1500us();
w1=1;w2=1;w3=0;w4=1;w5=1;w6=1;
P2 = table[m2]; //显示‘0’
Delay1500us();
w1=1;w2=1;w3=1;w4=0;w5=1;w6=1;
P2 = table[m1]+0x80; //显示‘0.’,当要显示小数点时只需+0x80
Delay1500us();
w1=1;w2=1;w3=1;w4=1;w5=0;w6=1;
P2 = table[s2]; //显示‘0’
Delay1500us();
w1=1;w2=1;w3=1;w4=1;w5=1;w6=0;
P2 = table[s1]; //显示‘0’
Delay1500us();
}
void main()
{
Timer0Init();
while(1)
{
display();
}
}
void interruptTimer0() interrupt 1 //定时器0中断
{
ET0 = 0;
TR0 = 0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
j++;
if(j >= 20) //20*0.05ms=1s
{
s1++; //秒数个位+1
j = 0; //记满20次清零
if(s1 > 9) //秒数个位记满9次进位
{
s1 = 0;
s2++; //秒数十位+1
if(s2 > 5)
{
s2 = 0;
m1++;
if(m1 > 9)
{
m1 = 0;
m2++;
if(m2 > 5)
{
m2 = 0;
h1++;
if(h2 < 2)
{
if(h1 > 9)
{
h1 = 0;
h2++;
}
}
else
{
if(h1 > 3)
{
h1 = 0;
h2 = 0;
m1 = 0;
m2 = 0;
s1 = 0;
s2 = 0;
}
}
}
}
}
}
}
ET0 = 1;
TR0 = 1;
}
```