本程序实现的功能:将GPIO.13端口设置成下降沿触发中断,当中断事件发生后,与GPIOD.8相连的LED变亮。
步骤:
① 配置要使用到的端口的时钟,输入输出以及端口的第二功能(这里的第二功能是指把端口配置成EXTI口)
void GPIO_Configuration(void)
{
/* Configure PC13 as input floating (EXTI Line13) */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
②初始化EXTI_InitStructure结构体的各种需要的配置
/* Connect EXTI Line13 to PC.13 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
/* Configure EXTI Line13 to generate an interrupt on falling edge */
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Generate software interrupt: simulate a falling edge applied on EXTI line 13 */
// EXTI_GenerateSWInterrupt(EXTI_Line13); //若使用此句,则不用手动触发中断事件,软件自动触发,继而直接转入中断处理函数。
③设置抢占式优先级和响应式优先级
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the EXTI15_10 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
④ 在stm32f10x_it.c的EXTI15_10_IRQHandler中断函数里,完成中断处理程序。
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET)
{
EXTI_ClearITPendingBit(EXTI_Line13); //别忘记清除中断标志位哦!!!!
GPIO_SetBits(GPIOD, GPIO_Pin_8 );
GPIO_ResetBits(GPIOD, GPIO_Pin_9);
}
}