平台:
GD32F350G8
背景:
工作的关系接触到了gd32的一个开发板,老板的第一个需求就是利用中断实现按键控制LED,并尝试长按短按,前后搞了好几天,到处去寻找资料,好在皇天不负有心人,全部搞定。
首先外部中断的一些思路:
1.使能IO口时钟,配置I/o 口为输入模式
2.开启AFIO时钟
3.设置IO口与中断线的映射关系
4.配置中断分组(NVIC),使能中断
5.初始化EXTI,选择触发方式
6.设置中断处理函数
代码如下:
1.使能IO口时钟,配置I/o 口为输入模式
rcu_periph_clock_enable(RCU_CFGCMP);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
2.开启AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
3.设置IO口与中断线的映射关系
syscfg_exti_line_config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN4);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN5);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN6);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN7);
4.配置中断分组(NVIC),使能中断
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
5.初始化EXTI,选择触发方式
nvic_irq_enable(EXTI4_15_IRQn, 2U, 0U);
exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_RISING);
6.设置中断处理函数
void EXTI4_15_IRQHandler (void)
{
if (SET == exti_interrupt_flag_get(EXTI_4)
{
{
exti_interrupt_flag_clear(EXTI_4);
delay_ms(10);//防抖
RED_LED_ON;
}
else
RED_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_5))
{
{
delay_ms(10);//防抖
exti_interrupt_flag_clear(EXTI_5);
BLUE_LED_ON;
}
else
BLUE_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_6))
{
{
delay_ms(10);//防抖
GREEN_LED_ON;
exti_interrupt_flag_clear(EXTI_6);
}
else
GREEN_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_7))
{
{
delay_ms(10);//防抖
YELLOW_LED_ON;
exti_interrupt_flag_clear(EXTI_7);
}
else
YELLOW_LED_OFF;
}
全部代码如下:
GPIO.c
/*****************************************************************************/
#include "gpio.h"
/*****************************************************************************/
#define GPIO_MODULE
/*****************************************************************************/
void GPIO_Init(void)
{
rcu_periph_clock_enable(RCU_CFGCMP);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
// GPIO A
gpio_mode_set(RED_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, RED_LED);
gpio_output_options_set(RED_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RED_LED);
gpio_mode_set(GREEN_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GREEN_LED);
gpio_output_options_set(GREEN_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GREEN_LED);
gpio_mode_set(BLUE_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, BLUE_LED);
gpio_output_options_set(BLUE_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, BLUE_LED);
gpio_mode_set(YELLOW_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, YELLOW_LED);
gpio_output_options_set(YELLOW_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, YELLOW_LED);
gpio_mode_set(KEY_DET1_PORT , GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY_DET1);
gpio_mode_set(KEY_DET2_PORT , GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY_DET2);
gpio_mode_set(KEY_DET3_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY_DET3);
gpio_mode_set(KEY_DET4_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY_DET4);
}
/*****************************************************************************/
void GPIO_NVIC_Init(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(EXTI4_15_IRQn, 2U, 0U);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB,EXTI_SOURCE_PIN4);
exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_RISING);
exti_interrupt_flag_clear(EXTI_4);
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(EXTI4_15_IRQn, 2U, 0U);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB, EXTI_SOURCE_PIN5);
exti_init(EXTI_5, EXTI_INTERRUPT, EXTI_TRIG_RISING);
exti_interrupt_flag_clear(EXTI_5);
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(EXTI4_15_IRQn, 2U, 0U);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB, EXTI_SOURCE_PIN6);
exti_init(EXTI_6, EXTI_INTERRUPT, EXTI_TRIG_RISING);
exti_interrupt_flag_clear(EXTI_6);
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
nvic_irq_enable(EXTI4_15_IRQn, 2U, 0U);
syscfg_exti_line_config(EXTI_SOURCE_GPIOB, EXTI_SOURCE_PIN7);
exti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_RISING);
exti_interrupt_flag_clear(EXTI_7);
}
/*****************************************************************************/
Gd32f3X0_it.c
#include "gd32f3x0_it.h"
#include "usbd_int.h"
#include "usb_delay.h"
#include "custom_hid_core.h"
#include "gpio.h"
extern uint8_t key_flag1;
extern uint8_t key_flag2;
extern uint8_t key_flag3;
extern uint8_t key_flag4;
void EXTI4_15_IRQHandler (void)
{
if (SET == exti_interrupt_flag_get(EXTI_4)
{
{
exti_interrupt_flag_clear(EXTI_4);
delay_ms(10);//防抖
RED_LED_ON;
}
else
RED_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_5))
{
{
delay_ms(10);//防抖
exti_interrupt_flag_clear(EXTI_5);
BLUE_LED_ON;
}
else
BLUE_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_6))
{
{
delay_ms(10);//防抖
GREEN_LED_ON;
exti_interrupt_flag_clear(EXTI_6);
}
else
GREEN_LED_OFF;
}
if (SET == exti_interrupt_flag_get(EXTI_7))
{
{
delay_ms(10);//防抖
YELLOW_LED_ON;
exti_interrupt_flag_clear(EXTI_7);
}
else
YELLOW_LED_OFF;
}