美文网首页我爱编程
按键单击与双击

按键单击与双击

作者: 你的优先级最高 | 来源:发表于2018-05-31 19:00 被阅读118次
    • 按键的单击与双击与长按短按实现方式相近,以下代码仅供参考,写得不是很好
    • 适用于蓝桥杯开发板
    • 程序扩展性不强,更重要的是思路

    io.h

    #ifndef _IO_H
    #define _IO_H
    #include "stm32f10x.h"
    
    #define SingleClick 1
    #define DoubleClick 2
    
    #define KEY_B1 1
    #define KEY_B2 2
    #define KEY_B3 3
    #define KEY_B4 4
    
    #define TimeInterval 300 //判断双击与单击时间 
    
    #define keyState1 1
    #define keyState2 2
    #define keyState3 3
    #define keyState4 4
    
    //enum ss{keyState1,keyState2,keyState3,keyState4}keyState;
    
    #define KEY0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
    #define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
    #define KEY2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
    #define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)
    
    enum k{key0,key1,key2,key3};
    
    extern void Delay_Ms(u32 nTime);
    
    void RCC_Init(void);
    void IO_Init(void);
    void LED_Control(u16 nled , u8 flag);
    
    u8 KEY_Scan(void);
    void Key_Double(void);
    
    #endif
    
    

    io.c

    u8 KEY_Scan(void)//按键扫描函数,查找键值
    {
        static u8 flag = 1;
    
        if(flag&&(KEY0==0||KEY1==0||KEY2==0||KEY3==0))
        {
            Delay_Ms(10);
            flag = 0;
            if(KEY0==0)return key0;
            else if(KEY1==0)return key1;
            else if(KEY2==0)return key2;
            else if(KEY3==0)return key3;
        }
        else if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==1)flag = 1;
    
        return 0xff;
    }
    
    //单击双击
    /*
        1、两次间隔10ms - 300ms以内是双击, 按下第一次开始计时
        2、300ms 之后没有按下第二次就是单击
        3、思路与我按键单击双击的思维在方向上不太一样,这里是直接状态中判断按键
    */
    void Key_Double(void)
    {
        static u8 key_value = 0xff;
    //  static u16 keynum1 = 0,keynum2 = 0;
        static u8 key = 0; //记录第一次按键是谁按下
    
        switch(keyState)//keyState见宏定义,是按键第一次按下
        {
            case keyState1: key_value = KEY_Scan(); 
                            if(key_value != 0xff){
    //                          keynum1 = keyTime;
                                flag_1 = 1;
                                keyState = keyState2;
    
                                if(key_value==key0)key = 1;
                                else if(key_value==key1)key = 2;
                                else if(key_value==key2)key = 3;
                                else if(key_value==key3)key = 4;
                            }
                            else 
                            {
                                flag_1 = 0;
                                keyState = keyState1;
                            }           
                            break;
            case keyState2: key_value = KEY_Scan();
                            if(keyTime<300)
                            {
                                switch(key)
                                {
                                    case 1:if(key_value==key0){
                                                keyDouble = KEY_B1;flag_1 = 0;
                                                keyTime = 0;
                                                keyState = keyState1;
                                            }   break;
                                    case 2:if(key_value==key1){
                                                keyDouble = KEY_B2;flag_1 = 0;
                                                keyTime = 0;
                                                keyState = keyState1;
                                            }   break;
                                    case 3:if(key_value==key2){
                                                keyDouble = KEY_B3;flag_1 = 0;
                                                keyTime = 0;
                                                keyState = keyState1;
                                            }   
                                            break;
                                    case 4:if(key_value==key3){
                                                keyDouble = KEY_B4;flag_1 = 0;
                                                keyTime = 0;
                                                keyState = keyState1;
                                            }   
                                            break;
                                }   
                            }
                            else 
                            {
                                switch(key)
                                {
                                    case 1:keySingle = KEY_B1;      break;
                                    case 2:keySingle = KEY_B2;      break;
                                    case 3:keySingle = KEY_B3;      break;
                                    case 4:keySingle = KEY_B4;      break;
                                }
                                flag_1 = 0;
                                keyTime = 0;
                                keyState = keyState1;
                                key = 0;
                            }
                            break;
            case keyState3: 
                            break;
            case keyState4:                             
                            break;
            default:    break;
        }
    }
    

    相关文章

      网友评论

        本文标题:按键单击与双击

        本文链接:https://www.haomeiwen.com/subject/llpndftx.html