voidload_LED(unsignedcharH,unsignedcharM)
{led[0]=H/10;led[1]=H%10;led[2]=M/10;led[3]=M%10;}
unsignedcharH1_set(unsignedcharH1){H1=(H1/10+1)*10+H1%10;if(H1>23){H1=H1%10;};returnH1;}
unsignedcharH0_set(unsignedcharH0)
{if(H0%10==3&&H0/10==2)H0=H0/10*10;elseH0=H0%10+1+H0/10*10;returnH0;}
unsignedcharM1_set(unsignedcharM1){M1=(M1/10+1)*10+M1%10;if(M1>=60){M1=M1%10;};returnM1;}
unsignedcharM0_set(unsignedcharM0){if(M0%10==9)M0=M0/10*10;elseM0=M0%10+1+M0/10*10;returnM0;}
voiddisplay(void)
{
charcodenum[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
charcodetv[]={0xfe,0xfd,0xfb,0xf7};
unsignedchark,i;
switch(flag)
{case0:
{load_LED(h_data,m_data);break;}
case1:
{load_LED(h_data,m_data);break;}
case2:
{load_LED(hdata1_on,mdata1_on);break;}
case3:
{load_LED(hdata1_off,mdata1_off);break;}
case4:
{load_LED(hdata2_on,mdata2_on);break;}
case5:
{load_LED(hdata2_off,mdata2_off);break;}
case6:
{load_LED(hdata1_on,mdata1_on);break;}
case7:
{load_LED(hdata1_off,mdata1_off);break;}
case8:
{load_LED(hdata2_on,mdata2_on);break;}
case9:
{load_LED(hdata2_off,mdata2_off);break;}
default:
{P1=0xFF;load_LED(h_data,m_data);break;}}
n++;
{if(flag)
{if(n>180)
n=0;
if(k==w)
{if(n<90)
{i=led[k];
P0=num[10];/*该LED位不显示(短时消隐),送消隐段码*/
P2=tv[k];/*送需要显示的LED位码*/
delay(10);/*短时消隐延时,有利于显示位的切换*/
P0=num[i];
if(k==1)/*如果送显示的LED为分的个位*/
P0=num[i]|0x80;/*则将分的个显示码附加小数点*/
delay(100);/*显示延时,让LED充分点亮*/}
else/*后90次消隐*/
{P0=num[10];/*送LED消隐段码*/
if(k==1)/*如果要消隐的是分的个位*/
P0=num[10]|0x80;/*则将分的个位后的小数点点亮(小数点不闪烁)*/
P2=tv[k];/*送消隐的位码(即哪个LED需要闪烁)*/
delay(100);/*消隐延时,让其充分消隐*/}
else/*如果送显示的位不调整的位,则不需要闪烁显示*/
{i=led[k];/*将该LED位需要显示的数存于i中*/
P0=num[10];/*该LED位不显示(短时消隐),送消隐段码*/
P2=tv[k];/*送需要显示的LED位码*/
delay(10);/*短时消隐延时,有利于显示位的切换*
P0=num[i];
if(k==1)/*如果送显示的LED为分的个位*/
{P0=num[i]|0x80;}/*则将分的个显示码附加小数点*/
delay(100);/*显示延时,让LED充分点亮*/}}
else/*如果不是处在设置模式,则正常显示*/
{load_LED(h_data,m_data);
i=led[k];/*将该LED位需要显示的数存于i中*/
P0=num[10];/*该LED位不显示(短时消隐),送消隐段码*/
P2=tv[k];/*送需要显示的LED位码*/
delay(10);/*短时消隐延时,有利于显示位的切换*/
P0=num[i];
if(k==1)/*如果送显示的LED为分的个位*/
{P0=num[i]|0x80;}/*则将分的个显示码附加小数点*/
delay(100);/*显示延时,让LED充分点亮*/}
}
}
/**************************************************************************/
voidadjust(void)/*调整函数定义*/
{if(P3_0==0)/*选择键按下*/
{delay(100);
while(P3_0==0){flag1++;display();}
if(flag1>50)
{flag1=0;flag++;
if(flag>=10){flag=0;}/*对flag清零*/
switch(flag)/*调整模式选择,原理是通过调整键按下的次数来选择*/
{case0:
{P1=0xFF;TR0=1;break;}
case1:
{P1=0xFF;P1_0=0;TR0=0;break;}
case2:
{P1=0xFF;P1_1=0;P1_3=0;P1_5=0;TR0=1;break;}
case3:
{P1=0xFF;P1_1=0;P1_3=0;P1_6=0;TR0=1;break;}
case4:
{P1=0xFF;P1_1=0;P1_4=0;P1_5=0;TR0=1;break;}
case5:
{P1=0xFF;P1_1=0;P1_4=0;P1_6=0;TR0=1;break;}
case6:
{P1=0xFF;P1_2=0;P1_3=0;P1_5=0;TR0=1;break;}
case7:
{P1=0xFF;P1_2=0;P1_3=0;P1_6=0;TR0=1;break;}
case8:
{P1=0xFF;P1_2=0;P1_4=0;P1_5=0;TR0=1;break;}
case9:
{P1=0xFF;P1_2=0;P1_4=0;P1_6=0;TR0=1;break;}
default:
{P1=0xFF;TR0=1;break;}}}}
if(P3_1==0)/*移位控制键,在4个LED上来回移动,选择需要设置的LED位*/
{delay(100);/*消抖*/
while(P3_1==0)display();
if(flag){w=w+1;
if(w>=4)w=0;}}
if(P3_2==0)/*加1操作*/
{delay(100);/*消抖*/while(P3_2==0)display();
if(flag)/*当处于调整模式时,对所选择的LED进行加1*/
{switch(flag)
{case1:
{if(w==0){h_data=H1_set(h_data);display();}
if(w==1){h_data=H0_set(h_data);display();}
if(w==2){m_data=M1_set(m_data);display();}
if(w==3){m_data=M0_set(m_data);display();}
break;}
case2:
{if(w==0){hdata1_on=H1_set(hdata1_on);display();}if(w==1){hdata1_on=H0_set(hdata1_on);display();}
if(w==2){mdata1_on=M1_set(mdata1_on);display();}
if(w==3){mdata1_on=M0_set(mdata1_on);display();}
break;}
case3:
{if(w==0){hdata1_off=H1_set(hdata1_off);display();}if(w==1){hdata1_off=H0_set(hdata1_off);display();}
if(w==2){mdata1_off=M1_set(mdata1_off);display();}
if(w==3){mdata1_off=M0_set(mdata1_off);display();}
break;}
case4:
{if(w==0){hdata2_on=H1_set(hdata2_on);display();}if(w==1){hdata2_on=H0_set(hdata2_on);display();}
if(w==2){mdata2_on=M1_set(mdata2_on);display();}
if(w==3){mdata2_on=M0_set(mdata2_on);display();
break;}
case5:
{if(w==0){hdata2_off=H1_set(hdata2_off);display();}if(w==1){hdata2_off=H0_set(hdata2_off);display();}
if(w==2){mdata2_off=M1_set(mdata2_off);display();}
if(w==3){mdata2_off=M0_set(mdata2_off);display();}
break;}
default:
{TR0=1;break;}}}}
if(P3_3==0)/*启动定时开关按钮*/
{delay(100);/*当调整键按下时,延时消抖,flag2控制延迟时间*/
while(P3_3==0){flag2++;display();}
if(flag2>50)
{flag2=0;display();if(start)start=0;elsestart=1;}}}
voidtimer0()interrupt1/*一号定时器中断T0服务子程序(产生一溢出)*/
{counter++;/*counter对定时中断进行计数,10000次中断为1秒*/
if(counter==100)
{s_data++;/counter=0;}
if(s_data==60)/*如果秒计满60则向分进位,并将秒清0*/
{s_data=0;m_data++;/*分进位*/}
if(m_data==60)/*如果分计满60则向时进位,并将分清0*/
{m_data=0;/*分清0*/h_data++;/*时进位*/}
if(h_data==24)/*如果时计满24则将时清0*/
{h_data=0;/*时清0*/}}
main(void)
{IE=0x82;/*EA|-|-|ES|ET1|EX1|ET0|EX0=10000010,中断允许控制*/
TMOD=0x02;
TH0=-200;/*计数常数寄存器设置,中断定时时间为(256+200)*12/f0*/
TL0=-200;/*定时计数器装初值*/
TR0=1;/*启动定时器T0,TCON为TF1|TR1|TF0|TR0|IE1|IT1|IE0|IT0*/
P3_6=1;/*关断定时开关1*/
P3_7=1;/*关断定时开关2*/
while
(1)/*以下为死循环*/
{adjust();/*调用调整函数*/
K1_on=h_data>hdata1_on&&h_datam_data>=mdata1_on&&m_data=mdata1_on&&m_dataK2_on=h_data>hda