| 采用按键来启动、停止、复位时钟的显示,但结果不正常。 以下是源程序
 //**************************************************
 //程序功能:时钟,前两位显示分钟,后两位显示秒
 //P1.7按键功能,开始计时
 //P1.6按键功能,停止计时
 //P1.5按键功能,清零,重新开始
 //*************************************************
 
 #include<reg51.h>
 code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
 0x82,0xf8,0x80,0x90,0xff}; //共阳数码管段码
 unsigned int cnt=0;
 unsigned char sec=0;
 unsigned char min=0;
 
 
 void delay1ms()
 {
 unsigned char i;
 for(i=124;i>0;i--);  //延时124*8+10=1002us
 }
 delay10ms()            //延时10ms
 {
 unsigned i,j;
 for(i=0;i<50;i++)
 for(j=0;j<100;j++);
 }
 
 void KEY(void)              //按键判断子程序
 {
 if(P1_7==0)                 //P1.7按键功能,开始计时
 {
 delay10ms();            //软件去抖动
 if(P1_7==0)
 {
 TH0=0x19;          //11.0529MHZ的晶振,250us的定时中断
 TL0=0x19;
 TR0=1;
 }
 }
 else if(P1_6==0)             //P1.6按键功能,停止计时
 {
 delay10ms();
 if(P1_6==0)
 { TR0=0;}
 }
 else if(P1_5==0)           //P1.5按键功能,清零,重新开始
 {
 delay10ms();
 if(P1_5==0)
 {sec=0;min=0;}
 }
 }
 
 void LED_display()       //显示
 {
 P1=0xfe;                  //P1.0=0,选通第一位
 P2=seg7code[min/10];     //显示小时的第一位
 delay1ms();
 P2=0xff;                  //消隐
 P1=0xfd;                  //P1.1=0,选通第二位
 P2=seg7code[min%10];     //显示小时的第一位
 delay1ms();
 P2=0xff;                 //消隐
 P1=0xfb;                   //P1.2=0,选通第三位
 P2=seg7code[sec/10];       //显示分的第一位
 delay1ms();
 P2=0xff;                 //消隐
 P1=0xf7;                   //P1.3=0,选通第四位
 P2=seg7code[sec%10];       //显示分的第二位
 delay1ms();
 P2=0xff;                 //消隐
 }
 
 void timer0 (void) interrupt 1 using 0
 {
 cnt++;             //每过250us,cnt加一
 if(cnt==4000)      //250*4000=1s
 {
 cnt=0;
 sec++;
 if(sec==60)       //60秒,重新计数,分钟加一
 { sec=0;
 min++;
 if(min==60)         //60分,重新计数  {
 min=0;
 }
 
 }
 }
 }
 
 main()
 {
 TMOD=0x02;          //T0,方式2
 
 EA=1;                 //开总中断
 ET0=1;               //开定时器T0中断
 
 while(1)
 {
 KEY();
 LED_display();
 
 }
 }
 |