先寫了一個按鍵消抖函數然后將兩種狀態存為兩種變量,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 );
}
|