新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C51單片機(jī)矩陣鍵盤掃描去抖程序

C51單片機(jī)矩陣鍵盤掃描去抖程序

作者: 時(shí)間:2011-10-27 來(lái)源:網(wǎng)絡(luò) 收藏
void Init_Key()

本文引用地址:http://2s4d.com/article/172438.htm

  {

  P5 = 0; //行線全數(shù)置為0

  EX1 = 1; // 允許外部鐘表秒間斷

  IT1 = 1; // 外部鐘表間斷配備布置為邊緣觸發(fā)

  }

  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 //如果無(wú)鍵按下

  {

  if(Key_Down) //如果時(shí)下是鍵開(kāi)釋,返回鍵值

  {

  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中的挪用要領(lǐng)為

  if(Key_Pressed == 1)

  {

  //Key_Pressed = 0;

  Scan_Key();

  }

  if(Key_Released == 1)

  {

  Key_Released = 0;

  Ack_Key();

  }

DIY機(jī)械鍵盤相關(guān)社區(qū):機(jī)械鍵盤DIY



上一頁(yè) 1 2 下一頁(yè)

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