STM32中断的使用

@

目录

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;

NVIC_PriorityGroupConfig //设置优先级分组:先占优先级和从优先级
NVIC_Init //根据 NVIC_InitStruct 中指定的参数初始化外设 NVIC 寄存器

EXTI外部中断/事件控制器

//官方文档
typedef struct  //结构体
{
  uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.
                                         This parameter can be any combination of @ref EXTI_Lines */
   
  EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.
                                         This parameter can be set either to ENABLE or DISABLE */ 
}EXTI_InitTypeDef;

EXTI_Init //根据 EXTI_InitStruct 中指定的参数初始化外设 EXTI 寄存器

外部中断服务函数:

GPIO引脚 中断标志位 中断处理函数
PA0~PG0 EXTI0 EXTI0_IRQHandler
PA1~PG1 EXTI1 EXTI1_IRQHandler
PA2~PG2 EXTI2 EXTI2_IRQHandler
PA3~PG3 EXTI3 EXTI3_IRQHandler
PA4~PG4 EXTI4 EXTI4_IRQHandler
PA5~PG5 EXTI5 EXTI9_5_IRQHandler
~
PA10~PG10 EXTI10 EXTI15_10_IRQHandler
~
PA15~PG15 EXTI15
  • 中断线 0-4 每个中断线对应一个中断函数,中断线 5-9 共用中断函数 EXTI9_5_IRQHandler,中断线 10-15 共用中断函数 EXTI15_10_IRQHandler

代码示例

//bsp_exti.c
#include "bsp_exti.h"
#include <misc.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_exti.h>
#include <stm32f10x_rcc.h>

void PA0_EXTI0_Configuration(void)
{
    //定义结构体
    NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;

    //开启时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                     //设置优先级分组
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;                    //选择中断类型 外部中断线0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;                  //响应先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                     //使能外设NVIC
    NVIC_Init(&NVIC_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 						
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);         //选择GPIO管脚用作外部中断线路

    EXTI_InitStructure.EXTI_Line = EXTI_Line0;                          //使能外部线路0
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                 //请求模式 中断请求
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;             //触发模式为下降沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                           //使能外设EXTI
    EXTI_Init(&EXTI_InitStructure);     
    

    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;                    //选择中断类型 外部中断线0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;                  //响应先级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                     //使能外设NVIC
    NVIC_Init(&NVIC_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 						
	GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);         //选择GPIO管脚用作外部中断线路

    EXTI_InitStructure.EXTI_Line = EXTI_Line5;                          //使能外部线路0
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;                 //请求模式 中断请求
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;             //触发模式为下降沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                           //使能外设EXTI
    EXTI_Init(&EXTI_InitStructure);  
}

void EXTI0_IRQHandler(void)
{
    //检测指定的EXTI线路中断请求发生与否 消抖
    if(EXTI_GetITStatus(EXTI_Line0) != RESET)
    {
        GPIOA->ODR ^= GPIO_Pin_1;
        EXTI_ClearITPendingBit(EXTI_Line0);//清除中断标志位
    }


}

void EXTI9_5_IRQHandler(void) 
{
    //检测指定的EXTI线路中断请求发生与否 消抖
    if(EXTI_GetITStatus(EXTI_Line5) != RESET)
    {
        GPIOA->ODR ^= GPIO_Pin_4;
        EXTI_ClearITPendingBit(EXTI_Line5);//清除中断标志位
    }

}
//bsp_exti.h
#ifndef __BSP_EXTI_H__
#define __BSP_EXTI_H__

#include <stm32f10x.h>

void PA0_EXTI0_Configuration(void);

#endif
//效果 PA0 PA5按键响应中断并取反相应LED电平
//main.c
#include "Led_Key.h" //见上篇
#include "bsp_exti.h"
#include "stm32f10x_gpio.h"

int main(void)
{
	Led_Configuration();
	Key_Configuration();
	PA0_EXTI0_Configuration();
	while(1)
	{
		
	}
	
}

...Thank you for reading...______________________Heisenberg_Poppings

上一篇:python


下一篇:分享一下然让显卡满血复活的小技巧(GTX)