GPIO 输入上拉,按键按下,pin接地,触发中断
Key_Config
1 void Key_Config(void) 2 { 3 GPIO_InitTypeDef GPIO_InitStructure; 4 5 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 6 7 /* Configure KEY1 Button PC4*/ 8 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 9 10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 11 GPIO_InitStructure.GPIO_Pin = GPIO_KEY1; 12 GPIO_Init(GPIOC, &GPIO_InitStructure); 13 14 /* Configure KEY2 Button PB10*/ 15 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 16 17 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 18 GPIO_InitStructure.GPIO_Pin = GPIO_KEY2; 19 GPIO_Init(GPIOB, &GPIO_InitStructure); 20 21 /* Configure KEY3 Button PC13*/ 22 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 23 24 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 25 GPIO_InitStructure.GPIO_Pin = GPIO_KEY3; 26 GPIO_Init(GPIOC, &GPIO_InitStructure); 27 28 /* Configure KEY4 Button PA0*/ 29 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 30 31 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 32 GPIO_InitStructure.GPIO_Pin = GPIO_KEY4; 33 GPIO_Init(GPIOA, &GPIO_InitStructure); 34 }
KEY_EXIT_Init
1 void KEY_EXIT_Init(void) 2 { 3 EXTI_InitTypeDef EXTI_InitStructure; 4 5 6 /* Connect Button EXTI Line to Button GPIO Pin */ 7 GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4); 8 9 /* Configure Button EXTI line */ 10 EXTI_InitStructure.EXTI_Line = EXTI_Line4; 11 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 12 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 13 EXTI_InitStructure.EXTI_LineCmd = ENABLE; 14 EXTI_Init(&EXTI_InitStructure); 15 16 17 /* Connect Button EXTI Line to Button GPIO Pin */ 18 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10); 19 20 /* Configure Button EXTI line */ 21 EXTI_InitStructure.EXTI_Line = EXTI_Line10; 22 EXTI_Init(&EXTI_InitStructure); 23 24 25 /* Connect Button EXTI Line to Button GPIO Pin */ 26 GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13); 27 28 /* Configure Button EXTI line */ 29 EXTI_InitStructure.EXTI_Line = EXTI_Line13; 30 EXTI_Init(&EXTI_InitStructure); 31 32 /* Connect Button EXTI Line to Button GPIO Pin */ 33 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); 34 35 /* Configure Button EXTI line */ 36 EXTI_InitStructure.EXTI_Line = EXTI_Line0; 37 EXTI_Init(&EXTI_InitStructure); 38 }
Key_NVIC_Config
1 void Key_NVIC_Config(void) 2 { 3 NVIC_InitTypeDef NVIC_InitStructure; 4 5 /* Set the Vector Table base address at 0x08000000 */ 6 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000); 7 8 /* Configure the Priority Group to 2 bits */ 9 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 10 11 12 /* Enable the EXTI Interrupt */ 13 NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; 14 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 15 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 16 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 17 NVIC_Init(&NVIC_InitStructure); 18 19 NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; 20 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 21 NVIC_Init(&NVIC_InitStructure); 22 23 NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; 24 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 25 NVIC_Init(&NVIC_InitStructure); 26 27 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; 28 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; 29 NVIC_Init(&NVIC_InitStructure); 30 }