比如要是使用GPC3作为中断源,上升沿触发 外部中断必须配置好EXTI和NVIC 使用以下函数打开SYSCFG的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE)
GPIO设置如下 void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOF Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//注意要打开SYSCFG时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
因为要用的IO口做中断源,故IO的复用功能必须使能,F4的与F103的有些不同,它的外部中断复用功能是由SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOx,GPIO_PinSourcex); 进行配置的。
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3); //这是以前的F103所用的配置函数
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource3); //407使用的配置函数
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
设置外部中断源和优先级 void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断处理函数
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
do something();
EXTI_ClearITPendingBit(EXTI_Line3);
printf("EXTI3_IRQHandler\r\n");
}
}
|