专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

单片机矩阵键盘的控制

作者:李俊轩   来源:本站原创   点击数:  更新时间:2013年07月18日   【字体:

本来感觉矩阵键盘很简单的,没有想到编了一个程序,编了一个下午,哎,难怪有人说,如果技术和科学有你理想那么容易,那它就不是科学了。在参考http://www.51hei.com/mcuteach/226.html 后终于把矩阵键盘里的16个按键控制数码管显示0至16的效果实现了出来,呵呵,功夫不负有心人,虽然编了好久,好累,好困。但还是一个字,值!下面看下程序吧,如果各位高手看到了我程序有所不足的,麻烦指点。

下面是我的程序:#include<reg52.h>
unsigned char temp;
char x;
unsigned char number[]={     
0x3f,
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x6f,
0x77,
0x7c,
0x39,
0x5e,
0x79,
0x71
};        //¶¨ÒåÊý×é0ÖÁ15
void delay()
{
 int i,j;
 for(i=5;i<0;i--)
 for(j=120;j<0;j--);
}        //ÑÓʱ³ÌÐò 
void main()

 P1 = 0x00;     //³ÌÐò¿ªÊ¼Ê±ÊýÂë¹Ü²»ÏÔʾ
 while(1)
 {
 P3 = 0x7f;     //ʹ°´¼üS1ÖÁS4ÏÔʾ0ÖÁ3
 temp = P3;
 temp = temp & 0x0f;
 while(temp != 0x0f)
 {
  delay();
   temp = P3;
  temp = temp & 0x0f;
 
  while(temp != 0x0f)
  { 
   temp = P3;
   switch(temp)
   {
    case 0x7e:x=0;
     break;
    case 0x7d:x=1;
     break;
    case 0x7b:x=2;
     break;
    case 0x77:x=3;
     break;
   }
   while(temp!=0x0f)   //Ìø³ö
   {
    temp=P3;
    temp=temp&0x0f;
   }
    P1 = number[x];

  }
  
 }


//cond

 P3 = 0xbf;       //ʹ°´¼üS5ÖÁS8ÏÔʾ4ÖÁ7
 temp = P3;
 temp = temp & 0x0f;
 while(temp != 0x0f)
 {
  delay();
   temp = P3;
  temp = temp & 0x0f;
 
  while(temp != 0x0f)
  { 
   temp = P3;
   switch(temp)
   {
    case 0xbe:x=4;
     break;
    case 0xbd:x=5;
     break;
    case 0xbb:x=6;
     break;
    case 0xb7:x=7;
     break;
   }
   while(temp!=0x0f)  //Ìø³ö
   {
    temp=P3;
    temp=temp&0x0f;
   }
   
    P1 = number[x];
  }
  }

//thirth

P3 = 0xdf;       //ʹS9ÖÁS12ÏÔʾ8ÖÁb
 temp = P3;
 temp = temp & 0x0f;
 while(temp != 0x0f)
 {
  delay();
   temp = P3;
  temp = temp & 0x0f;
 
  while(temp != 0x0f)
  { 
   temp = P3;
   switch(temp)
   {
    case 0xde:x=8;
     break;
    case 0xdd:x=9;
     break;
    case 0xdb:x=10;
     break;
    case 0xd7:x=11;
     break;
   }
   while(temp!=0x0f)    //Ìø³ö
   {
    temp=P3;
    temp=temp&0x0f;
   }
   
    P1 = number[x];

  }
  
 }

//four
P3 = 0xef;         //ʹS13ÖÁS16ÏÔʾCÖÁF
 temp = P3;
 temp = temp & 0x0f;
 while(temp != 0x0f)
 {
  delay();
   temp = P3;
  temp = temp & 0x0f;
 
  while(temp != 0x0f)
  { 
   temp = P3;
   switch(temp)
   {
    case 0xee:x=12;
     break;
    case 0xed:x=13;
     break;
    case 0xeb:x=14;
     break;
    case 0xe7:x=15;
     break;
   }
   while(temp!= 0x0f)
   {
    temp = P3;
    temp = temp&0x0f;
   }
   while(temp!=0x0f)        //Ìø³ö
   {
    temp=P3;
    temp=temp&0x0f;
   }
    P1 = number[x];

  }
  
 }
 }
}
感觉好长,如果厉害点的话,应该可以做编得更短的,有时间只有再把它完善了。

关闭窗口

相关文章