一,简介
STM32中断类型分为终端类型系统异常和外部中断,分别体现在内核水平和外设水平。STM32 中断非常强大,支持256个中断,每个外设都可以产生中断。
二,中断控制器
NVIC:嵌套向量中断控制器,属于内核外设,管理着包括内核和片上所有外设的中断相关的功能,在core_cm3.h定义。
typedef struct
{
__IO uint32_t ISER[8]; /*!< Offset: 0x000 Interrupt Set Enable Register */
uint32_t RESERVED0[24];
__IO uint32_t ICER[8]; /*!< Offset: 0x080 Interrupt Clear Enable Register */
uint32_t RSERVED1[24];
__IO uint32_t ISPR[8]; /*!< Offset: 0x100 Interrupt Set Pending Register */
uint32_t RESERVED2[24];
__IO uint32_t ICPR[8]; /*!< Offset: 0x180 Interrupt Clear Pending Register */
uint32_t RESERVED3[24];
__IO uint32_t IABR[8]; /*!< Offset: 0x200 Interrupt Active bit Register */
uint32_t RESERVED4[56];
__IO uint8_t IP[240]; /*!< Offset: 0x300 Interrupt Priority Register (8Bit wide) */
uint32_t RESERVED5[644];
__O uint32_t STIR; /*!< Offset: 0xE00 Software Trigger Interrupt Register */
} NVIC_Type;
三,中断编程的顺序
1. 使能中断请求 ;
NVIC的中断使能寄存器相当于一个大门,而配置所需外设的相应寄存器相当于一个小门,同时打开才能使能中断请求。
2. 配置中断优先级分组;
优先级设定:NVIC->IPRx
优先级分组:SCB->AIRCR:PRIGROUP[10:8]
/**
* @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
* @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;
}
3. 配置NVIC寄存器,初始化NVIC_InitTypeDef;
typedef struct
{
uint8_t NVIC_IRQChannel; //中断源//
uint8_t NVIC_IRQChannelPreemptionPriority;//抢占优先级//
uint8_t NVIC_IRQChannelSubPriority; //子优先级//
FunctionalState NVIC_IRQChannelCmd; //使能或者失能
} NVIC_InitTypeDef;
4. 编写中断服务函数;
中断服务函数名是在启动文件中规定好的,不能更改。
通常情况下,中断服务函数要写在stm32f10x_it.c文件中