基於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;
}
}
}
}
}
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM