GD32F103VE外部中断线线0~15,对应外部IO口的输入中断。它有7个中断向量,外部中断线0 ~ 4分别对应EXTI0_IRQn ~ EXTI4_IRQn中断向量;外部中断线 5 ~ 9 共用一个 EXTI9_5_IRQn中断向量;外部中断线10~15 共用一个 EXTI15_10_IRQn中断向量。
7个中断向量:EXTI0_IRQn,EXTI1_IRQn,EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn,EXTI9_5_IRQn和EXTI15_10_IRQn。
这7个中断向量对应7个中断服务函数: EXTI0_IRQHandler(),EXTI1_IRQHandler(),EXTI2_IRQHandler() ,EXTI3_IRQHandler(),EXTI4_IRQHandler(),EXTI9_5_IRQHandler(),EXTI15_10_IRQHandler();
测试程序如下:
#include "KEY.h"
#include "LED.h"/*
GD32F103VE外部中断线线0~15,对应外部IO口的输入中断。
它有7个中断向量,外部中断线0 ~ 4分别对应EXTI0_IRQn ~ EXTI4_IRQn中断向量;
外部中断线 5 ~ 9 共用一个 EXTI9_5_IRQn中断向量;外部中断线10~15 共用一个EXTI15_10_IRQn中断向量。
7个中断向量:EXTI0_IRQn,EXTI1_IRQn,EXTI2_IRQn, EXTI3_IRQn, EXTI4_IRQn,EXTI9_5_IRQn和EXTI15_10_IRQn。
这7个中断向量对应7个中断服务函数:
EXTI0_IRQHandler();
EXTI1_IRQHandler();
EXTI2_IRQHandler();
EXTI3_IRQHandler();
EXTI4_IRQHandler();
EXTI9_5_IRQHandler();
EXTI15_10_IRQHandler();
*/void ExternalInterrupt3_Init(void);
void ExternalInterrupt7_Init(void);
void ExternalInterrupt13_Init(void);//函数功能:初始化ExternalInterrupt3
void ExternalInterrupt3_Init(void)
{//NVIC_PRIGROUP_PRE4_SUB0:抢占优先级为4bit(取值为0~15),子优先级为0bit(没有响应优先级)//NVIC_PRIGROUP_PRE3_SUB1:抢占优先级为3bit(取值为0~7),子优先级为1bit(取值为0~1)//NVIC_PRIGROUP_PRE2_SUB2:抢占优先级为2bit(取值为0~3),子优先级为2bit(取值为0~3)//NVIC_PRIGROUP_PRE1_SUB3:抢占优先级为1bit(取值为0~1),子优先级为3bit(取值为0~7)//NVIC_PRIGROUP_PRE0_SUB4:抢占优先级为0bit(没有抢占优先级),子优先级为3bit(取值为0~15)nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);//设置系统中断优先级"抢占优先级为4bit,子优先级为0bit"nvic_irq_enable(EXTI3_IRQn, 2U, 0U);//设置EXTI3_IRQn的中断优先级,抢占优先级为2,子优先级为0rcu_periph_clock_enable(RCU_GPIOE);//使能GPIOE时钟,enable GPIO clockrcu_periph_clock_enable(RCU_AF); //外部中断,需要使能复用功能时钟gpio_init(GPIOE, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3);//将GPIOE3设置为浮空输入gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOE, GPIO_PIN_SOURCE_3);//设置GE3引脚为外部中断源,select GPIO pin exti sourcesexti_init(EXTI_3, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//配置外部中断线使用外部中断3(EXTI_3)//中断模式为外部中断(EXTI_INTERRUPT)//中断触发方式为下降沿中断(EXTI_TRIG_FALLING)exti_interrupt_flag_clear(EXTI_3);//清除外部中断3标志exti_interrupt_enable(EXTI_3);//使能外部中断3(EXTI_3)
}//函数功能:外部中断3的中断服务函数,外部中断0~外部中断4具有独立的中断入口地址
void EXTI3_IRQHandler(void)
{FlagStatus ret;ret=exti_interrupt_flag_get(EXTI_3);//读取外部中断3(EXTI_3)的中断标志//get EXTI lines flag when the interrupt flag is setif(RESET != ret){MCU_Led_Toggle();exti_interrupt_flag_clear(EXTI_3);}
}//函数功能:初始化ExternalInterrupt7
void ExternalInterrupt7_Init(void)
{rcu_periph_clock_enable(RCU_GPIOE);//使能GPIOE时钟,enable GPIO clockrcu_periph_clock_enable(RCU_AF);//使能复用功能时钟gpio_init(GPIOE, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);//将GPIOE7设置为浮空输入nvic_irq_enable(EXTI5_9_IRQn, 2U, 0U);//设置EXTI5_9_IRQn的中断优先级,抢占优先级为2,子优先级为0gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOE, GPIO_PIN_SOURCE_7);//设置GE7引脚为外部中断源,select GPIO pin exti sourcesexti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//配置外部中断线使用外部中断7(EXTI_7)//中断模式为外部中断(EXTI_INTERRUPT)//中断触发方式为下降沿中断(EXTI_TRIG_FALLING)exti_interrupt_flag_clear(EXTI_7);//清除外部中断7标志exti_interrupt_enable(EXTI_7);//使能外部中断7(EXTI_7)
}//this function handles external lines 5 to 9 interrupt request
//函数功能:外部中断5~外部中断9的中断服务函数
void EXTI5_9_IRQHandler(void)
{FlagStatus ret;ret=exti_interrupt_flag_get(EXTI_7);//读取外部中断7(EXTI_7)的中断标志//get EXTI lines flag when the interrupt flag is setif(RESET != ret){MCU_Led_Toggle();exti_interrupt_flag_clear(EXTI_7);}
}//函数功能:初始化ExternalInterrupt13
void ExternalInterrupt13_Init(void)
{rcu_periph_clock_enable(RCU_GPIOE);//使能GPIOE时钟,enable GPIO clockrcu_periph_clock_enable(RCU_AF);//使能复用功能时钟gpio_init(GPIOE, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13);//将GPIOE13设置为浮空输入nvic_irq_enable(EXTI10_15_IRQn, 2U, 0U);//设置EXTI10_15_IRQn的中断优先级,抢占优先级为2,子优先级为0gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOE, GPIO_PIN_SOURCE_13);//设置GE13引脚为外部中断源,select GPIO pin exti sourcesexti_init(EXTI_13, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//配置外部中断线使用外部中断13(EXTI_13)//中断模式为外部中断(EXTI_INTERRUPT)//中断触发方式为下降沿中断(EXTI_TRIG_FALLING)exti_interrupt_flag_clear(EXTI_13);//清除外部中断13标志exti_interrupt_enable(EXTI_13);//使能外部中断13(EXTI_13)
}//this function handles external lines 10 to 15 interrupt request
//函数功能:外部中断10~外部中断15的中断服务函数
void EXTI10_15_IRQHandler(void)
{FlagStatus ret;ret=exti_interrupt_flag_get(EXTI_13);//读取外部中断13(EXTI_13)的中断标志//get EXTI lines flag when the interrupt flag is setif(RESET != ret){MCU_Led_Toggle();exti_interrupt_flag_clear(EXTI_13);}
}
#include "LED.h"void LED_Init(void);//函数功能:初始化MCU_Led
void LED_Init(void)
{rcu_periph_clock_enable(RCU_GPIOD);//使能GPIOD时钟,enable GPIO clockgpio_init(GPIOD, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);//将GPIOD15设置为输出上拉MCU_Led_Off();
// GPIO_BC(GPIOD) = GPIO_PIN_15;//清除GPIOD15输出,GPIO bit clear register
}
#ifndef __LED_H
#define __LED_H#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t#define MCU_Led_Off() GPIO_BOP(GPIOD)=GPIO_PIN_15 //GD15输出高电平
#define MCU_Led_On() GPIO_BC(GPIOD)=GPIO_PIN_15 //GD15输出低电平
#define MCU_Led_Toggle() gpio_bit_write( GPIOD,GPIO_PIN_15,(bit_status)((1-gpio_input_bit_get(GPIOD,GPIO_PIN_15))) )
//GD15取反输出电平extern void LED_Init(void);#endif
测试时,按下连接到PE3,PE7和PE13的按钮,就可以实现LED灯反转。