標題: 一個經(jīng)典的按鍵掃描加按鍵消抖的程序 [打印本頁]

作者: 劉錦輝    時間: 2020-9-15 22:07
標題: 一個經(jīng)典的按鍵掃描加按鍵消抖的程序
先寫了一個按鍵消抖函數(shù)然后將兩種狀態(tài)存為兩種變量,0代表松開,1代表按下按鍵。
f(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin)==GPIO_PIN_SET)這個函數(shù)是讀取引腳電平的一個函數(shù),返回值為0或1.

后面函數(shù)是一個外部中斷函數(shù),根據(jù)第一個函數(shù)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.torrancerestoration.com/bbs/) Powered by Discuz! X3.1