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

📄 main.c

📁 这是用KEIL C 搞的AT89C51用的16键盘、实时时钟显示以及8个发光二极管8种键控流水灯显示程序。引用了AT89C51的两个TIMER,程序很好用。
💻 C
字号:
//由于在调试键盘程序出现了不理解的现象,有些词句不能正确的执行,比如:getboard=0;本来是个很明显的付值语句,
//可是用单步运行是可以清楚的看到程序根本就不执行这步.
//最后我把扫描送值的方法改了,不是按书上的送法,而是列送0F,
//下次时行送F0.才解决了问题,这个问题现在都还不知道,为什么不会执行呢?

 #include "reg51.h"

 #define unint unsigned int
 #define unchar unsigned char
  
unchar show[11]={0x3f,0x06,0x5b,0x4f,
                 0x66,0x6d,0x7d,0x07,
                 0x7f,0x6f,0x77};
 unchar *minute0=show;
 unchar *minute1=show;
 unchar *second0=show;
 unchar *second1=show;
 unchar conut=0;//TIMER1中计数
 count1=0;//TIMER0中计数
 unchar getkey=0;//取到扫描键值
 unchar s_getkey=0;//第二次取键值
 unchar m=1,n=1;//用于是否调出循环
 unchar count3=0;

 void DelayLBD();//二极管延时
 void Delay_ms(unchar i);//延时i毫秒
 void Key_Function();//执行功能键
 unchar ScanKey();//键盘扫描

////////////////////////
////////MAIN////////////
 main()
 {  
   TMOD=0x11;
     TH0=0x3c;
    TL0=0xaf;
    TH1=0xfe;
    TL1=0x0b;
    EA=1;
    ET0=1;
    ET1=1;
    TR0=1;
    TR1=1;  
 
  while(1)
  { 
    Key_Function();
   }
 }

