比如要是使用GPC3作為中斷源,上升沿觸發(fā)外部中斷必須配置好EXTI和NVIC
使用以下函數(shù)打開SYSCFG的時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE)
GPIO設(shè)置如下
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的復(fù)用功能必須使能,F(xiàn)4的與F103的有些不同,它的外部中斷復(fù)用功能是由SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOx,GPIO_PinSourcex); 進(jìn)行配置的。
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3);//這是以前的F103所用的配置函數(shù)
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource3); //407使用的配置函數(shù)
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);
}
設(shè)置外部中斷源和優(yōu)先級
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);
}
中斷處理函數(shù)
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
do something();
EXTI_ClearITPendingBit(EXTI_Line3);
printf(“EXTI3_IRQHandlerrn”);
}
}