NVIC驅(qū)動有很多的用途,如使能或者失能IRQ(中斷請求),或者是使能或失能單獨的IRQ通道,或者是改變其優(yōu)先級。以下介紹相關(guān)函數(shù)的功能和基本用法。
1.NVIC_DeInit函數(shù)的功能是將外設(shè)NVIC寄存器重設(shè)為默認(rèn)值。
2.NVIC)PriorityGroupConfig函數(shù)的功能是設(shè)置優(yōu)先級分組:先占優(yōu)先級和從優(yōu)先級,且有一個輸入?yún)?shù)是優(yōu)先級分組的長度
例:定義先占優(yōu)先級1,從優(yōu)先級3.
NVIC_PriorityGroupConfig(NVICPriorityGroup_1);
3.NVIC_Init函數(shù)的功能是根據(jù)NVIC_InitStruct里面指定的參數(shù)初始化外設(shè)NVIC寄存器,其中NVIC_InitTypeDef structure定義結(jié)構(gòu)體的代碼如下:
typedef struct
{
u8 NVIC_IRQChannel;
u8 NVIC_IRQChannelPreemptionPriority;
u8 NVIC_IRQChannelSubPriority;
FunctionalState NVIC_IRQChannelCmd;
}NVIC_InitTypeDef;
NVIC_IRQChannel參數(shù)是用于使能或者失能指定的IRQ通道,其中IRQ通道有很多,可以自己查詢相關(guān)的手冊。
NVIC_IRQChannelPreemptionPriority參數(shù)設(shè)置了成員NVIC_IRQChannel中的先占優(yōu)先級。
IRQChannelSubPriority參數(shù)設(shè)置了成員NVIC_IRQChannel中的從優(yōu)先級。
當(dāng)選定NVIC_PriorityGroup_0,參數(shù)NVIC_IRQChannelPreemptionPriority對中斷通道的設(shè)置不產(chǎn)生影響;
當(dāng)選定NVIC_PriorityGroup_4,參數(shù)IRQChannelSubPriority對中斷通道的設(shè)置不產(chǎn)生影響。
NVIC_IRQChannelCmd參數(shù)指定了在成員NVIC_IRQChannel中定義的IRQ通道被使能或者失能,參數(shù)可取ENABLE 和 DISABLE。
例:定義優(yōu)先級
NVIC_InittypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVICPriorityGroup_1);//先占優(yōu)先級用1 ,從優(yōu)先級用3
//定義TIM3中斷的優(yōu)先級,先占優(yōu)先級為0,從優(yōu)先級為2
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure(&NVIC_InitStructure);
//定義USART1串口中斷的優(yōu)先級,先占優(yōu)先級1,從優(yōu)先級5.
NVIC_InitStructure.NVIC_IRQChannel = SUART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure(&NVIC_InitStructure);
//定義RTC中斷優(yōu)先級為1,從優(yōu)先級為7;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQSubPriority = 7;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure(&NVIC_InitStructure);
4.NVIC_SetVectorTable函數(shù)的功能是設(shè)置向量表的位置和偏移。其中輸入?yún)?shù)中,對于32位的OFFSET向量表基地址的偏移量對于FLASH,參數(shù)值必須高于0x08000100,對于RAM必須高于0X100.
例:對FLASH指定向量表基地址偏移量。
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);