微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 我的键盘扫描 方法一 但是有个小提问

我的键盘扫描 方法一 但是有个小提问

时间:10-02 整理:3721RD 点击:
在图中的那里问为什么不是红色?


程序::忘了。在这里:/br

  1. #include   <reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int

  4.    //子函数集
  5. void delay(int i)        //延时函数
  6. {
  7. while(i--);
  8. }

  9. uchar keyscanf(void)         //键盘调用函数
  10. {
  11.    uchar l,h;
  12.    P3=0xf0;
  13.    l=P3;
  14.    l=l&0xf0;
  15.    if (l!=0xf0)
  16.     {
  17.           delay(50);
  18.           if (l!=0xf0)
  19.             {
  20.                  l=P3&0xf0;       
  21.                  l=l|0x0f;
  22.                  P3=l;
  23.                  h=P3;
  24.                  h=h&0x0f;
  25.                  l=l&0xf0;
  26.                  return(h+l); //输出键盘值
  27.                 }
  28.            }
  29.         return(0xff);
  30. }

  31. void main ()         //主函数显示        用74ls48驱动7段共阴极数码管
  32. {
  33.    uchar key;
  34.    key=keyscanf();                 //调用键盘值

  35.    switch (key)
  36.       {          
  37.           case 0xee:P0=0x01;delay(20000);break;
  38.           case 0xde:P0=0x02;delay(20000);break;
  39.           case 0xbe:P0=0x03;delay(20000);break;
  40.           case 0x7e:P0=0x0c;delay(20000);break;
  41.           case 0xed:P0=0x04;delay(20000);break;
  42.           case 0xdd:P0=0x05;delay(20000);break;
  43.           case 0xbd:P0=0x06;delay(20000);break;
  44.           case 0x7d:P0=0x0d;delay(20000);break;

  45.           case 0xeb:P0=0x07;delay(20000);break;
  46.           case 0xdb:P0=0x08;delay(20000);break;
  47.           case 0xbb:P0=0x09;delay(20000);break;
  48.           case 0x7b:P0=0x0e;delay(20000);break;
  49.           case 0xe7:P0=0x0a;delay(20000);break;
  50.           case 0xd7:P0=0x00;delay(20000);break;
  51.           case 0xb7:P0=0x0b;delay(20000);break;
  52.           case 0x77:P0=0x0f;delay(20000);break;
  53.          
  54.           }



  55. }

复制代码

还有个复杂点的,,,,

  1. #include   <reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4.    
  5.    //子函数
  6. void delay(int i)        //延时函数
  7. {
  8. while(i--);
  9. }   
  10. uchar key(void)         
  11. {       
  12.    uchar h,numl;
  13.     h=P2;
  14.         while(1){
  15.            P2=0xfe;
  16.           {
  17.                 if(h!=0xfe)
  18.             delay(20);
  19.                 h=P2;
  20.             if(h!=0xfe)
  21.                    {
  22.                            switch(h)
  23.                           {
  24.                           case 0xee:numl=1;break;
  25.                           case 0xde:numl=2;break;
  26.                           case 0xbe:numl=3;break;
  27.                           case 0x7e:numl=4;break;
  28.                           }
  29.                           return(numl);
  30.                    }  
  31.                 }

  32.         P2=0xfd;
  33.           {       
  34.             h=P2;
  35.                 if(h!=0xfd)
  36.             delay(20);
  37.                 h=P2;
  38.             if(h!=0xfd)
  39.                    {
  40.                            switch(h)
  41.                           {
  42.                           case 0xed:numl=5;break;
  43.                           case 0xdd:numl=6;break;
  44.                           case 0xbd:numl=7;break;
  45.                           case 0x7d:numl=8;break;
  46.                           }
  47.                            return(numl);
  48.                    }
  49.                 }
  50.         P2=0xfb;
  51.           {       
  52.             h=P2;
  53.                 if(h!=0xfb)
  54.             delay(20);
  55.                 h=P2;
  56.             if(h!=0xfb)
  57.                    {
  58.                            switch(h)
  59.                           {
  60.                           case 0xeb:numl=9;break;
  61.                           case 0xdb:numl=10;break;
  62.                           case 0xbb:numl=11;break;
  63.                           case 0x7b:numl=12;break;
  64.                           }
  65.                           return(numl);
  66.                    }
  67.            }
  68.         P2=0xf7;
  69.       {       
  70.             h=P2;
  71.                 if(h!=0xf7)
  72.             delay(20);
  73.                 h=P2;
  74.             if(h!=0xf7)
  75.                    {
  76.                            switch(h)
  77.                           {
  78.                           case 0xe7:numl=13;break;
  79.                           case 0xd7:numl=14;break;
  80.                           case 0xb7:numl=15;break;
  81.                           case 0x77:numl=16;break;
  82.                           }
  83.                           return(numl);
  84.                    }
  85.                 }       
  86.         }

  87. }

  88. void main()
  89. {       
  90. uchar a;
  91.     a=key();
  92.     switch (a)
  93.       {          
  94.           case 1:P0=0x01;delay(20000);break;
  95.           case 2:P0=0x02;delay(20000);break;
  96.           case 3:P0=0x03;delay(20000);break;
  97.           case 4:P0=0x0c;delay(20000);break;
  98.           case 5:P0=0x04;delay(20000);break;
  99.           case 6:P0=0x05;delay(20000);break;
  100.           case 7:P0=0x06;delay(20000);break;
  101.           case 8:P0=0x0d;delay(20000);break;

  102.           case 9:P0=0x07;delay(20000);break;
  103.           case 10:P0=0x08;delay(20000);break;
  104.           case 11:P0=0x09;delay(20000);break;
  105.           case 12:P0=0x0e;delay(20000);break;
  106.           case 13:P0=0x0a;delay(20000);break;
  107.           case 14:P0=0x00;delay(20000);break;
  108.           case 15:P0=0x0b;delay(20000);break;
  109.           case 16:P0=0x0f;delay(20000);break;
  110.           }
  111. }

复制代码

尽量避免p0做IO口

return(0xff)什么意思

谢谢分享。

返回,,重新开始  里面的0xff没有多少用,,,但是里面必须要有个东西,,,不然不能编译,,我试了的。也可以不用这个用while也行

学习了

写的挺乱的,我感觉小编还是再想下程序运行的过程吧,,上面的程序,在逻辑上有点不通

运行了的,,,就是在仿真中运行,,,,都可以的,,,,,,

这貌似像郭天祥矩阵键盘操作思想,你可以去参考想下

程序有点多,有点乱!看不懂!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top