Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420115
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: C/C++

2014-04-27 12:19:39


点击(此处)折叠或打开

  1. //按下4X4键盘在数码管上依次显示0-F
  2. #include<reg52.h>

  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. sbit dula=P2^6;
  6. sbit wela=P2^7;
  7. uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  8. void delayms(uint xms)
  9. {
  10.     uint i,j;
  11.     for(i=xms;i>0;i--)
  12.         for(j=124;j>0;j--);
  13. }

  14. void display(uchar num)//只要送段选数据即可
  15. {
  16.     dula=1;
  17.     P0=table[num];
  18.     dula=0;
  19. }

  20. void matrixkeyscan()
  21. {
  22.     uchar temp,key;
  23.     /*****************判断第一行是否有键被按下***************************/
  24.     P3=0xfe;/*11111110B就是讲第一行线置低电平,其余行为高电平*/
  25.     temp=P3;
  26.     temp=temp&0xf0;/*判断高四位是否有0,temp的高四位就是矩阵键盘的列*/
  27.     if(temp!=0xf0)/*判断第一行是否有键被按下*/
  28.     {
  29.         delayms(10);
  30.         temp=P3;
  31.         temp=temp&0xf0;
  32.         if(temp!=0xf0)
  33.         {
  34.             temp=P3;
  35.             switch(temp)
  36.             {
  37.             case 0xee: key=0;
  38.                 break;
  39.             case 0xde: key=4;
  40.                 break;
  41.             case 0xbe: key=8;
  42.                 break;
  43.             case 0x7e: key=12;
  44.                 break;
  45.             }
  46.             while(temp!=0xf0)
  47.             {
  48.                 temp=P3;
  49.                 temp=temp&0xf0;
  50.             }
  51.             display(key);/*显示*/
  52.         }
  53.     }

  54.     /*****************判断第二行是否有键被按下***************************/
  55.     P3=0xfd;
  56.     temp=P3;
  57.     temp=temp&0xf0;
  58.     if(temp!=0xf0)
  59.     {
  60.         delayms(10);
  61.         temp=P3;
  62.         temp=temp&0xf0;
  63.         if(temp!=0xf0)
  64.         {
  65.             temp=P3;
  66.             switch(temp)
  67.             {
  68.             case 0xed: key=1;
  69.                 break;
  70.             case 0xdd: key=5;
  71.                 break;
  72.             case 0xbd: key=9;
  73.                 break;
  74.             case 0x7d: key=13;
  75.                 break;
  76.             }
  77.             while(temp!=0xf0)
  78.             {
  79.                 temp=P3;
  80.                 temp=temp&0xf0;
  81.             }
  82.             display(key);
  83.         }
  84.     }

  85.     /*****************判断第三行是否有键被按下***************************/
  86.     P3=0xfb;
  87.     temp=P3;
  88.     temp=temp&0xf0;
  89.     if(temp!=0xf0)
  90.     {
  91.         delayms(10);
  92.         temp=P3;
  93.         temp=temp&0xf0;
  94.         if(temp!=0xf0)
  95.         {
  96.             temp=P3;
  97.             switch(temp)
  98.             {
  99.             case 0xeb: key=2;
  100.                 break;
  101.             case 0xdb: key=6;
  102.                 break;
  103.             case 0xbb: key=10;
  104.                 break;
  105.             case 0x7b: key=14;
  106.                 break;
  107.             }
  108.             while(temp!=0xf0)
  109.             {
  110.                 temp=P3;
  111.                 temp=temp&0xf0;
  112.             }
  113.             display(key);
  114.         }
  115.     }

  116.     /*****************判断第四行是否有键被按下***************************/
  117.     P3=0xf7;
  118.     temp=P3;
  119.     temp=temp&0xf0;
  120.     if(temp!=0xf0)
  121.     {
  122.         delayms(10);
  123.         temp=P3;
  124.         temp=temp&0xf0;
  125.         if(temp!=0xf0)
  126.         {
  127.             temp=P3;
  128.             switch(temp)
  129.             {
  130.             case 0xe7: key=3;
  131.                 break;
  132.             case 0xd7: key=7;
  133.                 break;
  134.             case 0xb7: key=11;
  135.                 break;
  136.             case 0x77: key=15;
  137.                 break;
  138.             }
  139.             while(temp!=0xf0)
  140.             {
  141.                 temp=P3;
  142.                 temp=temp&0xf0;
  143.             }
  144.             display(key);
  145.         }
  146.     }

  147. }/*matrixkeyscan()函数结束*/

  148. void main()
  149. {
  150.     dula=1;
  151.     P0=0;
  152.     dula=0;
  153.     
  154.     wela=1;
  155.     P0=0xc0;
  156.     wela=0;

  157.     while(1)
  158.         matrixkeyscan();
  159. }

阅读(1618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~