分类: 嵌入式
2014-02-20 10:31:49
NVIC_IRQChannelPreemptionPriority:先占优先级
NVIC_IRQChannelSubPriority:从优先级
高先占优先级中断可以打断低先占优先级的中断,即可中断嵌套。先占优先级号码越小优先级越高
当先占优先级号码相同时,从优先级号码起作用。同先占优先级下的中断是不能进行嵌套的,只能根据从优先级的号码进行排队,从优先级号码即为排队号码,号码越低越靠前。
先用函数NVIC_PriorityGroupConfig(NVIC_PriorityGroup_x)决定有几个先占优先级和从优先级。NVIC_PriorityGroup_x可取如下值。
NVIC_PriorityGroup_0 => 选择第0组
NVIC_PriorityGroup_1 => 选择第1组
NVIC_PriorityGroup_2 => 选择第2组
NVIC_PriorityGroup_3 => 选择第3组
NVIC_PriorityGroup_4 => 选择第4组
举例:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置为1组。
NVIC_InitStructure.NVIC_IRQChannel
= EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别0,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel
= SPI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 指定抢占式优先级别1,可取0-1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定从优先级别0,可取0-7
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定从优先级别1,可取0-7
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI0_IRQChannel 先占优先级比SPI1_IRQChannel、USART1_IRQChannel高,所以EXTI0_IRQChannel中断可以打断正在执行的SPI1_IRQChannel/USART1_IRQChannel中断,实现中断嵌套。
SPI1_IRQChannel、USART1_IRQChannel先占优先级相等,不能相互打断,即在执行SPI1_IRQChannel/USART1_IRQChannel中断时USART1_IRQChannel/SPI1_IRQChannel中断无法打断。同时产生SPI1_IRQChannel、USART1_IRQChannel中断时,要根据从优先级进行排队,SPI1_IRQChannel从优先级比较高,所以先执行SPI1_IRQChannel中断。