key.c
#include "key.h"
void key_init()
{
//0.clk_enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_EXTIT, ENABLE);
//1.GPIO PA0 PE2 PE3 PE4
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IN; //输入模式
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_0;
GPIO_InitStruct.GPIO_PuPd =GPIO_PuPd_NOPULL; //无上拉 无下拉
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz; //输出速率为50MHz
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_Init(GPIOE,&GPIO_InitStruct);
//2.PA0-EXTI Line0 PE2-EXTI2 PE3-EXTI3 PE4-EXTI4
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); //用来指定中断/事件线的输入源
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
//3.EXTI CFG
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line =EXTI_Line0|EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStruct.EXTI_LineCmd =ENABLE;
EXTI_InitStruct.EXTI_Mode =EXTI_Mode_Interrupt; //中断模式
EXTI_InitStruct.EXTI_Trigger =EXTI_Trigger_Falling; //下降沿
EXTI_Init(&EXTI_InitStruct);
//4.NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//2 bits for pre-emption priority
//2 bits for subpriority
NVIC_InitTypeDef NVIC_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 =EXTI2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =3;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel =EXTI3_IRQn;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel =EXTI4_IRQn;
NVIC_Init(&NVIC_InitStruct);
}
void EXTI0_IRQHandler(void)
{
//IRQ?
if(EXTI_GetITStatus(EXTI_Line0)==SET) //如果中断产生
{
//do
if(Bit_RESET==GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) //确定是由于对应的按键按下
{
delay(100);
GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
}
//clear
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI2_IRQHandler(void)
{
//IRQ?
if(EXTI_GetITStatus(EXTI_Line2)==SET)
{
//do
if(Bit_RESET==GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))
{
delay(100);
GPIO_ToggleBits(GPIOF, GPIO_Pin_10);
}
//clear
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler(void)
{
//IRQ?
if(EXTI_GetITStatus(EXTI_Line3)==SET)
{
//do
if(Bit_RESET==GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3))
{
delay(100);
GPIO_ToggleBits(GPIOE, GPIO_Pin_13);
}
//clear
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
void EXTI4_IRQHandler(void)
{
//IRQ?
if(EXTI_GetITStatus(EXTI_Line4)==SET)
{
//do
if(Bit_RESET==GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4))
{
delay(100);
GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
}
//clear
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
|