STM32F429 >> 7. 中断

版权声明:如需转载请标注 https://blog.csdn.net/weixin_40973138/article/details/85192992

STM32 的中断主要涉及两个部分,NVIC 和EXTI

NVIC 是嵌套向量中断控制器,控制着整个芯片中断相关的功能;外部中断/事件控制器(EXTI)可以实现对每个中断/事件线进行单独配置,可以单独配置为中断或者事件,以及触发事件的属性。

EXTI 可分为两大功能:产生中断产生事件

当其产生中断时,其中断线路将输入信号输入到NVIC,进一步运行中断服务函数,实现功能;而当其产生事件时,则是传输一个脉冲信号给其他外设使用
在这里插入图片描述
EXTI 是在APB2 总线上的

在这里插入图片描述
EXTI0 至 EXTI15 用于 GPIO,通过编程控制可以实现任意一个 GPIO 作为 EXTI的输
入源。

编程要点


  1. 配置NVIC
    a. 设置优先级组
    b. 配置 NVIC_InitStructure
    c. 使能 NVIC
  2. 配置EXTI
    a. 初始化 LED
    b. 初始化 KEY
    c. 开启 EXTI 时钟
    d. 执行 NVIC 配置函数
    e. 连接 EXTI 到中断源
    f. 配置 EXTI_InitStructure
    g. 使能 EXTI
  3. 编写EXTI 中断服务函数

bsp_exti.c

/**
  ******************************************************************************
  * @file    bsp_exti.c
  * @author  Waao
  * @version V1.0.0
  * @date    21-Dec-2018
  * @brief   This file contains some board support package's functions for the configuration of the NVIC.
  *            
  ******************************************************************************
  * @attention
  *
  * None
  *
  ******************************************************************************
  */


#include <bsp_exti.h>
#include <bsp_key.h>
#include <bsp_led.h>

/**
  * @brief  Configure the NVIC
  * @param  None
  * @retval None
  */
static void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//Configure the NVIC to prioritygroup1
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	
	//Configure the preemption priority to 1
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	//Configure the subpriority to 1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	//Enable the interrupt channel
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
	//Configure the interrupt channel
	NVIC_InitStructure.NVIC_IRQChannel = KEY1_INT_EXTI_IRQ;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = KEY2_INT_EXTI_IRQ;
	NVIC_Init(&NVIC_InitStructure);
}


/**
  * @brief  Configure the EXTI
  * @param  None
  * @retval None
  */
void EXTI_Key_Config(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;
	
	KEY_GPIO_Config();
	LED_GPIO_Config();
	
	//Enable the clock of the SYSCFG(must do that when use the GPIO external interrupt)
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
		
	NVIC_Configuration();
	
	//Connect the exti interrupt source to key1 pin 
	SYSCFG_EXTILineConfig(KEY1_INT_EXTI_PORTSOURCE, KEY1_INT_EXTI_PINSOURCE);
	
	//Choice the exti interrupt source
	EXTI_InitStructure.EXTI_Line = KEY1_INT_EXTI_LINE;
	//Choice the mode
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	//Choice the trigger type
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	//Enable
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	
	//Connect the exti interrupt source to key1 pin 
	SYSCFG_EXTILineConfig(KEY2_INT_EXTI_PORTSOURCE, KEY2_INT_EXTI_PINSOURCE);
	
	EXTI_InitStructure.EXTI_Line = KEY2_INT_EXTI_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
}

bsp_exti.h

/**
  ******************************************************************************
  * @file    bsp_exti.h
  * @author  Waao
  * @version V1.0.0
  * @date    21-Dec-2018
  * @brief   This file contains some board support package's definition for the exti.
  *            
  ******************************************************************************
  * @attention
  *
  * None
  *
  ******************************************************************************
  */
#ifndef __EXTI_H_
#define	__EXTI_H_

#include <stm32f4xx.h>


/*******************************************************/
#define KEY1_INT_GPIO_PORT                GPIOA
#define KEY1_INT_GPIO_CLK                 RCC_AHB1Periph_GPIOA
#define KEY1_INT_GPIO_PIN                 GPIO_Pin_0
#define KEY1_INT_EXTI_PORTSOURCE          EXTI_PortSourceGPIOA
#define KEY1_INT_EXTI_PINSOURCE           EXTI_PinSource0
//The EXTI_Line number must be consistent with the GPIO_Pin number
#define KEY1_INT_EXTI_LINE                EXTI_Line0
#define KEY1_INT_EXTI_IRQ                 EXTI0_IRQn

#define KEY1_IRQHandler                   EXTI0_IRQHandler


#define KEY2_INT_GPIO_PORT                GPIOC
#define KEY2_INT_GPIO_CLK                 RCC_AHB1Periph_GPIOC
#define KEY2_INT_GPIO_PIN                 GPIO_Pin_13
#define KEY2_INT_EXTI_PORTSOURCE          EXTI_PortSourceGPIOC
#define KEY2_INT_EXTI_PINSOURCE           EXTI_PinSource13
#define KEY2_INT_EXTI_LINE                EXTI_Line13
#define KEY2_INT_EXTI_IRQ                 EXTI15_10_IRQn

#define KEY2_IRQHandler                   EXTI15_10_IRQHandler

/*******************************************************/

static void NVIC_Configuration(void);
void EXTI_Key_Config(void);

#endif

中断线须与端口号一致

stm32f4xx_it.c

部分代码,其余部分不需修改:

/******************************************************************************/
/*                 STM32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f429_439xx.s).                         */
/******************************************************************************/

/**
  * @brief  Turn the status of the led2 when we trigger the exti0
  * @param  None
  * @retval None
  */
void KEY1_IRQHandler(void)
{
	LED2_TOGGLE;
	EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE);
}

/**
  * @brief  Turn the status of the led3 when we trigger the exti13
  * @param  None
  * @retval None
  */
void KEY2_IRQHandler(void)
{
	LED3_TOGGLE;
	EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE);
}

中断服务函数在stm32f4xx_it.c 中进行构建
中断服务函数的名称必须与启动文件当中给出的一致
中断服务函数中任务执行完必须清除中断标志

猜你喜欢

转载自blog.csdn.net/weixin_40973138/article/details/85192992
今日推荐