1、中断和事件区别
从外部激励信号来看,中断和事件没有区别。从处理来看,中断会向CPU发出请求,需要CPU响应中断程序。事件则是对其他模块发出的脉冲信号,具体其他设备如何响应,就由这个模块决定。
2、配置
1、设置中断优先级分组
主要设置SCB->AIRCR[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;
}
2、配置中断参数
主要配置中断优先级寄存器(NVIC_IPRx)和中断开启寄存器((NVIC_ISERx),以及中断关闭寄存器(NVIC_ICERx)。
- 配置函数说明如下:
/**
* @brief Initializes the NVIC peripheral according to the specified
* parameters in the NVIC_InitStruct.
* 根据NVIC_InitStruct中的指定参数初始化NVIC外设。
* @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
* the configuration information for the specified NVIC peripheral.
* @retval None
*/
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
{
uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
{
/* Compute the Corresponding IRQ Priority --------------------------------*/
/* 计算相应的IRQ优先级 --------------------------------*/
tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
tmppre = (0x4 - tmppriority);
tmpsub = tmpsub >> tmppriority;
tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
tmppriority = tmppriority << 0x04;
NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
/* Enable the Selected IRQ Channels --------------------------------------*/
NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
(uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
}
else
{
/* Disable the Selected IRQ Channels -------------------------------------*/
NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
(uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
}
}
- NVIC_InitTypeDef结构说明如下:
typedef struct
{
// 指定要启用或禁用的IRQ通道。此参数可以是@ref IRQn_Type的值(有关完整的STM32设备IRQ通道列表,请参阅stm32f10x.h文件)
uint8_t NVIC_IRQChannel;
// 指定NVIC_IRQChannel中指定的IRQ通道的抢占优先级。 如表@ref NVIC_Priority_Table中所述,此参数可以是0到15之间的值。
uint8_t NVIC_IRQChannelPreemptionPriority;
// 为NVIC_IRQChannel中指定的IRQ通道指定子优先级。 如表@ref NVIC_Priority_Table中所述,此参数可以是0到15之间的值。
uint8_t NVIC_IRQChannelSubPriority;
// 指定是启用还是禁用NVIC_IRQChannel中定义的IRQ通道。此参数可以设置为ENABLE或DISABLE
FunctionalState NVIC_IRQChannelCmd;
} NVIC_InitTypeDef;