1. 程式人生 > >正點原子 24 NVIC中斷優先順序分組

正點原子 24 NVIC中斷優先順序分組

數值越,優先順序越

高優先順序的搶佔優先順序是可以打斷正在進行的低搶佔優先順序中斷的。
搶佔優先順序相同的中斷,高響應優先順序不可以打斷低響應優先順序的中斷。
搶佔優先順序相同的中斷,當兩個中斷同時發生的情況下,哪個響應優先順序高,哪個先執行。
如果兩個中斷的搶佔優先順序和響應優先順序都是一樣的話,則看哪個中斷先發生就先執行;

第一步:

中斷優先順序分組函式

在misc.c函式中

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;
}

#define IS_NVIC_PRIORITY_GROUP(GROUP) (((GROUP) == NVIC_PriorityGroup_0) || \
                                                                                        ((GROUP) == NVIC_PriorityGroup_1

) || \
                                                                                        ((GROUP) == NVIC_PriorityGroup_2
) || \
                                                                                        ((GROUP) == NVIC_PriorityGroup_3) || \
                                                                                        ((GROUP) == NVIC_PriorityGroup_4))

第二步:

設定中斷優先順序

void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
{
  uint8_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 --------------------------------*/    
    tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
    tmppre = (0x4 - tmppriority);
    tmpsub = tmpsub >> tmppriority;

    tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
    tmppriority |=  (uint8_t)(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   NVIC_InitStructure; 

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串列埠1中斷

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;// 搶佔優先順序為1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;// 子優先順序位2

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能

NVIC_Init(&NVIC_InitStructure);  //根據上面指定的引數初始化NVIC暫存器