51单片机万年历

可实现功能:

启动程序数码管按照××-××-××格式显示时分秒并走时

按下C1按钮后按照××-××-××格式显示年月日

按下C3调时模式按钮时分秒/年月日静止,指示灯亮起,进入调时模式

先按C2选位按钮再按下C0加一按钮,对应的位置改变

再次按下C3按钮退出调时模式,继续走时

 

#include<reg51.h>
#define uChar unsigned char
#define uInt unsigned int

uChar a []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 
uChar b []={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; 
uChar second=00,minute=00,hour=10,year=17,month=01,day=11,count;

sbit Key1 = P3^7; //计时停止
sbit Key2 = P3^6; //调位
sbit Key3 = P3^0; //加一
sbit Key4 = P3^2; //切换
sbit LED1 = P1^7;


void Delayms(uInt t)
{
uInt i,j;
for(i=0;i<t;i++)
for(j=0;j<125;j++)
;
}

 

void Dispaly1(uChar second,uChar minute,uChar hour)
{

P2=b[0];
P0=a[hour/10];
Delayms(5);


P2=b[1];
P0=a[hour%10];
Delayms(5);

P2=b[2];
P0=0x40;
Delayms(5);

P2=b[3];
P0=a[minute/10];
Delayms(5);


P2=b[4];
P0=a[minute%10];
Delayms(5);

P2=b[5];
P0=0x40;
Delayms(5);

P2=b[6];
P0=a[second/10];
Delayms(5);


P2=b[7];
P0=a[second%10];
Delayms(5);


}

 


void Dispaly2(uChar day,uChar month,uChar year)
{

P2=b[0];
P0=a[day/10];
Delayms(5);


P2=b[1];
P0=a[day%10];
Delayms(5);

P2=b[2];
P0=0x40;
Delayms(5);

P2=b[3];
P0=a[month/10];
Delayms(5);


P2=b[4];
P0=a[month%10];
Delayms(5);

P2=b[5];
P0=0x40;
Delayms(5);

P2=b[6];
P0=a[year/10];
Delayms(5);


P2=b[7];
P0=a[year%10];
Delayms(5);


}

void Keyscan1()
{
static uChar i=0,j=0;
if(Key1==0)
{
Delayms(10); //消抖
if(Key1==0)
while(!Key1); //等待按键弹起
i++;
}
if(i%2==1)
{
LED1=0;
TR0=0;
}
if(i%2==0)
{
LED1=1;
TR0=1;
}
if(Key2==0)
{
Delayms(10);
if(Key2==0)
while(!Key2);
j++;
}
if(j%4==1)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
second++;
if(second==60)
second=0;
}
}
if(j%4==2)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
minute++;
if(minute==60)
minute=0;
}
}
if(j%4==3)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
hour++;
if(hour==24)
hour=0;
}
}




}

void Keyscan2()
{
static uChar m=0,n=0;
if(Key1==0)
{
Delayms(10);
if(Key1==0)
while(!Key3);
m++;
}
if(m%2==1)
{
LED1=0;
TR0=0;
}
if(m%2==0)
{
LED1=1;
TR0=1;
}
if(Key2==0)
{
Delayms(10);
if(Key2==0)
while(!Key2);
n++;
}
if(n%4==1)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
day++;
if(day==30)
day=0;
}
}
if(n%4==2)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
month++;
if(month==12)
month=0;
}
}
if(n%4==3)
{
if(Key3==0)
{
Delayms(10);
if(Key3==0)
while(!Key3);
year++;
if(year==99)
year=0;
}
}




}

void main()
{

TMOD=0x01; 
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
static uChar h=0;
if(Key4==0)
{
Delayms(10);
if(Key4==0)
while(!Key4);
h++;
}
if(h%2==1)
{
Dispaly2(year,month,day);
Keyscan2();
}
if(h%2==0)
{
Dispaly1(second,minute,hour);
Keyscan1();
}

}
}

void time0_int(void) interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
count++;
if(count==100)
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(day==30)
{
day=0;
month++;
if(month==12)
{
month=0;
year++;
if(year==99)
{
year=0;
}
}
}
}
}
}
}
}

猜你喜欢

转载自blog.csdn.net/yilongdashi/article/details/80576564