?? clock2.c
字號:
#include "reg51.h"
char disp[6]={0,0,0,0,2,1};
char hdisp[6];
char clock[3]={50,59,23},nclock[3];
char K=0;
char i,m=5,time=0,naotime=0,scancode;
void Delay(int n);
void display();
void timego();
void Delay(int n) /*延時程序*/
{int j;
for(j=n;j>0;j--);
}
void display()/*顯示程序*/
{
char select[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
char dis_code[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char i;
for(i=0;i<=5;i++)
{
P2=select[i];
P0=dis_code[disp[i]];
Delay(200);
}
}
void initial_timer()/*定時0鞒跏薊*/
{TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=0xe0;
TL0=0xef;
}
timer0() interrupt 1 using 1 /*定時0中斷*/
{
TH0=0xe0;
TL0=0xef;
K++;
while(K==100)
{K=0;
clock[0]=clock[0]+1;
if(clock[0]==60){clock[1]=clock[1]+1;clock[0]=0;}
if(clock[1]==60){clock[2]=clock[2]+1;clock[1]=0;}
if(clock[2]==24)clock[2]=0;
hdisp[0]=clock[0]%10;
hdisp[1]=clock[0]/10;
hdisp[2]=clock[1]%10;
hdisp[3]=clock[1]/10;
hdisp[4]=clock[2]%10;
hdisp[5]=clock[2]/10;
if((time+naotime)==0)
{
for(i=0;i<6;i++)
{
disp[i]=hdisp[i];
}
}
}
}
char judgekey() /*判斷是否有鍵按下,有返回1,沒有返回0*/
{int scan;
P1=0xf0;
scan=P1;
if((scan&0xf0)!=0xf0)
return 1;
else
return 0;
}
char valuekey() /*求鍵號*/
{char keycode;
if(judgekey())
{
Delay(200);
}
if(judgekey())
{ keycode=0xfe;
while((keycode&0x10)!=0)
{
P1=keycode;
scancode=P1;
if((scancode&0xf0)!=0xf0)
break;
else
keycode=(keycode<<1)|0x01;
}
scancode=~scancode;
return scancode;
}
}
void newtime()//修改時間存到clock數組
{
if(time!=0)
{ clock[0]=disp[1]*10+disp[0];
clock[1]=disp[3]*10+disp[2];
clock[2]=disp[5]*10+disp[4];
}
}
void newnaotime() //輸入鬧鈴時間存儲到鬧鈴單元
{
if((naotime!=0)&&(m==1))
{
nclock[0]=disp[1]*10+disp[0];
nclock[1]=disp[3]*10+disp[2];
nclock[2]=disp[5]*10+disp[4];
}
}
void checknaotime( )//檢查時間是否為鬧鈴時間
{if((clock[1]==nclock[1])&&(clock[2]==nclock[2]))
{P3=0xef;}
}
void ntime() //鬧鈴程序
{newnaotime();
checknaotime();
}
void manage_key0() //鍵盤處理子程序
{if((time+naotime)!=0)
disp[m--]=0;
if(m==0){m=5;}
}
void manage_key1()
{if((time+naotime)!=0)
disp[m--]=1;
if(m==0){m=5;}
}
void manage_key2()
{if((time+naotime)!=0)
disp[m--]=2;
if(m==0){m=5;}
}
void manage_key3()
{if((time+naotime)!=0)
disp[m--]=3;
if(m==0){m=5;}
}
void manage_key4()
{if((time+naotime)!=0)
disp[m--]=4;
if(m==0){m=5;}
}
void manage_key5()
{if((time+naotime)!=0)
disp[m--]=5;
if(m==0){m=5;}
}
void manage_key6()
{if((time+naotime)!=0)
disp[m--]=6;
if(m==0){m=5;}
}
void manage_key7()
{if((time+naotime)!=0)
disp[m--]=7;
if(m==0){m=5;}
}
void manage_key8()
{if((time+naotime)!=0)
disp[m--]=8;
if(m==0){m=5;}
}
void manage_key9()
{if((time+naotime)!=0)
disp[m--]=9;
if(m==0){m=5;}
}
void manage_keytime()
{ ET0=0;
time++;
if(time==2){time=0;ET0=1;m=5;}
}
void manage_keynaotime()
{naotime++;
if(naotime==2){naotime=0;m=5;}
}
void keymanage(char s) //鍵盤處理程序
{
switch(s)
{
case 0x82: manage_key0();break;
case 0x41: manage_key1();break;
case 0x42: manage_key2();break;
case 0x44: manage_key3();break;
case 0x21: manage_key4();break;
case 0x22: manage_key5();break;
case 0x24: manage_key6();break;
case 0x11: manage_key7();break;
case 0x12: manage_key8();break;
case 0x14: manage_key9();break;
case 0x81:manage_keytime();break;
case 0x84: manage_keynaotime();break;
}
}
void keyscane()//鍵盤程序
{char ss;
ss=valuekey();
while(judgekey());
keymanage(ss);
newtime();
}
void main()
{
timego();
while(1)
{
keyscane();
ntime();
display();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -