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

矩陣鍵盤簡單使用

作者:huqin   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年04月08日   【字體:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SER = P2^0;
sbit SRCLK = P2^1;
sbit SRCLR = P2^2;
sbit RRCLK =  P2^3;
sbit E = P2^4;
uchar table595[16]={0x30,0x7e,0x29,0x2c,0x66,0xa4,0xa0,0x3e,0x20,0x24,0x22,0xe0,0xb1,0x68,0xa1,0xa3};
void delay(uchar z)
{
 uchar x,y;
 for(x=0;x<110;x++)
  for(y=0;y<z;y++);
}
uchar keyscan()//矩陣鍵盤掃描程序
{
 uchar num,temp;
 while(1)
 {
  P3=0xfe;
  temp=P3;
  temp=temp&0xf0;
  while(temp!=0xf0)
  {
   delay(5);
   temp=P3;
   temp=temp&0xf0;
   while(temp!=0xf0)
   {
    temp=P3;
    switch(temp)
    {
     case 0xee:num=0;
     break;
     case 0xde:num=1;
     break;
     case 0xbe:num=2;
     break;
     case 0x7e:num=3;
     break;
    }
    while(temp!=0xf0)
    {
     temp=P3;
     temp=temp&0xf0;
    }
    return num;
   }
  }
  P3=0xfd;
  temp=P3;
  temp=temp&0xf0;
  while(temp!=0xf0)
  {
   delay(5);
   temp=P3;
   temp=temp&0xf0;
   while(temp!=0xf0)
   {
    temp=P3;
    switch(temp)
    {
     case 0xed:num=4;
     break;
     case 0xdd:num=5;
     break;
     case 0xbd:num=6;
     break;
     case 0x7d:num=7;
     break;
    }
    while(temp!=0xf0)
    {
     temp=P3;
     temp=temp&0xf0;
    }
    return num;
   }
  }
  P3=0xfb;
  temp=P3;
  temp=temp&0xf0;
  while(temp!=0xf0)
  {
   delay(5);
   temp=P3;
   temp=temp&0xf0;
   while(temp!=0xf0)
   {
    temp=P3;
    switch(temp)
    {
     case 0xeb:num=8;
     break;
     case 0xdb:num=9;
     break;
     case 0xbb:num=10;
     break;
     case 0x7b:num=11;
     break;
    }
    while(temp!=0xf0)
    {
     temp=P3;
     temp=temp&0xf0;
    }
    return num;
   }
  }
  P3=0xf7;
  temp=P3;
  temp=temp&0xf0;
  while(temp!=0xf0)
  {
   delay(5);
   temp=P3;
   temp=temp&0xf0;
   while(temp!=0xf0)
   {
    temp=P3;
    switch(temp)
    {
     case 0xe7:num=12;
     break;
     case 0xd7:num=13;
     break;
     case 0xb7:num=14;
     break;
     case 0x77:num=15;
     break;
    }
    while(temp!=0xf0)
    {
     temp=P3;
     temp=temp&0xf0;
    }
    return num;
   }
  }
 }
}
void X74HC595(uchar j)
{
 uchar i,temp;
 E = 1;
 _nop_();
 _nop_();
 E = 0;
 SRCLR = 1;
 temp=table595[j];
 for(i=0;i<8;i++)
 {
  temp=_crol_(temp,1);
  SER = temp & 0x80; //p2.0連J9的1口
  SRCLK = 0;
  _nop_();
  SRCLK = 1; 
  _nop_();
  RRCLK= 0; 
  _nop_();
  RRCLK = 1;
  _nop_(); 
 }
}
void main()
{
 uchar p;
 while(1)
 {
  p=keyscan();
  X74HC595(p);
 }
}
關(guān)閉窗口

相關(guān)文章