专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

单片机数字钟程序

作者:佚名   来源:本站原创   点击数:  更新时间:2008年06月26日   【字体:

#include <at89x52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define A 1

uint year;
uchar count;
uchar flag=0;
uchar month,date,hour,min,sec;
uchar code monthdata[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//uchar code disdata[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar code disdata[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//uchar reg;

void sysini(void);
void timer2(void);
void delayX1ms(uint);
void delay(void);
void display(void);
void display2(uchar);
void change(void);
uchar weekday(void);
uchar daysofmonth(void) reentrant;
bit isleap(void) reentrant;

;c语言编写的单片机数字钟程序 http://www.51hei.com 出品
;主程序开始。

void main(void)
{sysini();
 while(1)
   {if(P3_2) display();//P3.0--mode,P3.1--set
    else change();
   }
   display();
}

void sysini(void)
{
year=2004;month=1;date=1;
hour=0;min=0;sec=0;
count=0;

T2CON=0x00;T2MOD=0x00;
TH2=0x3c;TL2=0xb0;
RCAP2H=0x3c;RCAP2L=0xb0; //50ms
TR2=1;ET2=1;
SCON=0x00;
EA=1;


}

void timer2(void) interrupt 5
{TF2=0;
 if(++count>=20)
  {count=0;
   if(++sec>=60)
     {sec=0;
      if(++min>=60)
        {min=0;
         if(++hour>=24)
           {hour=0;
            if(++date>daysofmonth())
              {date=1;
               if(++month>12)
                 {month=1;
                  year++;
                 }
              }
            }
         }
      }
  }
}

void delayX1ms(uint count)
{uint i,j;
 for(i=0;i<count;i++)
   for(j=0;j<120;j++)//120
     ;
}

void delay(void)
{uchar a=10;
while(a)
  a--;
}

void display(void)
{

SBUF=disdata[date%10];delay();P1=0xfe;delayX1ms(1);P1=0xff;
if(date/10) SBUF=disdata[date/10]; else SBUF=0x00;delay();P1=0xfd;delayX1ms(1);P1=0xff;
SBUF=disdata[month%10];delay();P1=0xfb;delayX1ms(1);P1=0xff;
if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;delay();P1=0xf7;delayX1ms(1);P1=0xff;
SBUF=disdata[year%10];delay();P1=0xef;delayX1ms(1);P1=0xff;
SBUF=disdata[year/10%10];delay();P1=0xdf;delayX1ms(1);P1=0xff;
SBUF=disdata[year/100%10];delay();P1=0xbf;delayX1ms(1);P1=0xff;
SBUF=disdata[year/1000];delay();P1=0x7f;delayX1ms(1);P1=0xff;
SBUF=disdata[sec%10];delay();P2=0xfe;delayX1ms(1);P2=0xff;
SBUF=disdata[sec/10];delay();P2=0xfd;delayX1ms(1);P2=0xff;
SBUF=disdata[min%10];delay();P2=0xfb;delayX1ms(1);P2=0xff;
SBUF=disdata[min/10];delay();P2=0xf7;delayX1ms(1);P2=0xff;
SBUF=disdata[hour%10];delay();P2=0xef;delayX1ms(1);P2=0xff;
if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;delay();P2=0xdf;delayX1ms(1);P2=0xff;
SBUF=disdata[weekday()];delay();P2=0xbf;delayX1ms(1);P2=0xff;

}

 

 


void display2(uchar a)
{

if(a=='d') SBUF=0x00;else SBUF=disdata[date%10];delay();P1=0xfe;delayX1ms(1);P1=0xff;
if(a=='d') SBUF=0x00;else{if(date/10) SBUF=disdata[date/10];else SBUF=0x00;}delay();P1=0xfd;delayX1ms(1);P1=0xff;
if(a=='m') SBUF=0x00;else SBUF=disdata[month%10];delay();P1=0xfb;delayX1ms(1);P1=0xff;
if(a=='m') SBUF=0x00;else{if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;}delay();P1=0xf7;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year%10];delay();P1=0xef;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/10%10];delay();P1=0xdf;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/100%10];delay();P1=0xbf;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/1000];delay();P1=0x7f;delayX1ms(1);P1=0xff;
if(a=='S') SBUF=0x00;else SBUF=disdata[sec%10];delay();P2=0xfe;delayX1ms(1);P2=0xff;
if(a=='S') SBUF=0x00;else SBUF=disdata[sec/10];delay();P2=0xfd;delayX1ms(1);P2=0xff;
if(a=='M') SBUF=0x00;else SBUF=disdata[min%10];delay();P2=0xfb;delayX1ms(1);P2=0xff;
if(a=='M') SBUF=0x00;else SBUF=disdata[min/10];delay();P2=0xf7;delayX1ms(1);P2=0xff;
if(a=='H') SBUF=0x00;else SBUF=disdata[hour%10];delay();P2=0xef;delayX1ms(1);P2=0xff;
if(a=='H') SBUF=0x00;else {if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;}delay();P2=0xdf;delayX1ms(1);P2=0xff;
SBUF=disdata[weekday()];delay();P2=0xbf;delayX1ms(1);P2=0xff;

}

