继续体验GD32的外设功能,在GD32107C-EVAL上有3个按键可以供用户使用。
本次实验采用了其中的两个按键,KEY1 和 KEY2。通过原理图可以看出外部中断触发方式应该选用下降沿触发,分别有EXTI0和EXTI13
下面贴出初始化函数:
1、使能GPIOA,GPIOC,AFIO的时钟
2、将IO口配置成上拉输入
3、配置外部中断为下降沿触发
4、将EXTILINE0映射到PA0,EXTILINE13映射到PC13
5、配置中断优先级,使能中断
void ButtonInit() { GPIO_InitTypeDef GPIO_InitStruct; EXTI_InitTypeDef EXTI_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO,ENABLE ); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU ; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOA,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; GPIO_Init (GPIOC,&GPIO_InitStruct); GPIO_EXTILineConfig (GPIO_PortSourceGPIOA,GPIO_PinSource0); GPIO_EXTILineConfig (GPIO_PortSourceGPIOC,GPIO_PinSource13); EXTI_InitStruct.EXTI_Line = EXTI_Line0 | EXTI_Line13; EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt ; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_Init (&EXTI_InitStruct); NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; NVIC_Init (&NVIC_InitStruct); NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; NVIC_Init (&NVIC_InitStruct); }
中断服务函数:
void EXTI0_IRQHandler() { if(EXTI_GetITStatus(EXTI_Line0) == SET ) { gLedStatusFlag = LED_ON; EXTI_ClearITPendingBit (EXTI_Line0); } } void EXTI15_10_IRQHandler() { if(EXTI_GetITStatus(EXTI_Line13) == SET ) { gLedStatusFlag = LED_OFF; EXTI_ClearITPendingBit (EXTI_Line13); } }
主函数:
int main() { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); LedPinInit(); ButtonInit(); while(1) { if(gLedStatusFlag == LED_ON) { LedPowerSelect(LED_ON); gLedStatusFlag = LED_NONE; } else if(gLedStatusFlag == LED_OFF) { LedPowerSelect(LED_OFF); gLedStatusFlag = LED_NONE; } } }
按下KEY1使LED亮起来,按下KEY2让LED熄灭,下面是效果图:
启动开发板程序运行:
按下KEY1
按下KEY2
GD32的性能和STM32相比,初步体验,也是完全不输给STM32。期待接下来一步步的体验,希望得到GD32带来的惊喜,呵呵O(∩_∩)O~