#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar tt,bai,shi,ge;
uint count=300;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
sbit key2=P3^5;
void delay(uchar z);
void init();
void key();
void display(uint count);
void main()
{
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xf8;
wela=0;
init();
while(1)
{
key();
display(count);
}
}
void delay(uchar z)
{
int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
void timer_0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==20)
{
tt=0;
count--;
}
}
void key()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
TR0=1;
}
while(!key1);
}
}
void display(uint count)
{
delay(10);
bai=count/100;
shi=(count-bai*100)/10;
ge=count%10;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(2);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(3);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(3);
}
按键按下,数码管只显示最后一个个位,好无奈啊 ,试了好长时间! |