⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 学习板时钟程序.c

📁 51单片机电子钟
💻 C
字号:
#include <reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
uchar count=1;
uchar key;
uchar kc;
uchar sec=0;
uchar min=0;
uchar hour=0;
uchar day=1;
uchar mon=1;
uint year=2007;
uchar mod=0;
uchar check=0;
bit shine=1;
uchar code numtab[34]={0xde,0x06,0xd5,0x57,0x0f,0x5b,0xdb,0x16,0xdf,0x5f,0x9f,0xcb,0xd8,0xc7,0xd9,0x99,0xfe,0x26,0xf5,0x77,0x2f,0x7b,0xfb,0x36,0xff,0x7f,0xbf,0xeb,0xf8,0xe7,0xf9,0xb9,0x01,0x00};
uchar x[8];


 ////////////////////////////////////////////////////////////////////////////////////////////
void delay(uint t)                                                               /*延时子程序*/
{while(t--);
}

/////////////////////////////////////////////////////////////////////////////////////////////
void numdisplay(void)                                                        /*数码管显示子程序*/
{uchar i;
XBYTE[0x9fff];
if(check==1)
{if(shine==1) {P1=numtab[x[0]];}
else {P1=numtab[33];}}
else P1=numtab[x[0]];
XBYTE[0xdfff];
XBYTE[0xffff];
delay(400);
 for(i=1;i<8;i++)
 {if(i+1==check)
 {if(shine==1) {P1=numtab[x[i]];}
 else {P1=numtab[33];}}
 else P1=numtab[x[i]];
 XBYTE[0xbfff];
 XBYTE[0xffff];
 delay(400);
 }
}

 /////////////////////////////////////////////////////////////////////////////////////////////
 void time(void)                                                          /*时间子程序*/
{ sec++;
  if(sec>=60)
  {min++;
   sec=0;}
   if(min>=60)
   {hour++;
    min=0;}
    if(hour>=24)
	{hour=0;
	 day++;
     if(mon==2||mon==4||mon==6||mon==9||mon==11)
	    {if(mon==2) {if(!(year%4)&&(year%100)||!(year%400)) {if(day>=30) {day=1;mon=3;}}
		            else if(day>=29) {day=1;mon=3;}}
         else if(day>=31){day=1;mon++;}}
	 else if(day>=32) {day=1;mon++;}}
	 if(mon>=13) {mon=1;year++;}           
 }

 
/**********************************************************************************************************/
void T0_delay (void) interrupt 1 using 1                               //中断亮灯程序//
{   
    TH0=47; 
    TL0=224; 
	if(count==10) {shine=0;}
    if(count==20) {shine=1;time();count=0;}
    count++;
}

//////////////////////////////////////////////////////////////////////////////////////////////
void keyin(void)                               //键盘扫描
{unsigned char i,j,w;
  delay(500);
  if(~XBYTE[0xf800]&0xf)
      {w=XBYTE[0xf8fe]&0x0f;
       if(~w&0x0f)
          {i=4;
           j=w;
           w=0xff;
           }
        w=XBYTE[0xf8fd]&0x0f;
        if(~w&0x0f)
           {i=3;
            j=w;
            w=0xff;
            }
        w=XBYTE[0xf8fb]&0x0f;
        if(~w&0x0f)
          {i=2;
           j=w;
           w=0xff;
          }
        w=XBYTE[0xf8f7]&0x0f;
        if(~w&0x0f)
           {i=1;
            j=w;
            w=0xff;
            }
        w=XBYTE[0xf8ef]&0x0f;
        if(~w&0x0f)
           {i=0;
            j=w;
            w=0xff;
           }
        w=~j;
        key=(w<<4)|i;
     }
 while(~XBYTE[0xf800]&0xf);
  switch(key)
  {case 0x80:kc=20;break;
   case 0x40:kc=15;break;
   case 0x20:kc=0;break;
   case 0x10:kc=5;break;
   case 0x81:kc=19;break;
   case 0x41:kc=14;break;
   case 0x21:kc=9;break;
   case 0x11:kc=4;break;
   case 0x82:kc=18;break;
   case 0x42:kc=13;break;
   case 0x22:kc=8;break;
   case 0x12:kc=3;break;
   case 0x83:kc=17;break;
   case 0x43:kc=12;break;
   case 0x23:kc=7;break;
   case 0x13:kc=2;break;
   case 0x84:kc=16;break;
   case 0x44:kc=11;break;
   case 0x24:kc=6;break;
   case 0x14:kc=1;break;}
if(mod)
{switch(check)
 {case 0:break;
  case 1:if(kc<10) year=2000+kc*10; check=2; break;
  case 2:if(kc<10) year=year+kc; check=4;break;
  case 4:if(kc<2) mon=kc*10; check=5;break;
  case 5:if(kc<10) mon=mon+kc; check=7;break;
  case 7:if(kc<4) day=kc*10; check=8;break;
  case 8:if(kc<10) day=day+kc; check=0;break;}}
else
{switch(check)
  {case 0:break;
  case 1:if(kc<3) hour=kc*10; check=2; break;
  case 2:if(kc<10) hour=hour+kc; check=4;break;
  case 4:if(kc<6) min=kc*10; check=5;break;
  case 5:if(kc<10) min=min+kc; check=7;break;
  case 7:if(kc<6) sec=kc*10; check=8;break;
  case 8:if(kc<10) sec=sec+kc; check=0;break;}}
  switch(kc)
{case 11:if(++mod>1) mod=0;break;
 case 12:if(++check>8) check=0; if(check==3) check=4; if(check==6) check=7; break;
}
 EX1=1;
 }

  /////////////////////////////////////////////////////////////////////////////////////////////
void keybreak(void)  interrupt 2 using 0    //键盘中断
{
  EX1=0;
  key=0;
  keyin();}

//////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
TMOD=0x01;
TH0=47;
TL0=224;
ET0=1;
EA=1;
EX1=1;
TR0=1;
XBYTE[0xf800];
x[2]=32;
x[5]=32;
while(1)
{
 switch(mod)
  {case 0:x[7]=sec%10; x[6]=sec/10;x[4]=min%10; x[3]=min/10; x[1]=hour%10; x[0]=hour/10;break;
  case 1:x[7]=day%10; x[6]=day/10;x[4]=mon%10; x[3]=mon/10;x[1]=year%10; x[0]=(year/10)%10;break;}
numdisplay();
} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -