#include <reg52.h> 
     
 
     
      #include <string.h> 
     
 
     
      #define uchar unsigned char 
     
 
     
      #define uint unsigned int 
     
 
     
      sbit key1=P3^2; //
      定义硬件引脚 
     
 
     
      sbit key2=P3^3; 
     
 
     
      sbit key3=P3^4; 
     
 
     
      sbit key4=P3^5; 
     
 
     
      sbit key5=P3^6; 
     
 
     
      sbit key6=P3^7; 
     
 
     
      sbit bz=P1^6; 
     
 
     
      sbit w1=P2^0; 
     
 
     
      sbit w2=P2^1; 
     
 
     
      sbit w3=P2^2; 
     
 
     
      sbit w4=P2^3; 
     
 
     
      sbit w5=P2^4; 
     
 
     
      sbit w6=P2^5; 
     
 
     
      #define all_off {P2=0X00;} 
     
 
     
      uint i,jihour,count,timer,count_ss; 
     
 
     
      uchar sec=52,min=59,hour=11,set,num,mb,count_mb; 
     
 
     
      bit flag_mb,flag_mb_run,flag_bs; 
     
 
     
      code uchar 
     
 
     
      shuma[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //
      显示段码共阳数码管
     
 
      
     
      //
      延时函数 
     
 
     
      void delay(uchar t) 
     
 
     
      { 
     
 
     
      uchar i,j; 
     
 
     
      for(i=0;i<t;i++) 
     
 
     
      { 
     
 
     
      for(j=14;j>0;j--); 
     
 
     
      } 
     
 
     
      } 
     
 
     
      void disp() //
      数码管显示函数 
     
 
     
      { 
     
 
     
      if(flag_mb) //
      显示秒表 
     
 
     
      { 
     
 
     
      all_off;P0=shuma[10];w1=1;delay(5); //
      无显示 
     
 
     
      all_off;P0=shuma[10];w2=1;delay(5); 
     
 
     
      all_off;P0=shuma[mb/10];w3=1;delay(5);//
      显示秒 
     
 
     
      all_off;P0=shuma[mb%10];w4=1;delay(5); 
     
 
     
      all_off;P0=shuma[count_mb/10];w5=1;delay(5); //
      百分秒 
     
 
     
      all_off;P0=shuma[count_mb%10];w6=1;delay(5); 
     
 
     
      } 
     
 
     
      else //
      显示 时间 
     
 
     
      { 
     
 
     
      all_off;P0=shuma[hour/10];w1=1;delay(5); //
      时 
     
 
     
      all_off;if(set==1)P0=shuma[hour%10]&0x7f;else 
     
 
     
      P0=shuma[hour%10];w2=1;delay(5); 
     
 
     
      all_off;P0=shuma[min/10];w3=1;delay(5); //
      分 
     
 
     
      all_off;if(set==2)P0=shuma[min%10]&0x7f;else 
     
 
     
      P0=shuma[min%10];w4=1;delay(5);
     
 
     
      all_off;P0=shuma[sec/10];w5=1;delay(5); //
      秒 
     
 
     
      all_off;if(set==3)P0=shuma[sec%10]&0x7f;else 
     
 
     
      P0=shuma[sec%10];w6=1;delay(5); 
     
 
     
      } 
     
 
     
      } 
     
 
     
      void keyscan() 
     
 
     
      //
      按键扫描函数 
     
 
     
      { 
     
 
     
      if(!key1) 
     
 
     
      //
      检测按下 
     
 
     
      { 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key1) //+ 
     
 
     
      { 
     
 
     
      switch(set) 
     
 
     
      { 
     
 
     
      case 1:if(hour<23)hour++;break; //
      设置时 
     
 
     
      case 2:if(min<59)min++;break; //
      设置分 
     
 
     
      case 3:if(sec<59)sec++;break; //
      设置秒 
     
 
     
      default:break; 
     
 
     
      } 
     
 
     
      while(!key1){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      if(!key2) 
     
 
     
      //
      检测按下 
     
 
     
      { 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key2) //- 
     
 
     
      { 
     
 
     
      switch(set) 
     
 
     
      {
     
 
     
      case 1:if(hour>0)hour--;break; //
      设置时 
     
 
     
      case 2:if(min>0)min--;break; //
      设置分 
     
 
     
      case 3:if(sec>0)sec--;break; //
      设置秒 
     
 
     
      default:break; 
     
 
     
      } 
     
 
     
      if(flag_mb)flag_mb_run=!flag_mb_run; 
     
 
     
      while(!key2){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      if(!key3) 
     
 
     
      //
      检测按下 
     
 
     
      { 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key3) //
      设置当前时间 
     
 
     
      { 
     
 
     
      set++;if(set==4)set=0; 
     
 
     
      if(set==0)TR0=1;else TR0=0; 
     
 
     
      while(!key3){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      if(!key4) 
     
 
     
      //
      检测按下 
     
 
     
      { 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key4) //
      秒表 
     
 
     
      { 
     
 
     
      flag_mb=!flag_mb;count_mb=0;mb=60; 
     
 
     
      while(!key4){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      if(!key5) 
     
 
     
      //
      检测按下 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key5) //
      秒表启动或停止 
     
 
     
      { 
     
 
     
      flag_mb_run=!flag_mb_run; 
     
 
     
      while(!key5){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      if(!key6) 
     
 
     
      //
      检测按下 
     
 
     
      { 
     
 
     
      delay(10); //
      延时消抖动 
     
 
     
      if(!key6) //
      秒表清零 
     
 
     
      { 
     
 
     
      count_mb=0;mb=60; 
     
 
     
      while(!key6){;}//
      检测松手 
     
 
     
      } 
     
 
     
      } 
     
 
     
      } 
     
 
     
      void main() 
     
 
     
      { 
     
 
     
      TMOD |= 0x01; //
      初始化定时器 
      0 
     
 
     
      TL0 = 0x00; 
     
 
     
      //
      设置定时初值 
     
 
     
      TH0 = 0xDC; 
     
 
     
      //
      设置定时初值 
      10MS @11.0592MHZ 
     
 
     
      EA=1; 
     
 
     
      ET0=1; 
     
 
     
      TR0=1; 
     
 
     
      while(1) 
     
 
     
      { 
     
 
     
      keyscan(); //
      按键扫描函数 
     
 
     
      disp(); 
     
 
     
      if(min==59 && sec==50) //
      整点前 
      10 
      秒 
     
 
     
      flag_bs=1; //
      报时 
     
 
     
      } 
     
 
     
      } 
     
 
     
      void Tim() interrupt 1 
     
 
     
      { 
     
 
     
      TL0 = 0x00; 
     
 
     
      //
      设置定时初值 
     
 
     
      TH0 = 0xDC; 
     
 
     
      //
      设置定时初值 
      10MS @11.0592MHZ 
     
 
     
      jihour++; 
     
 
     
      if(jihour==100) //1 
      秒 
     
 
     
      {jihour=0;sec++;} 
     
 
     
      if(sec==60) //1 
      分钟 
     
 
     
      {sec=0;min++;} 
     
 
     
      if(min==60) //1 
      小时 
     
 
     
      {min=0;hour++;} 
     
 
     
      if(hour==24) 
     
 
     
      {hour=0;} 
     
 
     
      if(flag_mb&&flag_mb_run) //
      秒表 
     
 
     
      { 
     
 
     
      count_mb--; 
     
 
     
      if(count_mb==255) //1S 
     
 
     
      { 
     
 
     
      count_mb=99; 
     
 
     
      mb--; 
     
 
     
      if(mb==255){mb=flag_mb_run=0;count_mb=0;} 
     
 
     
      } 
     
 
     
      }
     
 
     
      if(flag_bs) //
      蜂鸣器响 
     
 
     
      { 
     
 
     
      count_ss++; 
     
 
     
      if(count_ss==100) //
      约 
      1 
      秒 
     
 
     
      { 
     
 
     
      bz=0; //
      蜂鸣器响 
     
 
     
      } 
     
 
     
      if(count_ss==200) //
      约 
      1 
      秒 
     
 
     
      { 
     
 
     
      count_ss=0; 
     
 
     
      bz=1; 
     
 
     
      //
      蜂鸣器不响 
     
 
     
      num++;if(num==5){num=0;flag_bs=0;} 
     
 
     
      } 
     
 
     
      } 
     
 
     
      }