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

📄 text1.c

📁 用汇编语言写的时钟的程序
💻 C
字号:
#include<reg52.h>
code unsigned char ledcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
struct Time
{
 unsigned char flag;
 unsigned char second;
 unsigned char minute;
 unsigned char hour;
 unsigned int ms;

}gTime;
unsigned char tag=0;
unsigned char adjust_tag=0;
void TimeOut(void);
void keyIn(unsigned char keydata);
unsigned char keyOut(void);
unsigned char getkey(void);
void msCall(void);
void SLEDScan(void);
void keyscan(void);
void ledchange(void);
unsigned char keybufWp=0;
unsigned char keybufRp=0;
unsigned char keybuf[8];
unsigned char Disbuf[4]={0x00, 000, 0x00, 0x00};
void Initial (void)
{ 
  CP_RL2=0;
  C_T2=0;
  RCAP2H=-(1843>>8);
  RCAP2L=-1843;
  TR2=1;
  ET2=1;
  EA=1;
}

void Timer1(void) interrupt 5
{
	TF2=0;
  msCall();
 
}

void msCall(void)
{
 SLEDScan(); 
 keyscan();
 if(adjust_tag==0)
    TimeOut();
}

void SLEDScan(void)
{
  static unsigned char sts=0;
   P2|=0x0f;
   switch(sts)
    {
     case 0 ://;break;
     case 1 :P0=ledcode[Disbuf[3]];P2&=0xf7;break;
     case 2 :P0=ledcode[Disbuf[2]];P2&=0xfb;break;  
     case 3 :P0=ledcode[Disbuf[1]];P2&=0xfd;break;
	 case 4 :
	    if( gTime.flag )
		           {
				         P0=0x80;
	                     P2&=0xfd;
						 break;
                    }
         else
	 	       {
		           P0=0x00;
	               P2&=0xfd;
		            break;
                } 
     case 5 :P0=ledcode[Disbuf[0]];P2&=0xfe;break; 
    }
  if(++sts>=6) sts=1;

}

 void keyIn(unsigned char keydata)
  {
   keybuf[keybufWp]=keydata;
    if (++keybufWp>=8) 
	     keybufWp=0;
  }

 unsigned char keyOut(void)
 { unsigned char retvalue;
   while (keybufRp==keybufWp);
   retvalue=keybuf[keybufRp];
   if (++keybufRp>=8) keybufRp=0;
   return(retvalue);
 }
 
void keyscan(void)
{
	static unsigned char sts=0;
	static unsigned char cut=10;
	unsigned char keyvalue;

	if( --cut != 0 ) return;
	cut = 10;
	switch( sts )
	{
   		case 0:// ; 

	 	case 1:
			if( getkey() != 0xff )
 		    {
				keyvalue = getkey();
				sts = 2;
			}
			break;

	 case 2:
	     if(keyvalue!=getkey())
	        {
			 sts=1;	
	 		 break;
			}
			
           else
		   {
		   keyIn(keyvalue);
		   sts=3;
		   }
		   break;
	 case 3:
	     if(getkey()==0xff)
		    sts=4;
		    break;
	 case 4:if(getkey()!=0xff) 
	          {
			  sts=3;
			  break;
			  }
	         else 
			 sts=1;
			 break;
	}
 }


unsigned char getkey(void)
 { unsigned char p,x,y;
   P1=0x0f;
   x=P1;
   P1=0xf0;
   y=P1;
   p=x|y;
   switch(p)
     {  
       case 0xee : return(0x0c);break;               
       case 0xde : return(0x0d);break;
	   case 0xbe : return(0x0e);break;
	   case 0x7e : return(0x0f);break;
	   case 0xed : return(0x08);break;
	   case 0xdd : return(0x09);break;
	   case 0xbd : return(0x0a);break;
	   case 0x7d : return(0x0b);break;
	   case 0xeb : return(0x04);break;
	   case 0xdb : return(0x05);break;
	   case 0xbb : return(0x06);break;
	   case 0x7b : return(0x07);break;
	   case 0xe7 : return(0x00);break;
	   case 0xd7 : return(0x01);break;
	   case 0xb7 : return(0x02);break;
	   case 0x77 : return(0x03);break;
	   case 0xff : return(0xff);break;
     }
 }
