MCU为STM32F103C8T6
首先纠正几个错误关系:
1,中断线程与中断引脚是一一对应的,PA0、PB0…PG0对应的是EXTI_Line0,以此类推,不是随便关联起来就可以的;
2,相应的中断向量函数,EXTI0_IRQHandler~EXTI4_IRQHandler对应EXTI_Line0~EXTI_Line4,EXTI9_5_IRQHandler对应EXTI_Line5~EXTI_Line9,注意是一对多,同样EXTI15_10_IRQHandler对应EXTI_Line10~EXTI_Line15;
3,中断通道号EXTI15_10_IRQn不同头文件叫法不同,有的为EXTI15_10_IRQChannel;
4,使用中断必须开启引脚复用:RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
5,中断处理函数中注意清除中断标志位,防止持续进入中断:EXTI_ClearITPendingBit(EXTI_Line10);
初始化代码:
- void EXTI15_10_Init(void)
- {
- NVIC_InitTypeDef nvicConfig;
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- /* Configure pin: IRQ */
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- // Connect EXTI Line
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
- {
- // Configure EXTI line
- EXTI_InitTypeDef extiConfig;
- extiConfig.EXTI_Line = EXTI_Line10;
- extiConfig.EXTI_Mode = EXTI_Mode_Interrupt;
- extiConfig.EXTI_Trigger = EXTI_Trigger_Falling;
- extiConfig.EXTI_LineCmd = ENABLE;
- EXTI_Init(&extiConfig);
- }
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- nvicConfig.NVIC_IRQChannel = EXTI15_10_IRQn;
- nvicConfig.NVIC_IRQChannelPreemptionPriority = 0;
- nvicConfig.NVIC_IRQChannelSubPriority = 0;
- nvicConfig.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvicConfig);
- }
中断处理函数代码,位于文件stm32f10x_it.c中
- void EXTI15_10_IRQHandler(void) {
- static signed int i=1;
- if (EXTI_GetITStatus(EXTI_Line10) != RESET)
- {
- if(i>0)
- {
- GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET); // Led1 ON
- GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_RESET); // Led2 ON
- }
- else
- {
- GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_RESET); // Led2 ON
- GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_SET); // Led2 ON
- }
- i *= -1;
- EXTI_ClearITPendingBit(EXTI_Line10);
- }
- }