STM32中断的配置
扫描二维码
随时随地手机看文章
在STM32使用外部中断的基本步骤如下:
1. 设置好相应的时钟;
2.设置相应的中断;
3.IO口初始化;
4.把相应的IO口设置为中断线路(要在设置外部中断之前)并初始化;
5.在选择的中断通道的响应函数中中断函数。
//配置EXTI 参数
PA0-PF0 共用中断线源0
PA1-PF1 共用中断线源1
.............................................
所以不能同时使用PA0和PB0引脚产生外部中断,(查看官方程序stm32f30x_exti.c文件,下面是节选部分代码)
===============================================================================
##### EXTI features #####
===============================================================================
[..] External interrupt/event lines are mapped as following:
(#)All available GPIO pins are connected to the 16 external
interrupt/event lines from EXTI0 to EXTI15.
(#) EXTI line 16 is connected to the PVD output
(#) EXTI line 17 is connected to the RTC Alarm event
(#) EXTI line 18 is connected to USB Device wakeup event
(#) EXTI line 19 is connected to the RTC Tamper and TimeStamp events
(#) EXTI line 20 is connected to the RTC wakeup event
(#) EXTI line 21 is connected to the Comparator 1 wakeup event
(#) EXTI line 22 is connected to the Comparator 2 wakeup event
(#) EXTI line 23 is connected to the I2C1 wakeup event
(#) EXTI line 24 is connected to the I2C2 wakeup event
(#) EXTI line 25 is connected to the USART1 wakeup event
(#) EXTI line 26 is connected to the USART2 wakeup event
(#) EXTI line 27 is reserved
(#) EXTI line 28 is connected to the USART3 wakeup event
(#) EXTI line 29 is connected to the Comparator 3 event
(#) EXTI line 30 is connected to the Comparator 4 event
(#) EXTI line 31 is connected to the Comparator 5 event
(#) EXTI line 32 is connected to the Comparator 6 event
(#) EXTI line 33 is connected to the Comparator 7 event
(#) EXTI line 34 is connected for thr UART4 wakeup event
(#) EXTI line 35 is connected for the UART5 wakeup event
/*****************************************************************************/
GPIO_InitTypeDef GPIO_InitStructure;//定义结构体
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//outside inteput 引脚配置
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//Enable the GPIOB clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//Enable the SYSCFG clock
//打开PB时钟 配置PB1 引脚为输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//配置对应的 GPIO 为EXTI 线
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,EXTI_PinSource1);
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line1; //中断线源
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; //中断
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising; //上升沿触发中断
EXTI_InitStructure.EXTI_LineCmd=ENABLE;//中断线使能
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;//外部中断1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级 数字越小,优先级越高,优先级高的优先执行。
NVIC_InitStructure.NVIC_IRQChannelSubPriority=15; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置NVIC
抢占优先级高的中断可以打断抢占优先级低的中断,这时是不用管响应优先级的;响应优先级只是在两个或者多个抢占优先级相同的中断同时到来时进入响应优先级高的中断,而如果进入这个中断之后再来一个抢占优先级相同但是响应优先级更高的中断,则不会打断已有的中断。
总之就是抢占优先级不同的时候看抢占优先级级数,先响应抢占优先级高的中断,而且抢占优先级高的中断可以打断抢占优先级低的中断;当抢占优先级相同的时候看响应优先级级数,先响应响应优先级高的中断,但是响应优先级高的中断不可以打断正在执行的响应优先级低的中断。
注意: stm32官方给的中断响应函数(查看文件startup_stm32f30x.s),空函数名(自己编写函数体),不要自己定义新的中断响应函数。
DCD WWDG_IRQHandler ; Window WatchDog
DCD PVD_IRQHandler ; PVD through EXTI Line detection
DCD TAMPER_STAMP_IRQHandler ; Tamper and TimeStamps through the EXTI line
DCD RTC_WKUP_IRQHandler ; RTC Wakeup through the EXTI line
DCD FLASH_IRQHandler ; FLASH
DCD RCC_IRQHandler ; RCC
DCD EXTI0_IRQHandler ; EXTI Line0
DCD EXTI1_IRQHandler ; EXTI Line1
DCD EXTI2_TS_IRQHandler ; EXTI Line2 and Touch
DCD EXTI3_IRQHandler ; EXTI Line3
DCD EXTI4_IRQHandler ; EXTI Line4
DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7