/////////////////////////////////////
///////////执行按键功能///////////////
/////////////////////////////////////
void Key_Function()
{
  unchar idataa,idataa0;
  unchar idataa1;
  getkey=s_getkey;//变量在TIMER0中
  s_getkey=0;
if(getkey)
{
   switch(getkey)
   {
 
     case 0xee: n=m;
     getkey=0;
         //==LeftShift()==//
         while(n)
         {
           idataa=0xfe;
           P1=idataa;
            DelayLBD();
            while(idataa&0x80)//只判断最     
           {                  //高为即可
             if(n==0)
               break;             
             idataa=(idataa<<1)|0x01;
             P1=idataa;
             DelayLBD();
           }
          }

         break;
     case 0xed:  n=m;
     getkey=0;
        //== RightShift==//
        while(n)
        {
           idataa=0x7f;
           P1=idataa;
            DelayLBD();
           while(idataa&0x01)//只判断最
           {                 // 低为即可
             if(n==0)
               break;
             idataa=(idataa>>1)|0x80;
             P1=idataa;
             DelayLBD();
           }
          }
         break;
     case 0xeb:    n=m;
     getkey=0;
         //==LRShift()==//;
        while(n)
        {
           idataa=0xfe;
           P1=idataa;
           while(idataa&0x80)//只判断最
           {                 //高为即可
             if(n==0)
               break;
             idataa=(idataa<<1)|0x01;
             P1=idataa;
             DelayLBD();
           }
           while(idataa&0x01)
           {                   
             if(n==0)
               break;
             idataa=(idataa>>1)|0x80;
             P1=idataa;
             DelayLBD();
           }
        }
         
         break;
      case 0xe7:      n=m;
      getkey=0;
        //== RLShift()==//
        while(n)
        {
           idataa=0x7f;
           P1=idataa;
           while(idataa&0x01)//只判断最
           {                 //高为即可
             if(n==0)
              break;
             idataa=(idataa>>1)|0x80;
             P1=idataa;
             DelayLBD();
           }
           while(idataa&0x80)
           {
             if(n==0)
               break;
             idataa=(idataa<<1)|0x01;
             P1=idataa;
             DelayLBD();
           }
        }
         break;
         //////////////
         //////////////
      case 0xde: n=m;
      getkey=0;
         //==MiddleSide()==//     
         while(n)
         {
           idataa=0xe7;
           P1=idataa;
           DelayLBD();
           while(idataa&0x81)
           {
             if(n==0)
              break;
             idataa0=(idataa<<1)|0x1f;
             idataa1=(idataa>>1)|0xf8;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
          }
         break;
      case 0xdd: n=m;
      getkey=0;
         //==SideMiddle()==//      
         while(n)
         {
           idataa=0x7e;
           P1=idataa;
           DelayLBD();
           while(idataa&0x18)
           {
             if(n==0)
               break;
             idataa0=(idataa<<1)|0xf1;
             idataa1=(idataa>>1)|0x8f;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
          }
         break;
      case 0xdb:   n=m;
      getkey=0;
          //==MSSM()==//     
         while(n)
         {
           idataa=0xe7;
           P1=idataa;
           DelayLBD();
           while(idataa&0x81)
           {
             if(n==0)
               break;
             idataa0=(idataa<<1)|0x1f;
             idataa1=(idataa>>1)|0xf8;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
           while(idataa&0x18)
           {
             if(n==0)
               break;
             idataa0=(idataa<<1)|0xf1;
             idataa1=(idataa>>1)|0x8f;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
          }
          break;
      case 0xd7:  n=m;     
          //==SMMS()==//
          getkey=0;
         while(n)
         {
           idataa=0x7e;
           P1=idataa;
           DelayLBD();
           while(idataa&0x18)
           {
             if(n==0)
               break;
             idataa0=(idataa<<1)|0xf1;
             idataa1=(idataa>>1)|0x8f;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
            while(idataa&0x81)
           {
             break;
             idataa0=(idataa<<1)|0x1f;
             idataa1=(idataa>>1)|0xf8;
             idataa=idataa0&idataa1;
             P1=idataa;
             DelayLBD();
            }
          }
          break;
          /////////////
          /////////////
       case 0xbe:
          minute0++;
          getkey=0;
          Delay_ms(1);
           break;
       case 0xbd:
           minute0--;
           getkey=0;  
           if(minute0<show) minute0=show+5; 
           Delay_ms(1);
           break;
       case 0xbb:
            minute1++;
           getkey=0;
            Delay_ms(1);
            break;
       case 0xb7:
            minute1--;
            getkey=0;
            if(minute1<show) minute1=show+9; 
            Delay_ms(1);
            break;
         ////////////////
         //////////////// 
       case 0x7e:
            second0++;
            getkey=0;
            Delay_ms(1);
            break;
       case 0x7d:
             second0--;
             getkey=0;
             if(second0<show) second0=show+5; 
            Delay_ms(1);
             break;
       case 0x7b:
             second1++;
             Delay_ms(1);
             getkey=0;
             break;
       case 0x77:
             second1--;
             getkey=0;
              if(second1<show) second1=show+9;
             Delay_ms(1);

             break;
       default: 
          Delay_ms(2);
          break; 
    
   }  
   } 
 else  
   Delay_ms(3);

}
/////////////////
/*unchar LEDCODE[16]={0xee,0xde,0xbe,0x7e,
                    0xed,0xdd,0xbd,0x7d,
                    0xeb,0xdb,0xbb,0x7b,
                    0xe7,0xd7,0xb7,0x77};
*/
//////////////////////////
/////==二极管延时==///////
void DelayLBD()
{
  unchar j;
  unchar i=200;
  for(;i>0;i--)
    for(j=0;j<=100;j++)
     {
       if(s_getkey!=getkey) //新键值如果与旧值不同 
       {                    // 则证明有键按下退出
         n=0;               // 并执行下一按键功能
         getkey=s_getkey;
         break;
       }
      }
  
}
  ////////////////////////////////
  /////延时ims程序////////////////
void Delay_ms(unchar i)
{
  unint j;
  for(;i>0;i--)
    for(j=0;j<125;j++)
      if(s_getkey!=getkey)
        break;
}
//======================================//
/////TIMER0调用键盘与定时1S变LED段控值////
void Timer0() interrupt 1 
{
  unchar getboard;
  
  TH0=0x3c;//-50000/250;
  TL0=0xaf;//-50000%250;
  conut++;  
  count1++;
  if(count1>=3)
  {
   getboard=ScanKey();
   if(getboard)
     s_getkey=getboard;
     count1=0;
  }
    
  if(conut>20)
  {
   conut=0;
   second1+=1;
  } 
if(second1>=show+10)
  {
   second1=show;
   second0+=1;
  }
if(second0>=show+6)
  {
    second0=show;
    minute1+=1;
  }
 if(minute1>=show+10)
   { 
    minute1=show;	 
    minute0+=1;
   }
if(minute0>=show+6)
    minute0=show;
}  
///////=========///////////////-----------------------------
///////键盘扫描//////////////// 此子程序在最初调试时出现几处该执行却没有去执行的情况,比如说IF语句
unchar ScanKey(void)         // 本来t不等于0x0f的(变量窗口中可以看到),可是就是不执行Delay_ms()语句了,
{                            //而调到第二个if语句中的return(0);还有就是如recode=t;这种很明显的付值语句,
  unchar recode=0,sccode=0;  // 就算是执行了,record的值仍然保持原样
  unchar t=0,w=0;
  P3=0x0f;
  t=P3&0x0f; //高4位送全0码确读入低4位值
  if(t==0x0f)//低4位送1,下次可正
   return(0);
  Delay_ms(90);
  P3=0x0f;
  t=P3&0x0f;
  if(t==0x0f) 
 return(0); 
  P3=0x0f;
   t=P3&0x0f; //P3为输入不能要
  recode=t;   //高4位,只能要4低位

  P3=0xf0;
  sccode=P3&0xf0;
  return(sccode|recode); 
}
   
//////TIMER1刷新一个LED/////
void Timer1() interrupt 3  
{
  
 count3++;
  TH1=0xfe;
  TL1=0x0b;
  switch(count3)
  {
    case 1:
          P2=0xfc;
          P0=*minute0;
          break;
    case 2:
          P2=0xf3;   
          P0=*minute1;
          break;
    case 3:
          P2=0xcf;
          P0=*second0;
          break;
    case 4:

          P2=0x3f;
          P0=*second1;
         count3=0;
          break;
  }

}
  

⌨️ 快捷键说明

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