STM32F2系列的EXTI使用说明
1. 外部中断事件线的映射
140个GPIO连接到了16个外部中断/事件线,如下图所示:
GPIO与外部中断/事件线关联图
另外7个外部中断/事件线分别连接:
EXTI Line 16:PVD输出;
EXTI Line 17:RTC报警事件;
EXTI Line 18:USB OTG FS唤醒事件;
EXTI Line 19:以太网唤醒事件;
EXTI Line 20:USB OTG HS唤醒事件;
EXTI Line 21:RTC Tamper and TimeStamp事件
EXTI Line 22:RTC唤醒事件。
2. EXTI与SYSCFG(System configurationcontroller)关系
SYSCFG主要用于重新定位存储器在代码空间的位置,用于选择以太网的PHY和管理外部中断线与GPIO的联系。
如下图所示,用于配置外部中断并连接到EXTI外部中断。
3. EXTI初始化
voidKEY_EXTI_Initialize(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOC clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
/* Configure PC2 pin as input floating */
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/* Connect EXTI Line2 to PC2 pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource2);
/* Configure EXTI Line2 */
EXTI_InitStructure.EXTI_Line= EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd= ENABLE;
EXTI_Init(&EXTI_InitStructure);
/*Enable and set EXTI Line2 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel =EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
3.1.使能作为外部中断的引脚的GPIO端口时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
3.2.使能SYSCFG时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
3.3.初始化GPIO引脚
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
GPIO_Init(GPIOC,&GPIO_InitStructure);
3.4.连接引脚到外部中断线
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource2);
说明:每个引脚对应相应的线,如GPIO1则对应EXTI_PinSource1,查看1中的图。
3.5.配置外部中断线
EXTI_InitStructure.EXTI_Line =EXTI_Line2;
EXTI_InitStructure.EXTI_Mode =EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd= ENABLE;
EXTI_Init(&EXTI_InitStructure);
3.6.使能中断
NVIC_InitStructure.NVIC_IRQChannel= EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
NVIC_Init(&NVIC_InitStructure);
记得对IRQChannelPreemptionPriority和IRQChannelSubPriority赋值均为0x0F。
4. EXTI中断函数
voidEXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
/* LED1闪烁标识符 */
LED1_flag = 1;
/* LED1闪烁次数 */
LED1_Twinkle_Num = 2*Twinkle_Num;
g_OD.Position_Offset =g_OD.current_angle;
/* Clear the EXTI line 0 pending bit */
EXTI_ClearITPendingBit(EXTI_Line2);
}
}