機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
支持鍵盤雙擊事件的C程序設(shè)計(jì)
支持鍵盤雙擊事件的C程序設(shè)計(jì)
 更新時(shí)間:2008-7-27 15:59:33  點(diǎn)擊數(shù):4
【字體: 字體顏色

來源:mcu99.com      作者:李佰戰(zhàn)

/**********************************************************************************
         KeyBoard Encode Program
         This Program can encode press_key up to 128 and it can deal KB_DBClick Message
         This is just a test proram and only use 2 key.If More Key need to Encode ,please
      modify the function:KBKeyPress()
         This Porgram use Message_Driver method,the KB_Msg is used to record what KB_Msg has occured.
         The meaning of 'SysKBMsg' is list as following.
        
          Program Design:LiBaizhan
                     Ver:1.10
                    Date:2003-3-16
************************************************************************************/

#i nclude <reg51.h>
#i nclude <intrins.h>

sbit  Key1                =           P1^0;
sbit  Key2                =           P1^1;

/*
    Some System Var Pre_Definition
    Crystal Frequence is 11.0592MHz
*/
#define        TIME_2MS          0X74
#define        TIME_20MS         0X043B

#define        KB_INTERNAL       3        /*Key DBClk Detection Internal */

/*
   SysKBMsg define Keyboard Message,it include Single_Click or Double_Click
   It's bit6~bit0 record key_encode and bit 7 record DBClk(1) or SglClk(0)
   It can record and encode up to 127(2^7-1) keys
   No key is press when it is 0
   This method did not deal key such as Ctrl_Key or Alt_Key or Shift_Key...etc.
*/
                                 /*_________________________________________________*/
data unsigned char SysKBMsg=0;   /*|  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |*/
                                 /*| D/S | KB6 | KB5 | KB4 | KB3 | KB2 | KB1 | KB0 |*/
data unsigned char KBCounter=0;

bit  KBCounterStart=0;
bit  KBCounterStop=0;            /*if KeyBoard counter stop then this bit is 1*/
bit  KBCounterFlag=0;            /*Current Counter is used by KeyBoard*/

void TimerInt0(void) interrupt 1       /*timer 0 interrupt use to record how long key is press*/
{
   TR0=0;
   if(KBCounterFlag)             /*Current Counter is used by KeyBoard*/
   {
      if(KBCounter<KB_INTERNAL)  /*if DBClk Detection is not finish then detect continue*/
      {
         KBCounter++;
         TR0=1;
      }
      else
      {
         KBCounter=0;            /*DBClk Detection is finish*/
         KBCounterStop=1;
      }
   }
}


         void DelayMS(unsigned int dltime);
         void Init51(void);
unsigned char KBKeyPress(void);        /*only return what key is press*/
         void KBKeyEncode(void);       /*encode which key is pressed and DBClk or SglClk*/
         void KBStartTimer(unsigned char CntH,unsigned char CntL);  /*load counter initial value and start timer*/
         void KBStopTimer(void);
         void KBDealPressKey(void);    /*deal key_press message*/

void main(void)
{
   Init51();
   while(1)
   {
      KBKeyEncode();                /*recored what KeyBoard Msg occure:which key is press and single clk or db clk*/
      KBDealPressKey();
   }
}

/*
      Delay Time is :(20+17*dl_time)*12/CrystalFrequence(us)
*/
void DelayMS(unsigned int dltime)
{
   unsigned int i;
   for(i=0;i<dltime;i++);
}
void Init51(void)
{
   SCON  = 0x50;                      /* mode 1: 8-bit UART, enable receiver     */
   TMOD  = 0x21;                      /* timer 1 mode 2: 8-Bit reload            */
                                      /* timer 0 mode 1: 16-bit Timer            */
   TH1   = BAUD_4800;                 /* reload value 9600 baud                  */
   TR1   = 1;                         /* timer 1 run                             */
   IE    = 0X12;                      /* enable Serial INT,Timer0 INT            */
   ES    = 0;                         /* disable Serial INT*/
   EA    = 1;                         /* Open INT Ctrl                           */
}

void KBKeyEncode(void)
{
   data unsigned char CurrPress=0,LastPress=0;
   if((LastPress=KBKeyPress())!=0)              /*if some key is press then start encode*/
   {
      KBStartTimer(0,0);                        /*some key is press then start DBClk Detection Counter*/
      SysKBMsg=LastPress;                      /*record the key that is pressed this time*/
      while(!KBCounterStop)
      {
         if((CurrPress=KBKeyPress())!=0X0)      /*if some key is pressed during DBClk Detection then jump out to see wether DBclk is occured*/
            break;
      }
      if((KBCounterStop==0)&&(LastPress==CurrPress))            /*this key DBClk occured*/
         SysKBMsg|=0X80;
      KBStopTimer();
   }
}

unsigned char KBKeyPress(void)
{
   data unsigned char KeyPress=0;
   if((P1&0X03)!=0X03)
   {
      DelayMS(TIME_20MS);
      if((KeyPress=(P1&0X03))!=0X03)         /*some key is press*/
      {
         if(KBCounterStart)
            TR0=0;
         while((P1&0X03)!=0X03);             /*wait until key is free*/
         DelayMS(TIME_20MS);
         if(KBCounterStart)
            TR0=1;
      }
      else              /*Key is not real press*/
      {
         KeyPress=0;
      }
   }
   return KeyPress;
}

void KBStartTimer(unsigned char CntH,unsigned char CntL)
{
   TR0=0;
   TH0=CntH;
   TR0=1;                           /*Start Counter*/
   TL0=CntL;
   KBCounterFlag=1;                 /*this counter is used by KeyBoard*/
   KBCounterStart=1;
   KBCounterStop=0;
}

void KBStopTimer(void)
{
   TR0=0;
   TH0=0;
   TL0=0;
   KBCounter=0;
   KBCounterFlag=0;
   KBCounterStart=0;
}

void KBDealPressKey(void)
{
   data unsigned char tmp=0;
   switch(SysKBMsg)        /*here is just a test program,test to deal Sgl_Clk and DB_Clk Msg*/
   {
      case  0X01:       tmp=0X01;break;
      case  0X02:       tmp=0X02;break;
      case  0X81:       tmp=0X81;break;
      case  0X82:       tmp=0X82;break;
      default   :       break;
   }
   SysKBMsg=0;                                 /*this key msg has been done*/

 
  • 上一篇: MSP430 按鍵程序范例(附原理圖)
  • 下一篇: M50462遙控器解碼程序
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1