发布网友 发布时间:2024-10-24 02:23
共2个回答
热心网友 时间:2024-11-18 22:51
#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=55,min=59,hour=12,scanled;
unsigned char key,mode,time;
unsigned char disdat[8];
sbit key1=P1^4;
sbit key2=P1^5;
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]=min%10;
disdat[3]=min/10;
disdat[4]=hour%10;
disdat[5]=hour/10;
}
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>12)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:
P3=0x04;
P2=~ledtab[disdat[5]];
break;
case 1:
P3=0x08;
P2=~ledtab[disdat[4]]&0x7f;
break;
case 2:
P3=0x10;
P2=~ledtab[disdat[3]];
break;
case 3:
P3=0x20;
P2=~ledtab[disdat[2]]&0x7f;
break;
case 4:
P3=0x40;
P2=~ledtab[disdat[1]];
break;
case 5:
P3=0x80;
P2=~ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=6;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
TR1=1;
TR0=0;
ET0=1;
ET1=1;
EA=1;
sec=0;
min=0;
hour=0;
scanled=0;
time=0;
mode=0;
dischg();
while(1)
{
if(key1==0)
{
while(key1==0);
TR0=1;
}
if(key2==0)
{
while(key2==0);
TR0=0;
}
}
}
热心网友 时间:2024-11-18 22:55
看书