3x4矩阵键盘的扫描程序(C语言)
发起人:admin1  回复数:0  浏览数:4013  最后更新:2009/10/30 13:48:38 by admin1

发表新帖  帖子排序:
2009/10/30 13:48:39
admin1





角  色:普通会员
发 帖 数:13
注册时间:2009/10/28
3x4矩阵键盘的扫描程序(C语言)
3x4矩阵键盘的扫描程序(C语言)

按相应的按键,数码管显示相应的数字,星号键和井号键分别显示为E和F


#include<AT89X51.h>


unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99, //0~4
      0x92,0x82,0xF8,0x80,0x90, //5~9
      0x88,0x83,0xA7,0xA1,0x86,0x8E}; //A~F

void KeyScan();
void delay10ms(unsigned char time);
void Dispaly(unsigned char k);

unsigned char key,temp;

void main() //主程序
{
while(1)
{
  KeyScan();
}
}

void KeyScan() //按键扫描子程序
{
P1=0xFF;
P1_3=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
  temp=P1;
  temp&=0xF0;

  switch(temp)
  {
  case 0x70:
    key=1;break;
  case 0xB0:
    key=2;break;
  case 0xD0:
    key=3;break;
  }
  Dispaly(key);
  }
}

P1=0xFF;
P1_2=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
  temp=P1;
  temp&=0xF0;

  switch(temp)
  {
  case 0x70:
    key=4;break;
  case 0xB0:
    key=5;break;
  case 0xD0:
    key=6;break;
  }
  Dispaly(key);
  }
}

P1=0xFF;
P1_1=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
  temp=P1;
  temp&=0xF0;

  switch(temp)
  {
  case 0x70:
    key=7;break;
  case 0xB0:
    key=8;break;
  case 0xD0:
    key=9;break;
  }
  Dispaly(key);
  }
}

P1=0xFF;
P1_0=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
  temp=P1;
  temp&=0xF0;

  switch(temp)
  {
  case 0x70:
    key=14;break;
  case 0xB0:
    key=0;break;
  case 0xD0:
    key=15;break;
  }
  Dispaly(key);
  }
}
}

//延时程序
void delay10ms(unsigned char time)
{
unsigned char a,b,c;
for(a=0;a<time;a++)
  for(b=0;b<10;b++)
  for(c=0;c<120;c++)
  ;
}

void Dispaly(unsigned char k) //显示程序
{
P0=table[k];
P2_1=0;
}