比方要是运用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_IRQHandlerrn”);
}
}