/*void display(void)
{
 P1=0x7f;P2=0xff;SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[date%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(date/10) SBUF=disdata[date/10]; else SBUF=0x00;delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[month%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[year%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[year/10%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[year/100%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);SBUF=disdata[year/1000];delayX1ms(1);SBUF=0x00;
P1=0xff;P2=0x7f;
P2=_crol_(P2,1);SBUF=disdata[sec%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[sec/10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[min%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[min/10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[hour%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[weekday()];delayX1ms(1);SBUF=0x00;

}

 

 


void display2(uchar a)
{
P1=0x7f;P2=0xff;SBUF=0x00;
P1=_crol_(P1,1);if(a=='d') SBUF=0x00;else SBUF=disdata[date%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='d') SBUF=0x00;else{if(date/10) SBUF=disdata[date/10];else SBUF=0x00;}delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='m') SBUF=0x00;else SBUF=disdata[month%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='m') SBUF=0x00;else{if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;}delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='y') SBUF=0x00;else SBUF=disdata[year%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='y') SBUF=0x00;else SBUF=disdata[year/10%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='y') SBUF=0x00;else SBUF=disdata[year/100%10];delayX1ms(1);SBUF=0x00;
P1=_crol_(P1,1);if(a=='y') SBUF=0x00;else SBUF=disdata[year/1000];delayX1ms(1);SBUF=0x00;
P1=0xff;P2=0x7f;
P2=_crol_(P2,1);if(a=='S') SBUF=0x00;else SBUF=disdata[sec%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(a=='S') SBUF=0x00;else SBUF=disdata[sec/10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(a=='M') SBUF=0x00;else SBUF=disdata[min%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(a=='M') SBUF=0x00;else SBUF=disdata[min/10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(a=='H') SBUF=0x00;else SBUF=disdata[hour%10];delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);if(a=='H') SBUF=0x00;else {if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;}delayX1ms(1);SBUF=0x00;
P2=_crol_(P2,1);SBUF=disdata[weekday()];delayX1ms(1);SBUF=0x00;

}*/

void change(void)//这个程序写的很乱,其实应该有更简单的方法
{
bit a=0;
uint b=0,c=0;
while(!P3_2);
while(1)
 {if(!P3_3)
   {delayX1ms(10);
    if(!P3_3)
      {while(!P3_3)
        {if(++c>=1000)
           {c=0;
            display();
            while(!P3_3)
              {display();if(++c>=10)
        {c=0;
                if(++min>=60)min=0;
                 display();
                  }
               }
            }
         }
       if(++min>=60)min=0;
       c=0;
       }
     a=1;
     b=0;
    }
   else
    {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
  if(a)display();
     else display2('M');
     if(++b>=20)
       {b=0;
        a=!a;
       }
     }
   if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }

while(1)
 {if(!P3_3)
   {delayX1ms(10);
    if(!P3_3)
      {while(!P3_3)
       {if(++c>=1000)
           {c=0;
            display();
            while(!P3_3)
               {display();if(++c>=10)
        {c=0;
                 if(++hour>=24)hour=0;
                 display();
                  }
               }
            }
         }
       if(++hour>=24)hour=0;
       c=0;
       }
     a=1;
     b=0;
    }
   else
    {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 if(a)display();
     else display2('H');
     if(++b>=20)
       {b=0;
        a=!a;
       }
     }
   if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }

while(1)
 {if(!P3_3)
   {delayX1ms(10);
    if(!P3_3)
      {while(!P3_3)
        {if(++c>=1000)
           {c=0;
            display();
            while(!P3_3)
              {display();if(++c>=10)
        {c=0;
                 if(++date>daysofmonth())date=1;
                 display();
                  }
               }
            }
         }
       if(++date>daysofmonth())date=1;
       c=0;
       }
     a=1;
     b=0;
    }
   else
    {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 if(a)display();
     else display2('d');
     if(++b>=20)
       {b=0;
        a=!a;
       }
     }
   if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }

while(1)
 {if(!P3_3)
   {delayX1ms(10);
    if(!P3_3)
      {while(!P3_3)
        {if(++c>=1000)
           {c=0;
            display();
            while(!P3_3)
              {display();if(++c>=10)
        {c=0;
                if(++month>12)month=1;
                 display();
                  }
               }
            }
         }
       if(++month>12)month=1;
       c=0;
       }
     a=1;
     b=0;
    }
   else
    {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 if(a)display();
     else display2('m');
     if(++b>=20)
       {b=0;
        a=!a;
       }
     }
   if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }

while(1)
 {if(!P3_3)
   {delayX1ms(10);
    if(!P3_3)
      {while(!P3_3)
        {if(++c>=1000)
           {c=0;
            display();
            while(!P3_3)
              {display();if(++c>=10)
        {c=0;
                 if(++year>=2100)year=2000;
                 display();
                  }
               }
            }
         }
       if(++year>=2100)year=2000;
       c=0;
       }
     a=1;
     b=0;
    }
   else
    {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 if(a)display();
     else display2('y');
     if(++b>=20)
       {b=0;
        a=!a;
       }
     }
   if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }
 while(1)
 {if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 if(a)display();
  else display2('S');
  if(++b>=20)
    {b=0;
     a=!a;
    }
  if(!P3_3)
    {delayX1ms(10);
     if(!P3_3)
       sec=0;
    }
  if(!P3_2){delayX1ms(10);if(!P3_2){while(!P3_2);  break;}}
 }

}

uchar weekday(void)
{uint a=year-1;
 uint b=0;
 uchar i;
 for(i=1;i<month;i++)
    b+=monthdata[i-1];
 b+=date;
 if(month>2&&isleap())
    b++;
 i=(a+a/4-a/100+a/400+b)%7;
 if(i)return i;
 else return 8;
}

uchar daysofmonth(void) reentrant
{if(month!=2) return monthdata[month-1];
 else if(isleap()) return 29;
 else return 28;
}

bit isleap(void) reentrant
{return (year%4==0&&year%100!=0||year%400==0);
}

到这里这个单片机数字钟c程序就完毕了程序很长而且没什么注释,希望大家慢慢搞定。有什么问题可在本站论坛提出,马上为您解决。

 

关闭窗口