按键初始化如下:
void Key_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能IO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource12);//初始化中断线
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource13);//初始化中断线
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource14);//初始化中断线
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource15);//初始化中断线
EXTI_ClearITPendingBit(EXTI_Line12);
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line14);
EXTI_ClearITPendingBit(EXTI_Line15);
EXTI_InitStructure.EXTI_Line = EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15; //配置中断线为中断线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //配置中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //配置为上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //配置中断线使能
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_12);
GPIO_ResetBits(GPIOD,GPIO_Pin_13);
GPIO_ResetBits(GPIOD,GPIO_Pin_14);
GPIO_ResetBits(GPIOD,GPIO_Pin_15);
}
PD8~PD11为键盘的列,PD12~PD15为键盘的行。
main.c如下,当检测到有按键按下,则开始扫描,并输出按下的是哪个键:
while(1)
{
Delay(1000);
if(key_pressed_flag){
ReadValue=KEY_Scan();
if(ReadValue!=0){
Delay(10);
if(ReadValue==KEY_Scan()){
printf("ReadValue=%d",ReadValue);
printf("
");
}
else
printf("read error!");
}
key_pressed_flag=0;
}
}
中断函数如下:
void EXTI15_10_IRQHandler(void){
key_pressed_flag=1;
EXTI_ClearITPendingBit(EXTI_Line12);
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line14);
EXTI_ClearITPendingBit(EXTI_Line15);
}
现在按下键盘,并没有输出,不知道问题出在哪里?求解答!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>