CLOCK

#include "reg51.h"        
xdata unsigned char OUT_SEG _at_ 0xf000;
xdata unsigned char OUT_BIT _at_ 0xe000;
unsigned char LEDBuf[] ={0,0,0,0,0,0};
code unsigned char LEDMAP[] = {
  0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
  0x80, 0x90 ,0xff
};

sbit P12=0X92;// -
sbit P13=0X93;//switch hour,min,sec
char sec=0,min=0,hour=0,key0=0;
unsigned char T1s ;


#define LEDNUM 6


void Timer0 () interrupt 1
{
 TH0 = (65536 - 50000)/256; 
 TL0 = (65536 - 50000)%256; 
 T1s ++;
 if (T1s == 20)      //1s
 {
  T1s = 0;
  sec++;
  if(sec == 60)
  {
  sec = 0;
   min++;
  if(min == 60)
   {
  min = 0;
    hour++;
    if(hour == 24)
    {
    hour = 0;
    }
   }
  }
  }
  LEDBuf[5]=hour/10;
  LEDBuf[4]=hour%10;
  LEDBuf[3]=min/10;
  LEDBuf[2]=min%10;
  LEDBuf[1]=sec/10;
  LEDBuf[0]=sec%10;
}
void Delay(unsigned char CNT)
{
 unsigned char i,j;
 for(i=0;i for(j=0;j<100;j++);
}
void DisplayLED()
{
 unsigned char i,outbit=0x01;
 for(i=0;i {
  OUT_BIT=0;
  OUT_SEG=LEDMAP[LEDBuf[i]];
  OUT_BIT=outbit;
  Delay(1);
  outbit<<=1;
  }
}
void LongDelay()
{
 int i;
 for(i=0;i<50;i++)
  DisplayLED();

}
void ScanKeyboard()
{
 
 if(P13==1)
 {

  LongDelay();
  if(P13==1){
    TR0=0; //stop timer interrupt
    key0++;
    if(key0==4){TR0=1;  key0=0;}//Start timer interrupt
  }
  }
 switch(key0)
 {
  case 1://regulate second
  {

    if(P12==1)
    {
     LongDelay();
     if(P12==1) sec--;//- second
     if (sec<00)  sec=59;
     }
   
       DisplayLED();
      break;
   }
   case 2:
   {

       if(P12==1)
      {
       LongDelay();
       if(P12==1) min--;
       if (min<00) min=59;
      }
    
      LEDBuf[2]=10;
      LEDBuf[3]=10;
      DisplayLED();
      LEDBuf[3]=min/10;
      LEDBuf[2]=min%10;
      DisplayLED();
      break;
    }
    case 3:
    {

      if(P12==1)
      {
       LongDelay();
       if(P12==1) hour--;
       if (hour<00)  hour=59;
      }
    
      LEDBuf[5]=10;
      LEDBuf[4]=10;
      DisplayLED();
      LEDBuf[5]=hour/10;
      LEDBuf[4]=hour%10;
      DisplayLED();
      break;
    }

    default:break;

 }

}
void main()
{
   TMOD = 0X01;
   TH0 = (65536 - 50000)/256;
   TL0 = (65536 - 50000)%256;
   IE = 0x82;
   TR0 = 1;
   P1=0xff;
   while(1)
   {
    ScanKeyboard();
      DisplayLED();
 

 }
}
 

你可能感兴趣的:(CLOCK)