11.中断
从NVIC说起,NVIC中断控制器,内核的外设
在讲如何配置中断优先级之前,我们需要先了解下NVIC。NVIC 是嵌套向量中断控制器,控制着整个芯片中断相关的功能,它跟内核紧密耦合,是内核里面的一个外设。但是各个芯片厂商在设计芯片的时候会对Cortex-M3 内核里面的NVIC 进行裁剪,把不需要的部分去掉,所以说STM32 的NVIC 是Cortex-M3 的NVIC 的一个子集。
NVIC的配置
在配置中断的时候我们一般只用ISER、ICER 和IP 这三个寄存器,ISER 用来使能中断,ICER 用来失能中断,IP 用来设置中断优先级。
固件库文件core_cm3.h中:在这里我们只是参考即可,可以直接去调用其中的函数来配置。
1.首先需要配置优先级:
函数在misc.c中:
配置NVIC优先级分组:
例:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* 配置NVIC为优先级组1 */ 有2个主优先级,8个字优先级
主优先级低的可以打断主优先级高的,相同的主优先级不能打断。在相同的主优先级里:子优先级是的中断同时发生时,优先执行子优先级低的哪个。
我们可以跟进这个函数,看看他是如何配置的。
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
{
/* Check the parameters */
assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup)); //断言,检查
/* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup; //配置SCB->AIRCR
}
函数也是非常简单:
SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
操作AIRCR寄存器: SCB结构体就是我们之前提到的,在core_cm3.h定义的
//NVIC_PriorityGroup我们用户传进来的,AIRCR_VECTKEY_MASK .#define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000) ,
跟进寄存器描述,我们可以知道为什么要或上AIRCR_VECTKEY_MASK ,这个是寄存器的访问钥匙,就是说[31:16]必须是0xFA05,
我们传入的是NVIC_PriorityGroup,根据宏定义,我们知道:
#define NVIC_PriorityGroup_0 ((uint32_t)0x700) /*!< 0 bits for pre-emption priority4 bits for subpriority */
#define NVIC_PriorityGroup_1 ((uint32_t)0x600) /*!< 1 bits for pre-emption priority3 bits for subpriority */
#define NVIC_PriorityGroup_2 ((uint32_t)0x500) /*!< 2 bits for pre-emption priority2 bits for subpriority */
#define NVIC_PriorityGroup_3 ((uint32_t)0x400) /*!< 3 bits for pre-emption priority1 bits for subpriority */
#define NVIC_PriorityGroup_4 ((uint32_t)0x300) /*!< 4 bits for pre-emption priority0 bits for subpriority */
我们操作的就是[10:8]位,就是寄存器描述的优先级分组了。
库函数给我们封装上,绕了一大圈。。。其实就是在写SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
我们配置好了优先级分组,接下来该配置哪些呢。
初始化NVIC_InitTypeDef结构体:
这个NVIC_InitTypeDef结构体包含哪些变量:
/**
* @brief NVIC Init Structure definition
*/
typedef struct
{
uint8_t NVIC_IRQChannel; *!< Specifies the IRQ channel to be enabled or disabled.This parameter can be a value of @ref //IRQn_Type (For thecomplete STM32 Devices IRQ Channels list, pleaserefer to stm32f10x.h file) */
uint8_t NVIC_IRQChannelPreemptionPriority; /*!< Specifies the pre-emption priority for the IRQ channelspecified in NVIC_IRQChannel.Thisparameter //can be a value between 0 and 15 as described in the table @ref NVIC_Priority_Table */
uint8_t NVIC_IRQChannelSubPriority; /*!< Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel. This parameter //can be a value between 0 and 15 as described in the table @ref NVIC_Priority_Table */
FunctionalState NVIC_IRQChannelCmd; /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled.
//This parameter can be set either to ENABLE or DISABLE */
} NVIC_InitTypeDef;
/* 配置中断源:EXTI0_IRQn */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
/* 配置抢占优先级 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* 配置子优先级 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* 使能中断通道 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
根据需求来配置就可以了....
值得一说的就是中断源了/* 配置中断源:EXTI0_IRQn */,枚举定义,IRQn_Type类型
中断源是从哪个来的呢,就是我们所说的中断向量表了.
这个中断向量表官方已经为我们设定了。启始文件.s
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ;Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV Handler
DCD SysTick_Handler ; SysTick Handler
; External Interrupts
DCD WWDG_IRQHandler ; Window Watchdog
DCD PVD_IRQHandler ; PVD through EXTI Line detect
DCD TAMPER_IRQHandler ; Tamper
DCD RTC_IRQHandler ; RTC
DCD FLASH_IRQHandler ; Flash
DCD RCC_IRQHandler ; RCC
DCD EXTI0_IRQHandler ; EXTI Line 0
DCD EXTI1_IRQHandler ; EXTI Line 1
DCD EXTI2_IRQHandler ; EXTI Line 2
DCD EXTI3_IRQHandler ; EXTI Line 3
DCD EXTI4_IRQHandler ; EXTI Line 4
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
DCD ADC1_2_IRQHandler ; ADC1 & ADC2
DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX
DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0
DCD CAN1_RX1_IRQHandler ; CAN1 RX1
DCD CAN1_SCE_IRQHandler ; CAN1 SCE
DCD EXTI9_5_IRQHandler ; EXTI Line 9..5
DCD TIM1_BRK_IRQHandler ; TIM1 Break
DCD TIM1_UP_IRQHandler ; TIM1 Update
DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
DCD TIM2_IRQHandler ; TIM2
DCD TIM3_IRQHandler ; TIM3
DCD TIM4_IRQHandler ; TIM4
DCD I2C1_EV_IRQHandler ; I2C1 Event
DCD I2C1_ER_IRQHandler ; I2C1 Error
DCD I2C2_EV_IRQHandler ; I2C2 Event
DCD I2C2_ER_IRQHandler ; I2C2 Error
DCD SPI1_IRQHandler ; SPI1
DCD SPI2_IRQHandler ; SPI2
DCD USART1_IRQHandler ; USART1
DCD USART2_IRQHandler ; USART2
DCD USART3_IRQHandler ; USART3
DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line
DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend
DCD TIM8_BRK_IRQHandler ; TIM8 Break
DCD TIM8_UP_IRQHandler ; TIM8 Update
DCD TIM8_TRG_COM_IRQHandler ; TIM8 Trigger and Commutation
DCD TIM8_CC_IRQHandler ; TIM8 Capture Compare
DCD ADC3_IRQHandler ; ADC3
DCD FSMC_IRQHandler ; FSMC
DCD SDIO_IRQHandler ; SDIO
DCD TIM5_IRQHandler ; TIM5
DCD SPI3_IRQHandler ; SPI3
DCD UART4_IRQHandler ; UART4
DCD UART5_IRQHandler ; UART5
DCD TIM6_IRQHandler ; TIM6
DCD TIM7_IRQHandler ; TIM7
DCD DMA2_Channel1_IRQHandler ; DMA2 Channel1
DCD DMA2_Channel2_IRQHandler ; DMA2 Channel2
DCD DMA2_Channel3_IRQHandler ; DMA2 Channel3
DCD DMA2_Channel4_5_IRQHandler ; DMA2 Channel4 & Channel5
__Vectors_End
我们这里用到的是EXTI0_IRQn中断源,如果遇到中断,程序会自动跳转,Default_Handler (默认的中断程序 ) Default_Handler ;程序名 PROC;程序;
Default_Handler PROC
EXPORT EXTI0_IRQHandler [WEAK] ; 跳入EXTI0_IRQHandler 程序 [WEAK]表示为若定义
固件库中stm32f10x_it.c文件中:又帮我们重新定义了中断函数,我们根据自己的需求,重写该函数即可。
外部中断exit配置:
内核外设配置完成后,我们需要配置我们的外设:
就是把我们gpio配置一番就可以了。
/* 选择用到的GPIO */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
/* 配置为浮空输入 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIO_PORT_B, &GPIO_InitStructure);
2.配置EXIT
/* 选择EXTI的信号源 */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0; //这个中断线,只能1对1,GPA0、GPB0、GPC1对应EXTI_Line0
// 只能1对1 GPA0对应EXTI_Line0, q其他端口就不能对应了。
/* EXTI为中断模式 */
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
/* 上升沿中断 */
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
/* 使能中断 */
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//这就基本配置完成了。其实也挺简单的....
如何实际编程使用外部中断
1.时钟设置并打开相应GPIO模块时钟
2.将相应GPIO配置为浮空输入
3.NVIC设置
4.将外部中断线使能触发
5.外部中断线使能触发
6.准备好ISP,并在ISP处等待执行中断程序即可