STM32 (register)-external interrupt

1: For external interrupts, write code steps;
first: configure the IO port as input (PA15, keys KEY_1, PA0, key KEY_UP);
second: set the IO port multiplex clock (generally open clock only applies to high and low levels Use of), configure interrupt lines and trigger conditions;
third: interrupt grouping and interrupt enable;
fourth: write interrupt service functions;

2: Code part;
interrupt.c part;

在这里插入代码片
#include "interrupt.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
//对于外部中断,16个中断线(另外的在这里不谈)对应0~15个io口;
//只有中断0~4有独立的中断服务函数,9~5公用一个,15~10公用一个


//中断服务函数配置,函数名固定;(启动文件里有);因此不用再.h文件里声明
void EXTI0_IRQHandler(void)
{
    
    
 delay_ms(10);
 if(PAin(0)==1)
 {
    
    
  LED0=0;
  LED1=0;
 }
 EXTI->PR=1<<0;
}
void EXTI15_10_IRQHandler(void)
{
    
    
 delay_ms(10);
 if(PAin(15)==0)
 {
    
    
  LED1=0;
  LED0=1;
 }
 EXTI->PR=1<<15;//清除中断标志位
}

void EXTI1_Init()
{
    
    
 GPIOA->CRL &=0XFFFFFFF0;//端口输入配置
 GPIOA->CRL |=0X00000008;
 GPIOA->CRH &=0X0FFFFFFF;
 GPIOA->CRH |=0X80000000;
 Ex_NVIC_Config(GPIO_A,0,2);//开启io口复用时钟,配置中断线,和触发条件
 Ex_NVIC_Config(GPIO_A,15,1);//1是下降沿触发,2是上升沿触发
 MY_NVIC_Init(2,0,EXTI0_IRQn,2);//中断分组及其使能
 MY_NVIC_Init(2,1,EXTI15_10_IRQn,2);
}

interuupt.h part;

在这里插入代码片
#ifndef _INTERRUPT_H_
#define _INTERRUPT_H_

#include "sys.h"
//这里不声明中断服务函数,原因在上
void EXTI1_Init(void);
#endif

Main function

在这里插入代码片
int main(void)
{
    
       
 Stm32_Clock_Init(9);
 delay_init(72);     //延时初始化
 uart_init(72,9600); //串口初始化 
 LED_Init();     //初始化与LED连接的硬件接口
 EXTI1_Init();  //外部中断初始化
 while(1)
 {
    
         
  printf("ok!\r\n");//一个标志。一直输出就代表程序运行
  delay_ms(1000);   
 } 
}
****在这里我想讲清为啥要用中断;
	大家都知道,中断的作用就是去执行另一个程序,但我们如果调用函数也能实现,但为什要用呢?这是因为中断是脱离主程序执行的,而函数则不行,这大大提升了程序的运行效率;

Guess you like

Origin blog.csdn.net/qq_45906993/article/details/108567842