标题: 一个经典的按键扫描加按键消抖的程序 [打印本页]

作者: 刘锦辉    时间: 2020-9-15 22:07
标题: 一个经典的按键扫描加按键消抖的程序
先写了一个按键消抖函数然后将两种状态存为两种变量,0代表松开,1代表按下按键。
f(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin)==GPIO_PIN_SET)这个函数是读取引脚电平的一个函数,返回值为0或1.

后面函数是一个外部中断函数,根据第一个函数return的值,做出选择。



uint16_t Key_Detection(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
                {
                        uint16_t Key_Value=0;
                        if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin)==GPIO_PIN_SET)
                        {
                                HAL_Delay(18);
                                if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin)==GPIO_PIN_SET)                        //yan shi xiao dou
                                {
                                        Key_Value=1;
                                }
                                //while(HAL_GPIO_ReadPin(Key_GPIO_Port,Key_Pin)==GPIO_PIN_RESET);                //song shou jian ce
                        }
                        return Key_Value;
                }


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{


        uint32_t K[6]={10,100,1000,10000,100000,1000000};
        
        if(GPIO_Pin ==Key3_Pin )
        {
                        if(Key_Detection(Key3_GPIO_Port ,GPIO_Pin)==1)
                        {a++;}
                }
        
        if(GPIO_Pin==Key2_Pin  )
        {if(Key_Detection(Key1_GPIO_Port ,GPIO_Pin)==1)
        {                HAL_GPIO_TogglePin (GPIOE,GPIO_PIN_5 );
                Freq =Freq+K[a%6];
                //Freq++;
        }
}
        if(GPIO_Pin==Key1_Pin )
        {if(Key_Detection(Key1_GPIO_Port ,GPIO_Pin)==1)
                {               
                        HAL_GPIO_TogglePin (GPIOE,GPIO_PIN_5 );
                        Freq =Freq-K[a%6];
                        //Freq--;
                }
        }
ad9851_init ();
ad9851_writefrq (Freq );
}






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1