STM32外部中断之二
1配置中断 1、 分配中断向量表: /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 2、 设置中断优先级: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中断优先级 3、 初始化外部中断: /*允许EXTI4中断 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;//强占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能 NVIC_Init(&NVIC_InitStructure); //初始化中断 注意:如果我们配置的外部针脚为PA4,或PB4,或PC4,PD4等,那么采用的外部中断也必须是EXTI4,同样,如果外部中断针脚是PA1,PB1,PC1,PD1 那么中断就要用EXTI1,其他类推。 2配置GPIO针脚作为外部中断的触发事件 1、 选择IO针脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 注意,如果的针脚是端口的4号针脚,配置的中断一定是EXTI4 2、 配置针脚为输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 3、 初始化针脚 GPIO_Init(GPIOD,&GPIO_InitStructure); 3配置EXTI线,使中断线和IO针脚线连接上 1、 将EXTI线连接到IO端口上 将EXTI线4连接到端口GPIOD的第4个针脚上 GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); 注意:如果配置的针脚是4号,那么参数必须是GPIO_PinSource4 如果配置的针脚是3号,那么参数必须是GPIO_PinSource3 2、配置中断边沿 /*配置EXTI线0上出现下降沿,则产生中断*/ EXTI_InitStructure.EXTI_Line = EXTI_Line4; 注意:如果配置的4号针脚,那么EXTI_Line4是必须的 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断线使能 EXTI_Init(&EXTI_InitStructure); //初始化中断 EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4中断允许 到此中断配置完成,可以写中断处理函数。 举例: 配置函数 /************************************************************************* * 函数名 NVIC_Configration * 描述 配置各个中断寄存器 * 输入 无 * 输出 无 * 返回值 无 ****************************************************************************/ void NVIC_Configration(void) { NVIC_InitTypeDef NVIC_InitStructure; //#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //#else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //#endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中断优先级 /*允许EXTI4中断 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*允许EXTI9中断*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*配置SysTick处理优先级:优先级以及子优先级*/ } /************************************************************************ * 函数名 :GPIO_Configuration(void) * 描述 :配置TIM2阵脚 * 输入 :无 * 输出 :无 * 返回 :无 ************************************************************************/ void GPIO_Configuration(void){ /* GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_O |
stm32外部中 相关文章:
- STM32笔记之外部中断GPIO(12-03)
- STM32学习之外部中断(12-02)
- STM32学习之--外部中断(11-26)
- STM32外部中断易出错总结(11-26)
- STM32中EXTI(外部中断)和NVIC(嵌套向量中断)的关系(11-25)
- STM32外部中断的使用(11-23)