stm32f10x NVIC_Init()函数

本文详细介绍了STM32外部中断初始化与NVIC配置的过程,包括使用EXTI_InitTypeDef与NVIC_InitTypeDef结构体进行配置,以及正确设置中断优先级的方法。通过实例展示了如何使能外部中断并正确响应中断请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                    函数 NVIC_Init()

函数名

NVIC_Init

函数原型void NVIC_Init(NVIC_InitTypeDdf *  NVIC_InitStruct)
功能描述根据 NVIC_InitStruct 中指定的参数初始化外设NVIC寄存器
输入参数NVIC_InitStruct:指向结构体 NVIC_InitTypeDdf 的指针
输出参数
返回值
先决条件
被调用函数

NVIC_InitTypeDdf 定义于文件“stm32f10x_nvic.h":

typedef struct
{
  uint8_t NVIC_IRQChannel;                    /*!< Specifies the IRQ channel to be enabled or disabled.
                                                   This parameter can be a value of @ref IRQn_Type 
                                                   (For the complete STM32 Devices IRQ Channels list, please
                                                    refer to stm32f10x.h file) */

  uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< Specifies the pre-emption priority for the IRQ channel
                                                   specified in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  uint8_t NVIC_IRQChannelSubPriority;         /*!< Specifies the subpriority level for the IRQ channel specified
                                                    in NVIC_IRQChannel. This parameter can be a value
                                                    between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  FunctionalState NVIC_IRQChannelCmd;         /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel
                                                    will be enabled or disabled. 
                                                    This parameter can be set either to ENABLE or DISABLE */   
} NVIC_InitTypeDef;

 

例:

void Extix_Init(void)//外部中断初始化
{
	EXTI_InitTypeDef Exti_initStructure;
	NVIC_InitTypeDef Nvic_initStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//外部中断,需使能AFIO时钟
	key_Init();//按键初始化 PC1、PC13  

    //PC.1 PC.13 中断线以及中断初始化配置                                   
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource1);
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);
	
	Exti_initStructure.EXTI_Line = EXTI_Line1 | EXTI_Line13;
	Exti_initStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	Exti_initStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	Exti_initStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&Exti_initStructure);
	
    //NVIC 配置	
	Nvic_initStructure.NVIC_IRQChannel = EXTI1_IRQn ;
	Nvic_initStructure.NVIC_IRQChannelPreemptionPriority = 2;
	Nvic_initStructure.NVIC_IRQChannelSubPriority = 1;
	Nvic_initStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&Nvic_initStructure);
	
	Nvic_initStructure.NVIC_IRQChannel =  EXTI15_10_IRQn;
	Nvic_initStructure.NVIC_IRQChannelPreemptionPriority = 2;
	Nvic_initStructure.NVIC_IRQChannelSubPriority = 1;
	Nvic_initStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&Nvic_initStructure);
}

 

一开始这样写,结果两个中断都没响应

Nvic_initStructure.NVIC_IRQChannel = EXTI1_IRQn | EXTI15_10_IRQn;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值