getappple的笔记 https://bbs.21ic.com/?514800 [收藏] [复制] [RSS] 模数稍通\单片机初学\ARM略懂\很傻很天真\很笨很努力

日志

51小程序

已有 857 次阅读2008-5-18 12:52 |系统分类:单片机| 初学51, 键扫, P0/P1

#include "reg52.h"
//#define uchar unsigned char
//#define uint unsigned int
#include <intrins.h>


void delayF(unsigned int  i)
 { unsigned char  j;
  for(;i;i--)
   for(j=0;j<124;j++)
    {;}
    
 }


unsigned char keyscanF(void)
 { 
   unsigned char  kytp;
   unsigned char  kytp1;
   //kytp=0x00; kytp1=0x00;
   unsigned char pp1;
   unsigned char  k,m;
   //pp1=0x00;
   //k=0x00;
   //m=0x00;
   unsigned char a[4]={0xef,0xdf,0xbf,0x7f};//定义输出值
   for(k=0;k<4;k++)
   {
    P1=a[k];
    kytp=P1;
    delayF(10);
    kytp1=P1;
    if (kytp==kytp1)
     {     
      if (kytp1!=a[k])
       {       
        pp1=kytp;
        m=1;
        break;      //有按下,退出循环
       }
      else pp1=0x00;
     }
    
   }
   if (m==1)  {return(pp1);}
   else {m=0;return(m);}
 }


void main()
 { 
  while(1)
   { unsigned char  h;
    h=keyscanF();
    switch (h)
     {     
      case 0 : break;
      case 0xee :
       {
        P0=0xfe;
        //unsigned char i;
            delayF(500);       
          while (keyscanF()==0)
          {delayF(1000);P0=_crol_(P0,1);delayF(1000);}
        
        
         break;
       }
      case 0xdd :
       {
        P0=0x7f;
        delayF(500);
         while (keyscanF()==0)
          {delayF(1000);P0=_cror_(P0,1);delayF(1000);}
        break;
        }
         
      case 0xbb :
       {P0=0x00;break;}
      default : 
       {P0=0xff;break;}
     }
   }
 }


路过

鸡蛋

鲜花

握手

雷人

发表评论 评论 (1 个评论)

访客Gkb4ER 2008-5-24 16:09
PS 按键扫描最好采用定时中断扫描