如何實現(xiàn)數(shù)碼管3位計數(shù)器
#include
//共陰極數(shù)碼管編碼范圍
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; //設(shè)置兩個定時器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();
}
評論