NVIC_Type结构体使用实例
时间: 2023-08-02 09:06:28 浏览: 155
NVIC_Type结构体是用于控制中断的结构体,下面是一个使用实例:
```
#include "stm32f4xx.h"
int main(void)
{
// 使能SYSCFG时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// 配置PA0为外部中断
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while(1)
{
// 程序主循环
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
// 处理中断事件
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
```
在上面的代码中,我们使用了NVIC_InitTypeDef结构体来初始化NVIC控制器。NVIC_InitStructure.NVIC_IRQChannel表示中断通道,NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority表示抢占优先级,NVIC_InitStructure.NVIC_IRQChannelSubPriority表示子优先级,NVIC_InitStructure.NVIC_IRQChannelCmd表示中断使能状态。在上面的代码中,我们将PA0配置为外部中断,并且当PA0引脚上升沿触发时,会触发EXTI0_IRQn中断,并且处理该中断事件。
阅读全文