日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]C51單片機(jī)矩陣鍵盤電子掃描去抖程序  這段有1個(gè)C51的項(xiàng)目,用的是新華龍的C51 F020單片機(jī)。項(xiàng)目中要使成為事實(shí)4*5的矩陣鍵盤。矩陣電路圖如次如示     此中,四條列線接在 F020的P2~P5口線上,5條行線接在P5口

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

  這段有1個(gè)C51的項(xiàng)目,用的是新華龍的C51 F020單片機(jī)。項(xiàng)目中要使成為事實(shí)4*5的矩陣鍵盤。矩陣電路圖如次如示

  

 

  此中,四條列線接在 F020的P2~P5口線上,5條行線接在P5口線上(F020的P5口是差別于平凡C51的擴(kuò)大接口,不克不及位尋址)。同時(shí)4條列線接在一四輸入與非門(74LS20)上,門輸出接F020的外間斷1,如許,不論什么一鍵按下,都會(huì)孕育發(fā)生間斷,報(bào)信程序舉行鍵盤電子掃描。

  托1個(gè)新手給寫了鍵盤的電子掃描程序,基本功效都能使成為事實(shí),但對(duì)鍵盤的去抖措置懲罰老是做欠好,體現(xiàn)是或不克不及去抖,或按鈕相應(yīng)太卡,或采集到紕繆鍵值??磥?lái)新手對(duì)矩陣鍵盤電子掃描原理掌握較好(網(wǎng)上資料多),但對(duì)鍵盤去抖的知識(shí)卻有所欠缺,基本都是按照書上說(shuō)的延時(shí)一段時(shí)間再采集鍵值,現(xiàn)實(shí)應(yīng)用中,如許的措置懲罰是遠(yuǎn)遠(yuǎn)不敷的,過于簡(jiǎn)單?,F(xiàn)實(shí)去抖措置懲罰應(yīng)該如許舉行更合理一些,即連續(xù)采集鍵值,當(dāng)采集到的鍵值在一段時(shí)間內(nèi)是不異的,即以為按鈕狀況已經(jīng)穩(wěn)定,此鍵值為真實(shí)鍵值。別的,按鈕開釋時(shí),也會(huì)有抖動(dòng),導(dǎo)致誤采鍵值,是以在鍵開釋時(shí),也應(yīng)舉行去抖措置懲罰,措置懲罰要領(lǐng)同時(shí)是連續(xù)一段時(shí)間采集到無(wú)鍵按下狀況,才以為按鈕被開釋。按照這個(gè)要領(lǐng),我重寫了新手的程序,現(xiàn)實(shí)應(yīng)用中體現(xiàn)極好。

  現(xiàn)將程序發(fā)布如次,供新手參考。

  Key.h文件內(nèi)容

  #ifndef __key_H__

  #define __key_H__

  #define 滅茬_KEY 0x0000

  #define S1 0x3801

  #define S2 0x3401

  #define S3 0x3802

  #define S4 0x3402

  #define S5 0x3804

  #define S6 0x3404

  #define S7 0x3808

  #define S8 0x3408

  #define S9 0x3810

  #define S10 0x3410

  #define S11 0x2C01

  #define S12 0x1C01

  #define S13 0x2C02

  #define S14 0x1C02

  #define S15 0x2C04

  #define S16 0x1C04

  #define S17 0x2C08

  #define S18 0x1C08

  #define S19 0x2C10

  #define S20 0x1C10

  #define KEY_DELAY 20

  extern unsigned int Key_Value;

  extern void Init_Key();

  extern void Scan_Key();

  extern bit Key_Pressed;

  extern bit Key_Released;

  extern unsigned int idata Keypress_Count;

  extern unsigned int idata Keyrelease_Count;

  #endif

  key.c 文件內(nèi)容

  #include

  #include "key.h"

  bit Key_Down; //是不是有鍵按下的標(biāo)記

  unsigned int idata Keypress_Count;

  sbit Col_Key0 = P2^2;

  sbit Col_Key1 = P2^3;

  sbit Col_Key2 = P2^4;

  sbit Col_Key3 = P2^5;

  bit Key_Pressed;

  bit Key_Released;

  unsigned int Key_Value; bit Key_Down; //是不是有鍵按下的標(biāo)記

  unsigned int idata Keypress_Count; //一毫秒增加一次的變量

  unsigned int idata Keyrelease_Count; //一毫秒增加一次的變量

  //矩陣鍵盤施用間斷1作為鍵盤間斷void Init_Key()

 

  {

  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 = key<<8;

  rowvalue = 0x01;

  for(i=0;i<5;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í)下是鍵開釋,返回鍵值

  {

  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();

  }

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