运用两个定时器 来完成 3位数码管 一起显现计时器, 即从0- 999规模的计时
#include
//共阴极数码管编码规模
unsigned char code table[10]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0xef };
unsigned char first=0;
unsigned char second=0;
unsigned char third=0;
unsigned int number=0;
unsigned char t=0;
void Delay()
{
unsigned char j;
unsigned char i;
for(i=10; i>0; i–)
for(j=110; j>0; j–);
}
void main()
{
TMOD=0x11; //设置两个定时器T0,T1的工作方法: 运用16位寄存器的方法1
EA=1; //翻开 大局中止
ET0=1; // 翻开定时器T0的中止
ET1=1;
TR0=1; // 发动定时器T0
TR1=1;
TH0=(65536-50000)%256;
TL0=(65536-50000)%256;
TH1=(65536-50000)%256;
TL1=(65536-50000)%256;
while(1)
{
if(t==20)
{
t=0;
number++;
first=number/100;
second=(number-first*100)/10;
third=(number-first*100)%10;
}
}
}
void TimerLED0() interrupt 1 using 1
{
TH0=(65536-50000)%256;
TL0=(65536-50000)%256;
t++;
}
void TimerLED1() interrupt 3 using 2
{
TH1=(65536-10000)%256;
TL1=(65536-10000)%256;
P2=0xfe;
P0=table[first];
Delay();
P2=0xfd;
P0=table[second];
Delay();
P2=0xfb;
P0=table[third];
Delay();
}