中断类型:系统异常(内核中的中断),外部中断(内核外的中断),手册中表46中深灰色的是系统异常,白色部分是外部中断,地址是在flash中的地址。在图二中可以看到flash是从0x800 0000开始的,然而向量表格却是从0x0000 0000开始的,这样一来岂不是自相矛盾么?解答:STM32对flash进行了存储器重映射,将实际为0x800 0000开始的地址称之为0x0000 0000地址,其实质是向量表地址的偏移。
使能中断请求,与外设相关,通过相应的库函数使能。
配置中断优先级分组,在misc.c中的NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);进行设置
配置NVIC寄存器即配置中断源,抢占优先级、子优先级,中断使能。通过执行NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)函数来初始化misc.h中的结构体 NVIC_InitTypeDef来配置。
最后在stm32xxit.c中编写中断服务函数。
misc.c/*** @brief Configures the priority grouping: pre-emption priority and subpriority.* @param NVIC_PriorityGroup: specifies the priority grouping bits length. * This parameter can be one of the following values:* @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority* 4 bits for subpriority* @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority* 3 bits for subpriority* @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority* 2 bits for subpriority* @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority* 1 bits for subpriority* @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority* 0 bits for subpriority* @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. * The pending IRQ priority will be managed only by the subpriority. * @retval None*/
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;
}
misc.htypedef struct
{uint8_t NVIC_IRQChannel;//中断源(见Stm32f4xx.h中IRQn_Type结构体) /*!< Specifies the IRQ channel to be enabled or disabled.This parameter can be an enumerator of @ref IRQn_Type enumeration (For the complete STM32 Devices IRQ Channelslist, please refer to stm32f4xx.h file) */uint8_t NVIC_IRQChannelPreemptionPriority; //抢占优先级 /*!< Specifies the pre-emption priority for the IRQ channelspecified in NVIC_IRQChannel. This parameter can be a valuebetween 0 and 15 as described in the table @ref MISC_NVIC_Priority_TableA lower priority value indicates a higher priority */uint8_t NVIC_IRQChannelSubPriority; //子优先级 /*!< Specifies the subpriority level for the IRQ channel specifiedin NVIC_IRQChannel. This parameter can be a valuebetween 0 and 15 as described in the table @ref MISC_NVIC_Priority_TableA lower priority value indicates a higher priority */FunctionalState NVIC_IRQChannelCmd; //中断使能 /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannelwill be enabled or disabled. This parameter can be set either to ENABLE or DISABLE */
} NVIC_InitTypeDef;
解答: 中断服务程序实际上写在哪里都OK,但是呢,最好写在stm32xxit.c中,因为这个文件专门用于存放中断服务函数。中断服务函数名字要与启动文件中的中断函数名字一致,如果不一致,则中断服务函数不会被执行,执行默认的中断服务函数即启动文件中 . 程序,点程序即空循环程序。下图中断服务函数名字旁边有一个关键字weak,其意思是:该中断服务函数如果在其他文件定义了,则优先执行在其他文件写好的该中断服务函数,但是如果其他文件没有实现写该中断服务函数,则执行 . 这个空循环程序。