void keyscan()
{本文引用地址:http://m.butianyuan.cn/article/201611/321542.htm
if(hour_key==0) //時(shí)按鍵
{
delay(10);
if(hour_key==0)
{
while(!hour_key)display(hour1,minu1,sec1); //消除數(shù)碼管的抖動(dòng)
if(mode_number%2==0)//如果在時(shí)鐘模式下,時(shí)鐘時(shí)+1
hour1++;
else hour0++;//如果在鬧鐘模式下,鬧鐘時(shí)+1
if(hour1==12)
{
hour1=0;
}
if(hour0==12)
{
hour0=0;
}
}
}
if(minu_key==0) //分按鍵
{
delay(10);
if(minu_key==0)
{
while(!minu_key)display(hour1,minu1,sec1); //消除數(shù)碼管的抖動(dòng)
if(mode_number%2==0)
minu1++;
else
minu0++;
if(minu1==60)
{
minu1=0;
}
if(minu0==60)
{
minu0=0;
}
}
}
if(mode_key==0) //模式按鍵
{
delay(10);
if(mode_key==0)
{
while(!mode_key)display(hour1,minu1,sec1); //數(shù)碼管的抖動(dòng)
mode_number++;
}
}
if(stopsounder_key==0) //關(guān)鬧鐘按鍵
{
sounder_flag=1;
delay(10);
if(stopsounder_key==0)
{
while(!stopsounder_key)display(hour1,minu1,sec1);
beep=1;
}
}
}
void display(uchar hour,uchar minu,uchar sec)
{
h1=hour/10;
h2=hour;
m1=minu/10;
m2=minu;
s1=sec/10;
s2=sec;
P2=0xff;
P1=table[h1];
P2=select[0];
delay(5);
P2=0xff;
P1=table[h2];
P2=select[1];
delay(5);
P2=0xff;
P1=0x40;;
P2=select[2];
delay(5);
P2=0xff;
P1=table[m1];
P2=select[3];
delay(5);
P2=0xff;
P1=table[m2];
P2=select[4];
delay(5);
P2=0xff;
P1=0x40;
P2=select[5];
delay(5);
P2=0xff;
P1=table[s1];
P2=select[6];
delay(5);
P2=0xff;
P1=table[s2];
P2=select[7];
delay(5);
}
void timer1_int() interrupt 3 //控制時(shí)鐘工作 50ms
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
b++;
if(b==10)led=~led;
if(b==20)
{
led=~led;
b=0;
sec1++;
if(sec1==60)
{
sounder_flag=0; //每隔一分鐘,釋放檢測(cè)鬧鐘
sec1=0;
minu1++;
if(minu1==60)
{
minu1=0;
hour1++;
if(hour1==24)
hour1=0;
}
}
}
}
void main()
{
init();
for(;;)
{
keyscan(); //按鍵掃描
if(mode_number%2==0) //正常時(shí)鐘顯示
{
display(hour1,minu1,sec1);
}
else
{
display(hour0,minu0,sec0); //設(shè)定時(shí)間顯示
delay(100);
}
if(hour0==hour1&&minu0==minu1) //如果設(shè)定的時(shí)間到
{
if(!sounder_flag) //且按鍵按下一分鐘之后
{
beep=0;//鬧鈴開
}
}
}
}
評(píng)論