STM32F407 external interrupt configuration steps

Introduce the STM32F407 external interrupt configuration steps, take the button as an example, realize the external interrupt configuration, and use the button to trigger the interrupt to control the LED light.

[1] Introduction to external interrupt related knowledge

img

The CM4 core supports 256 interrupts, including 16 core interrupts and 240 external interrupts, and has 256 levels of programmable interrupt settings. But STM32F4 does not use all of the CM4 core, but only a part of it.

STM32F40xx/STM32F41xx has a total of 92 interrupts, and STM32F42xx/STM32F43xx has a total of 96 interrupts. The following only uses STM32F40xx/41xx as an example.

Among the 92 interrupts of STM32F40xx/STM32F41xx, there are 10 core interrupts and 82 maskable interrupts, with 16 levels of programmable interrupt priority, and these 82 maskable interrupts are commonly used by us.

img

img

img

【2】External interrupt sample code

exti.c code

#include "exti.h"

/*
函数功能:按键外部中断初始化
硬件连接:
KEY0 --->PE4  按下为低电平
KEY1 --->PE3  按下为低电平
KEY2 --->PE2  按下为低电平
KEY_UP-->PA0  按下为高电平
*/
void KEY_EXTI_Init(void)
{
    
    
		/*1. 开启SYSCFG时钟 */
		RCC->APB2ENR|=1<<14;
		
	  /*2. 开放来自线x上的中断请求*/
	  EXTI->IMR|=1<<0; //中断线0
	  EXTI->IMR|=1<<2; //中断线2
	  EXTI->IMR|=1<<3; //中断线3
	  EXTI->IMR|=1<<4; //中断线4
	
	  /*3. 配置中断线触发边沿*/
	  EXTI->RTSR|=1<<0; //上升沿
	  EXTI->FTSR|=1<<0; //下降沿
	  
		EXTI->RTSR|=1<<2; //上升沿
	  EXTI->FTSR|=1<<2; //下降沿
	 
		EXTI->RTSR|=1<<3; //上升沿
	  EXTI->FTSR|=1<<3; //下降沿
	
	  EXTI->RTSR|=1<<4; //上升沿
	  EXTI->FTSR|=1<<4; //下降沿
	  
	  /*4. 配置产生中断的对应IO口*/
	  SYSCFG->EXTICR[0]&=~(0xf<<0*4);
	  SYSCFG->EXTICR[0]|=0x0<<0*4;
	  
		SYSCFG->EXTICR[0]&=~(0xf<<2*4);
	  SYSCFG->EXTICR[0]|=0x4<<2*4;
		
		SYSCFG->EXTICR[0]&=~(0xf<<3*4);
	  SYSCFG->EXTICR[0]|=0x4<<3*4;
		
		SYSCFG->EXTICR[1]&=~(0xf<<0*4);
	  SYSCFG->EXTICR[1]|=0x4<<0*4;
		
	  /*5. 配置中断优先级*/
	  SetNVICPriorityGrouping(EXTI0_IRQn,2,2);
		SetNVICPriorityGrouping(EXTI2_IRQn,2,2);
		SetNVICPriorityGrouping(EXTI3_IRQn,2,2);
		SetNVICPriorityGrouping(EXTI4_IRQn,2,2);
}	

/*
功 能:外部中断线0中断服务函数
*/

void EXTI0_IRQHandler(void)
{
    
    
	 DelayMs(10);
	 if(KEY_UP)
	 {
    
    
			LED0=!LED0;
			LED1=!LED1;
			printf("KEY_UP\r\n");
	 }
	 EXTI->PR|=1<<0; //清除中断标志位
}

/*
功 能:外部中断线2中断服务函数
*/
void EXTI2_IRQHandler(void)
{
    
    
	 DelayMs(10);
	 if(KEY2==0)
	 {
    
    
			LED0=!LED0;
			LED1=!LED1;
			printf("KEY2\r\n");
	 }
	 EXTI->PR|=1<<2; //清除中断标志位
}

/*
功 能:外部中断线3中断服务函数
*/
void EXTI3_IRQHandler(void)
{
    
    
	 DelayMs(10);
	 if(KEY1==0)
	 {
    
    
			LED0=!LED0;
			LED1=!LED1;
		  printf("KEY1\r\n");
	 }
	 EXTI->PR|=1<<3; //清除中断标志位
}


/*
功 能:外部中断线4中断服务函数
*/
void EXTI4_IRQHandler(void)
{
    
    
	 DelayMs(10);
	 if(KEY0==0)
	 {
    
    
			LED0=!LED0;
			LED1=!LED1;
		  printf("KEY0\r\n");
	 }
	 EXTI->PR|=1<<4; //清除中断标志位
}

main.c code

#include "stm32f4xx.h" // Device header
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "sys.h"
#include "exti.h"


int main(void)
{
    
    
		LED_Init();
		KEY_Init();
		USART1_Init(84,115200);
		KEY_EXTI_Init();
		while(1)
		{
    
    
				
		}
}

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/131458133