void TimeOut(void)
{

 if (++gTime.ms<1000) return;
    {
     gTime.ms=0;
	 tag=1;
	 }
 if ( ++gTime.second<60) return;
     gTime.second=0;
 if ( ++gTime.minute<60) return;
     gTime.minute=0;
 if ( ++gTime.hour<24)  return;
     gTime.hour=0;

}
void mode1( void )/* 模式1  显示分秒 且让第2个小数点常亮*/
{
  Disbuf[3]=gTime.second%10;
  Disbuf[2]=gTime.second/10;
  Disbuf[1]=gTime.minute%10;
  Disbuf[0]=gTime.minute/10;
  gTime.flag=1;
}

void mode2( void )/*模式2   显示时分 让第2个小数点闪烁 */
  {  
   
       Disbuf[3]=gTime.minute%10;
       Disbuf[2]=gTime.minute/10;
       Disbuf[1]=gTime.hour%10;
       Disbuf[0]=gTime.hour/10;
       gTime.flag=(gTime.flag+1)%2;;
	   
  }
unsigned char khit(void)/*判断是否有键按下 有键盘返回值 无返回0*/
{
  return (keybufWp-keybufRp);
}
void changemode(void)/*  分状态显示,每按一次C键,模式切换一次*/
{
 static unsigned char sts=0;//状态标志
 unsigned char Data=0;
	 if(khit())
	  Data=keyOut();
	  if(Data==0xc)
     sts=(sts+1)%2;			
   switch(sts)
   { 
    case 0 : mode1();break;
	case 1 : mode2();break;
   } 
}
void adjust1(void)
{
    unsigned char time;
	   gTime.second=0;
       Disbuf[3]=0x00;
       Disbuf[2]=0x00;
	   Disbuf[1]=0x00;
       Disbuf[0]=0x01;
   while (khit()==0);
     {
        time=keyOut();
        Disbuf[2]=time;
        gTime.second=10*time;  
     }
   while (khit()==0);
     {
       time=keyOut();
       Disbuf[3]=time;
       gTime.second=gTime.second+time; 
     }
  
    
}
void adjust2(void)
{
    unsigned char time;
	 gTime.minute=0;
       Disbuf[3]=0x00;
       Disbuf[2]=0x00;
	   Disbuf[1]=0x00;
       Disbuf[0]=0x02;
   while (khit()==0);
     {
        time=keyOut();
        Disbuf[2]=time;
        gTime.minute=10*time;  
     }
   while (khit()==0);
     {
       time=keyOut();
       Disbuf[3]=time;
       gTime.minute=gTime.minute+time; 
     }

}

void adjust3(void)
{
    unsigned char time;
	 gTime.hour=0;
       Disbuf[3]=0x00;
       Disbuf[2]=0x00;
	   Disbuf[1]=0x00;
       Disbuf[0]=0x03;
   while (khit()==0);
     {
        time=keyOut();
        Disbuf[2]=time;
        gTime.hour=10*time;  
     }
   while (khit()==0);
     {
       time=keyOut();
       Disbuf[3]=time;
       gTime.hour=gTime.hour+time; 
     }
 
}
void adjust(void)
{   
    			  
	adjust3();
    adjust2();		  
	adjust1();
	  
}
 void main(void)
{  
   Initial();
   while(1)
   {   
    unsigned char Data=0;
	 if(khit())
	  {
        Data=keyOut();  
        if(Data==0xa)
           {
	        adjust_tag=1;
	        adjust();
	   	    adjust_tag=0;
	       }
	    else
		  keybufRp--;	
      }
   if( tag==1)//一秒到来的标志 把新的时间读入显示缓存区
     {    
       changemode();
	  tag=0;
     }
   }
}

⌨️ 快捷键说明

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