?? 學(xué)習(xí)板時鐘程序.c
字號:
#include <reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
uchar count=1;
uchar key;
uchar kc;
uchar sec=0;
uchar min=0;
uchar hour=0;
uchar day=1;
uchar mon=1;
uint year=2007;
uchar mod=0;
uchar check=0;
bit shine=1;
uchar code numtab[34]={0xde,0x06,0xd5,0x57,0x0f,0x5b,0xdb,0x16,0xdf,0x5f,0x9f,0xcb,0xd8,0xc7,0xd9,0x99,0xfe,0x26,0xf5,0x77,0x2f,0x7b,0xfb,0x36,0xff,0x7f,0xbf,0xeb,0xf8,0xe7,0xf9,0xb9,0x01,0x00};
uchar x[8];
////////////////////////////////////////////////////////////////////////////////////////////
void delay(uint t) /*延時子程序*/
{while(t--);
}
/////////////////////////////////////////////////////////////////////////////////////////////
void numdisplay(void) /*數(shù)碼管顯示子程序*/
{uchar i;
XBYTE[0x9fff];
if(check==1)
{if(shine==1) {P1=numtab[x[0]];}
else {P1=numtab[33];}}
else P1=numtab[x[0]];
XBYTE[0xdfff];
XBYTE[0xffff];
delay(400);
for(i=1;i<8;i++)
{if(i+1==check)
{if(shine==1) {P1=numtab[x[i]];}
else {P1=numtab[33];}}
else P1=numtab[x[i]];
XBYTE[0xbfff];
XBYTE[0xffff];
delay(400);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
void time(void) /*時間子程序*/
{ sec++;
if(sec>=60)
{min++;
sec=0;}
if(min>=60)
{hour++;
min=0;}
if(hour>=24)
{hour=0;
day++;
if(mon==2||mon==4||mon==6||mon==9||mon==11)
{if(mon==2) {if(!(year%4)&&(year%100)||!(year%400)) {if(day>=30) {day=1;mon=3;}}
else if(day>=29) {day=1;mon=3;}}
else if(day>=31){day=1;mon++;}}
else if(day>=32) {day=1;mon++;}}
if(mon>=13) {mon=1;year++;}
}
/**********************************************************************************************************/
void T0_delay (void) interrupt 1 using 1 //中斷亮燈程序//
{
TH0=47;
TL0=224;
if(count==10) {shine=0;}
if(count==20) {shine=1;time();count=0;}
count++;
}
//////////////////////////////////////////////////////////////////////////////////////////////
void keyin(void) //鍵盤掃描
{unsigned char i,j,w;
delay(500);
if(~XBYTE[0xf800]&0xf)
{w=XBYTE[0xf8fe]&0x0f;
if(~w&0x0f)
{i=4;
j=w;
w=0xff;
}
w=XBYTE[0xf8fd]&0x0f;
if(~w&0x0f)
{i=3;
j=w;
w=0xff;
}
w=XBYTE[0xf8fb]&0x0f;
if(~w&0x0f)
{i=2;
j=w;
w=0xff;
}
w=XBYTE[0xf8f7]&0x0f;
if(~w&0x0f)
{i=1;
j=w;
w=0xff;
}
w=XBYTE[0xf8ef]&0x0f;
if(~w&0x0f)
{i=0;
j=w;
w=0xff;
}
w=~j;
key=(w<<4)|i;
}
while(~XBYTE[0xf800]&0xf);
switch(key)
{case 0x80:kc=20;break;
case 0x40:kc=15;break;
case 0x20:kc=0;break;
case 0x10:kc=5;break;
case 0x81:kc=19;break;
case 0x41:kc=14;break;
case 0x21:kc=9;break;
case 0x11:kc=4;break;
case 0x82:kc=18;break;
case 0x42:kc=13;break;
case 0x22:kc=8;break;
case 0x12:kc=3;break;
case 0x83:kc=17;break;
case 0x43:kc=12;break;
case 0x23:kc=7;break;
case 0x13:kc=2;break;
case 0x84:kc=16;break;
case 0x44:kc=11;break;
case 0x24:kc=6;break;
case 0x14:kc=1;break;}
if(mod)
{switch(check)
{case 0:break;
case 1:if(kc<10) year=2000+kc*10; check=2; break;
case 2:if(kc<10) year=year+kc; check=4;break;
case 4:if(kc<2) mon=kc*10; check=5;break;
case 5:if(kc<10) mon=mon+kc; check=7;break;
case 7:if(kc<4) day=kc*10; check=8;break;
case 8:if(kc<10) day=day+kc; check=0;break;}}
else
{switch(check)
{case 0:break;
case 1:if(kc<3) hour=kc*10; check=2; break;
case 2:if(kc<10) hour=hour+kc; check=4;break;
case 4:if(kc<6) min=kc*10; check=5;break;
case 5:if(kc<10) min=min+kc; check=7;break;
case 7:if(kc<6) sec=kc*10; check=8;break;
case 8:if(kc<10) sec=sec+kc; check=0;break;}}
switch(kc)
{case 11:if(++mod>1) mod=0;break;
case 12:if(++check>8) check=0; if(check==3) check=4; if(check==6) check=7; break;
}
EX1=1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
void keybreak(void) interrupt 2 using 0 //鍵盤中斷
{
EX1=0;
key=0;
keyin();}
//////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
TMOD=0x01;
TH0=47;
TL0=224;
ET0=1;
EA=1;
EX1=1;
TR0=1;
XBYTE[0xf800];
x[2]=32;
x[5]=32;
while(1)
{
switch(mod)
{case 0:x[7]=sec%10; x[6]=sec/10;x[4]=min%10; x[3]=min/10; x[1]=hour%10; x[0]=hour/10;break;
case 1:x[7]=day%10; x[6]=day/10;x[4]=mon%10; x[3]=mon/10;x[1]=year%10; x[0]=(year/10)%10;break;}
numdisplay();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -