-
配置IO口,PA0配置为下降沿中断,事件线为EXTI0,PB0为推换购
void IO_Config(void)
{
//配置IO口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);//IO口时钟使能
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置为上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
-
配置外部中断控制器
void IO_EXTI_Config(void)
{
//配置外部中断控制器
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能时钟
EXTI_InitStructure.EXTI_Line = EXTI_Line0; //使用IO口0作为中断口
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置EXTI线路为中断请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//将PA0挂接到EXTI0_IRQn中断源上
}
-
配置NVIC中断控制器
void NVIC_Config(void)
{
//配置中断控制器
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断组0
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//指定中断源,0-4管脚属于
EXTI0_IRQn~EXTI4_IRQn,5-9管脚属于EXTI9_5_IRQn,10-15管脚属于EXTI15_10_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3; //先占优先级为3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;//从优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
-
实现中断函数EXTI0_IRQHandler
void EXTI0_IRQHandler(void)//中断处理函数
{
static unsigned int tt;
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{
tt ^= 1;
if(tt)
GPIO_SetBits(GPIOB,GPIO_Pin_0);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
EXTI_ClearITPendingBit(EXTI_Line0);//清除EXTI线路挂起位
}
}
-
入口main函数
int main(void)
{
IO_Config();
IO_EXTI_Config();
NVIC_Config();
while(1);
}
DEMO下载地址:
https://download.csdn.net/download/mygod2008ok/11885816