这是我上个月做的数字钟程序,可以调整时间,用了一个星期的时间附件里面包含数字钟的源代码
都是自己原创的请多多指教。- #include <REGX52.H>
- #define Fosc 11059200L
- #define INT_PERIOD 50
- #define OverFreq (1000/INT_PERIOD)
- #define ClockPeriod (0x10000-Fosc/12/OverFreq)
- unsigned char code DispSeg[]={
- //D7 D6 D5 D4 D3 D2 D1 D0==>dp g f e d c b a
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x27,
- 0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
- 0x00
- };
- struct TIME{
- unsigned char hour;
- unsigned char minute;
- unsigned char second;
- unsigned char ms;
- }Time;
- bit flag = 0;
- bit twinkle = 0;
- bit KeyChecked;
- bit WatchRunning;
- sbit K1 = P2^0;
- sbit Keyin = P2^1;
- unsigned char cnt = 0;
- unsigned char timer0_cnt = 0;
- unsigned char DispBuffer[6];
- void Delay(unsigned int xms)
- {
- unsigned char i, j;
- while(xms--){
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- void Translate( void )
- {
- DispBuffer[0] = Time.hour/10;
- DispBuffer[1] = Time.hour%10;
- DispBuffer[2] = Time.minute/10;
- DispBuffer[3] = Time.minute%10;
- DispBuffer[4] = Time.second/10;
- DispBuffer[5] = Time.second%10;
- }
- void timer0_init(void)
- {
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = ClockPeriod >> 8;
- TL0 = ClockPeriod & 0xFF;
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void timer1_init(void)
- {
- TMOD &= 0xF0;
- TMOD |= 0x10;
- TH1 = ClockPeriod >> 8;
- TL1 = ClockPeriod & 0xFF;
- ET1 = 1;
- EA = 1;
- TR1 = 1;
- }
- void InitCounter( void )
- {
- Time.hour = 0;
- Time.minute = 0;
- Time.second = 0;
- WatchRunning = 0;
- }
- void system_init(void)
- {
- InitCounter();
- timer0_init();
- timer1_init();
- }
- void Display( void )
- {
- unsigned char i=0x80;
- unsigned char *ptr=DispBuffer;
- Translate();
- if(!flag)
- {
- while(i!=0x02)
- {
- P0=DispSeg[*ptr++];
- P1 = i;
- Delay(1);
- i=i>>1;
- }
- }
- else if(flag)
- {
- if((cnt%4)==1)
- {
- while(i!=0x08)
- {
- P0=DispSeg[*ptr++];
- P1 = i;
- Delay(1);
- i=i>>1;
- }
- }
- else if((cnt%4)==2)
- {
- while(i!=0x02)
- {
- if((i==0x20)||(i==0x10))
- {
- P0 = 0x00;
- ptr++;
- P1 = i;
- Delay(1);
- i=i>>1;
- }else{
- P0=DispSeg[*ptr++];
- P1 = i;
- Delay(1);
- i=i>>1;
- }
- }
- }
- else if((cnt%4)==3)
- {
- while(i!=0x02)
- {
- if((i==0x80)||(i==0x40))
- {
- P0 = 0x00;
- ptr++;
- P1 = i;
- Delay(1);
- i=i>>1;
- }else{
- P0=DispSeg[*ptr++];
- P1 = i;
- Delay(1);
- i=i>>1;
- }
- }
- }
- else if((cnt%4)==0)
- {
- twinkle = 0;
- while(i!=0x02)
- {
- P0=DispSeg[*ptr++];
- P1 = i;
- Delay(1);
- i=i>>1;
- }
- }
- }
- }
- void KeyProcess( void )
- {
- if(Keyin == 0)
- {
- Display();
- if(Keyin == 0)
- {
- if((cnt%4)==1)
- {
- Time.second ++;
- if(Time.second == 60) Time.second = 0;
- }
- else if((cnt%4)==2)
- {
- Time.minute ++;
- if(Time.minute == 60) Time.minute = 0;
- }
- else if((cnt%4)==3)
- {
- Time.hour ++;
- if(Time.hour == 60) Time.hour = 0;
- }
- while(Keyin == 0) Display();
- }
- }
- }
- void key_pre(void)
- {
- if(K1==0)
- {
- Display();
- if(K1==0)
- {
- cnt++;
- twinkle = 1;
- while(K1==0)
- {
- Display();
- }
- }
- }
- }
- void main( void )
- {
- system_init();
- while(1)
- {
- key_pre();
- KeyProcess();
- Display();
- }
- }
- void Time1Interrupt( void ) interrupt 3
- {
- TH0 = ClockPeriod >> 8;
- TL0 += ClockPeriod & 0xFF;
- Time.ms ++;
- if( Time.ms == 20 )
- {
- Time.ms = 0;
- Time.second ++;
- if( Time.second == 60 )
- {
- Time.second = 0;
- Time.minute ++;
- if( Time.minute == 60 )
- {
- Time.minute = 0;
- Time.hour ++;
- }
- }
- }
- }
- void Time0Interrupt( void ) interrupt 1
- {
- timer0_cnt++;
- TH0 = ClockPeriod >> 8;
- TL0 = ClockPeriod & 0xFF;
- if(timer0_cnt==100)
- {
- if(twinkle)
- {
- flag = !flag;
- }
- timer0_cnt = 0;
- }
- }
复制代码
|