專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

單片機(jī)矩陣鍵盤的控制

作者:李俊軒   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年07月18日   【字體:

本來感覺矩陣鍵盤很簡(jiǎn)單的,沒有想到編了一個(gè)程序,編了一個(gè)下午,哎,難怪有人說,如果技術(shù)和科學(xué)有你理想那么容易,那它就不是科學(xué)了。在參考http://www.torrancerestoration.com/mcuteach/226.html 后終于把矩陣鍵盤里的16個(gè)按鍵控制數(shù)碼管顯示0至16的效果實(shí)現(xiàn)了出來,呵呵,功夫不負(fù)有心人,雖然編了好久,好累,好困。但還是一個(gè)字,值!下面看下程序吧,如果各位高手看到了我程序有所不足的,麻煩指點(diǎn)。

下面是我的程序:#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];

  }
  
 }
 }
}
感覺好長(zhǎng),如果厲害點(diǎn)的話,應(yīng)該可以做編得更短的,有時(shí)間只有再把它完善了。

關(guān)閉窗口

相關(guān)文章