用c语言循环语句怎么写一个51单片机的时钟啊,用8个数码管显示时间。

用c语言循环语句怎么写一个51单片机的时钟啊,用8个数码管显示时间。谢谢啦 我写了一天都没写出来

#include<reg51.h>

#define uchar unsigned char
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9
unsigned char sec=0,min=0,hour=12,scanled;
unsigned char key,flashbit,mode,time;
unsigned char disdat[8];
sbit led=P1^0;
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void dischg()
{
disdat[0]=sec%10;
disdat[1]=sec/10;
disdat[2]=10;

disdat[3]=min%10;
disdat[4]=min/10;
disdat[5]=10;

disdat[6]=hour%10;
disdat[7]=hour/10; 
}
void flash()
{
switch(flashbit)
{
case 0:break;
case 1:
disdat[6]=0x10;
disdat[7]=0x10;
delay(50);
dischg();
delay(80);
break;
case 2:
disdat[3]=0x10;
disdat[4]=0x10;
delay(50);
dischg();
delay(80);
break;
case 3:
disdat[0]=0x10;
disdat[1]=0x10;
delay(50);
dischg();
delay(80);
break;
default:break;
}
}
void t0isr() interrupt 1 //秒计时
{
TH0=0x3c;
TL0=0xb0;
time++;
switch(mode)
{
case 0:
if(time==20)
{
time=0;
sec++;
if(sec>59)
{
sec=0;
min++;
if(min>59)
{
min=0;
hour++;
if(hour>23)hour=0;
}
}
}
break;
case 1:
if(time==20)
{
time=0;
if(sec>0)sec--;
else if(min>0){sec=59;min--;}
else if(hour>0){sec=59;min=59;hour--;}
else {hour=0;min=0;sec=0;}
}
break;
}
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
switch(scanled)
{
case 0:
P2=0x01;
P0=~ledtab[disdat[7]];
break;
case 1:
P2=0x02;
P0=~ledtab[disdat[6]];
break;
case 2:
P2=0x04;
P0=~ledtab[disdat[5]];
break;
case 3:
P2=0x08;
P0=~ledtab[disdat[4]];
break;
case 4:
P2=0x10;
P0=~ledtab[disdat[3]];
break;
case 5:
P2=0x20;
P0=~ledtab[disdat[2]];
break;
case 6:
P2=0x40;
P0=~ledtab[disdat[1]];
break;
case 7:
P2=0x80;
P0=~ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=8;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1;
sec=55;
min=59;
hour=23;
flashbit=0;
scanled=0;
time=0;
mode=0;
dischg();
while(1)
{
flash();//闪烁
if((P3&0x0f)!=0x0f){
key=P3&0x0f;
while((P3&0x0f)!=0x0f);
led=0;
delay(10);
key|=0xf0;
switch(~key)
{
case 0x01:  //p3.1选择调时、分、秒
TR0=0;
flashbit+=1;
if(flashbit>3){flashbit=0;TR0=1;}
break;
case 0x02:  //p3.2调数
if(flashbit==0)break;
if(flashbit==1)
{
hour++;
if(hour>99)hour=0;
}
if(flashbit==2)
{
min++;
if(min>59)min=0;
}
if(flashbit==3)
{
sec++;
if(sec>59)sec=0;
}
break;
case 0x04:   //选择正/倒
TR0=0;
mode++;
mode&=0x01;
if(mode==0){sec=0;min=0;hour=0;}
dischg();
break;
case 0x08: //启动/暂停
TR0=~TR0;
break;
default:break;
}
}
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答