t;voiddisplay();voidkeyscan();voidkeyprocess(unsignedcharkey);voiddelayMS(uintt){uinti;while(t--)for(i=0;i<125;i++);}voidinit()//初始{keyrelease=1;buf=0x0f;min2=2;//避免打开就鸣叫,设闹钟分=2flag=0;TMOD=0x21;TH0=(65536-5000)/256;TL0=(65536-5000)%256;TH1=0xfd;TL1=0xfd;SCON=0x50;PCON=0x00;TR1=1;IP=0x10;TR0=0;IE=0x92;//开总、定时器0,串口中断}voidmain(){init();while(1){keyscan();display();if(hou==hou2&&min==min2&&sec<5)//仿真使用{alarm=~alarm;}//if(hou==hou2&&min==min2&&sec<5)//实际硬件使用//{//alarm=0;//}//else//alarm=1;}}voidkeyscan()//键盘扫描{keyinput=P2&0x0f;if(keyinput!=0x0f){delayMS(5);if(keyinput!=0x0f){if(keyrelease==1)//不可缺{keyrelease=0;//按住不放buf=keyinput;//buf用来暂时存放键值}}}else//没按下{while(keyinput!=0x0f);//如果不松手就停在此处keyrelease=1;keyprocess(buf);buf=0x0f;}}//按键处理函数voidkeyprocess(unsignedcharkey)//键值处理,正常计时,设置时分秒{switch(key)