新闻中心

EEPW首页>嵌入式系统>设计应用> C51单片机矩阵键盘扫描去抖程序

C51单片机矩阵键盘扫描去抖程序

作者: 时间:2011-10-27 来源:网络 收藏
void Init_Key()

本文引用地址://m.amcfsurvey.com/article/172438.htm

  {

  P5 = 0; //行线全数置为0

  EX1 = 1; // 允许外部钟表秒间断

  IT1 = 1; // 外部钟表间断配备布置为边缘触发

  }

  void Key_Int() interrupt 2

  {

  Key_Pressed = 1;

  EX1 = 0;

  }

  void Scan_Key()

  {

  unsigned char temp,rowvalue;

  unsigned int key;

  int i;

  temp = P2;

  temp = 0x3C;

  if(temp == 0x3C)

  {

  Key_Released = 0;

  Key_Pressed = 0;

  key = 灭茬_KEY;

  EX1 = 1;

  }

  else

  {

  key = temp;

  key = key8;

  rowvalue = 0x01;

  for(i=0;i5;i )

  {

  P5 = rowvalue

  DelayMs⑴;

  temp = P2;

  temp = 0x3C;

  if(temp == 0x3c)

  {

  rowvalue = rowvalue

  key = key | rowvalue;

  P5 = 0x00;

  break;

  }

  }

  P5 = 0x00;

  DelayMs⑴;

  }

  if(key!=灭茬_KEY) //如果有键按下

  { if(key==Key_Value) //如果按下的是不异的键

  {

  if(Keypress_Count>=KEY_DELAY)

  {

  Key_Down = 1;

  }

  }

  else if(Key_Down != 1)

  {

  Keypress_Count=0;

  Keyrelease_Count = 0;

  Key_Value=key;

  }

  }

  else //如果无键按下

  {

  if(Key_Down) //如果时下是键开释,返回键值

  {

  if(Keyrelease_Count >= KEY_DELAY)

  {

  Key_Down=0;

  Keypress_Count=0;

  Keyrelease_Count=0;

  Key_Released = 1;

  EX1 = 1;

  return;

  }

  }

  else

  {

  Keypress_Count=0;

  Keyrelease_Count=0;

  Key_Value = 灭茬_KEY;

  EX1 = 1;

  return;

  }

  }

  }

  在main.c中的挪用要领为

  if(Key_Pressed == 1)

  {

  //Key_Pressed = 0;

  Scan_Key();

  }

  if(Key_Released == 1)

  {

  Key_Released = 0;

  Ack_Key();

  }

DIY机械键盘相关社区:机械键盘DIY



上一页 1 2 下一页

评论


相关推荐

技术专区

关闭