《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

支持键盘双击事件的C程序设计
程序匠人 发表于 2005-8-17 17:55:00  阅读全文 | 回复(0) | 引用通告 | 编辑

      支持键盘双击事件的C程序设计!

      作者:李佰战 
      /**********************************************************************************
               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*/
      }

 

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!