首页 > 编程知识 正文

stm32f103可屏蔽中断通道,nvic_inittypedef

时间:2023-05-03 09:32:19 阅读:41238 作者:1667

stm32的外部中断EXTI和NVIC 中断如何配置步骤如下一,初始化用于中断的GPIO端口

二、初始化EXTI (外部中断)

三.配置NVIC (中断优先级)中断控制器

四.写中断函数

一、初始化用来中断的GPIO口

以GPIOA1为例:

voidexti_init(void ) { GPIO_InitTypeDef shank_init; //1、定义GPIOA1的结构体RCC _ AP B2 periphclockcmd (RCC _ AP B2 per iph _ gpioa,ENABLE ); //2,启用GPIOA时钟shank _ init.gpio _ mode=gpio _ mode _ IPU; shank_init.GPIO_Pin=GPIO_Pin_1; shank _ init.gpio _ speed=gpio _ speed _ 10 MHz; //3、配置GPIOA的结构体gpio_init(gpioa,shank_init ); //4、GPIOA1时钟初始化(} 二、初始化EXTI(外部中断)

边缘检测电路----"上升沿/下降沿触发中断

2.1、EXTI中断原理图

2.2、EXTI外部中断源

有20条中断/事件线

2.3、EXT外部中断的结构体,及参数讲解

typedef struct { uint 32 _ texti _ line; //! specifiestheextilinestobeenabledordisabled.thisparametercanbeanycombinationof @ refe xti _ lines */ext imode _ typededefexti specifiesthemodefortheextilines.thisparametercanbeavalueof @ refextimode _ typedef */extitriggger _ typedefexti _ trig gedef specifiesthetriggersignalactiveedgefortheextilines.thisparametercanbeavalueof @ refextimode _ typedef */functionalstatatetextexte specifiesthenewstateoftheselectedextilines.thisparametercanbeseteithertoenableordisable */} exti _ init typedef; 参数:1、EXTI_Line:EXTI中断/事件线选择; 可选的EXTI0到exti19(STM32f10x_exti.h )

2、EXTI_Mode:EXTI模式选择; 选择中断/事件模式

3、EXTI_Trigger :EXTI边缘触发事件,上升/下降/上升下降边缘触发; 控制是否启用33558www.Sina.com/Exti线; 或者禁用。

4、EXTI_LineCmd :

定义voidexti_init(void )//1、GPIOA1的结构体GPIO_InitTypeDef shank_init; //1.1,定义GPIOA1中断结构EXTI_InitTypeDef exti_init; //1.2,//2,GPIOA时钟RCC _ AP B2 periphclockcmd (RCC _ AP B2 per iph _ gpioa,ENABLE ); //2.1、GPIOA时钟为外部中断RCC _ AP B2 periphclockcmd (RCC _ AP B2 per iph _ afio,ENABLE ); //3.1、设置gpio _ extilineconfig (gpio _ portsourcegpioa,GPIO_PinSource1)作为外部中断源; //3.2、配置GPIOA的结构体shank _ init.gpio _ mode=gpio _ mode _ IPU; shank_init.GPIO_Pin=

GPIO_Pin_1; shank_init.GPIO_Speed = GPIO_Speed_10MHz; //4、初始化GPIOA1时钟 GPIO_Init(GPIOA,&shank_init); //5、配置EXTI中断 exti_init.EXTI_Line = EXTI_Line1; exti_init.EXTI_Mode = EXTI_Mode_Interrupt; exti_init.EXTI_Trigger = EXTI_Trigger_Falling; exti_init.EXTI_LineCmd = ENABLE; //6、初始化中断结构体 EXTI_Init(&exti_init); }

三、配置NVIC(中断优先级)中断控制器

NVIC结构体:

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;

参数:
1、NVIC_IRQChannel : 设置中断的通道(misc.h)

2、NVIC_IRQChannelPreemptionPriority : 设置抢占优先级

3、NVIC_IRQChannelSubPriority : 设置子优先级

4、NVIC_IRQChannelCmd : 设置是否使能中断控制器,使能/禁用

void Exti_Init(void){ //1、定义GPIOA1的结构体 GPIO_InitTypeDef shank_init; //1.1、定义GPIOA1中断结构体 EXTI_InitTypeDef exti_init; //1.2、定义NVIC中断结构体 NVIC_InitTypeDef nvic_init; // 2、使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //2.1、使能GPIOA时钟为外部中断 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //7.3、配置中断优先级组结构体 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //3.1、设置为外部中断源输入 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1); //3.2、配置GPIOA的结构体 shank_init.GPIO_Mode = GPIO_Mode_IPU; shank_init.GPIO_Pin = GPIO_Pin_1; shank_init.GPIO_Speed = GPIO_Speed_10MHz; //4、初始化GPIOA1时钟 GPIO_Init(GPIOA,&shank_init); //5、配置EXTI中断 exti_init.EXTI_Line = EXTI_Line1; exti_init.EXTI_Mode = EXTI_Mode_Interrupt; exti_init.EXTI_Trigger = EXTI_Trigger_Falling; exti_init.EXTI_LineCmd = ENABLE; //6、初始化中断结构体 EXTI_Init(&exti_init); // 7、配置NVIC结构体 nvic_init.NVIC_IRQChannel = EXTI1_IRQn; nvic_init.NVIC_IRQChannelCmd = ENABLE; nvic_init.NVIC_IRQChannelPreemptionPriority = 1; nvic_init.NVIC_IRQChannelSubPriority = 1; //7.1、初始化NVIC结构体 NVIC_Init(&nvic_init);}

四、编写中断处理函数
在启动文件中startup_stm32f10x_hds;找到定义中断处理函数

void EXTI_ClearFlag(uint32_t EXTI_Line);//1、清除中断标志位ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);//2、触发中断函数

中断处理函数编写:

void EXTI1_IRQHandler(void){ if ( EXTI_GetITStatus(EXTI_Line1) != RESET ) //{ GPIO_ResetBits(GPIOC, GPIO_Pin_13); delay(3000); GPIO_SetBits(GPIOC, GPIO_Pin_13);} EXTI_ClearFlag(EXTI_Line1); }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。