STM32外部中断

配置是stm32外部中断步骤:
1.使能IO时钟和AFIO时钟
2.配置EXIT中断线
3.配置中断控制器NVIC (EXTI15_10_IRQn,EXTI4_IRQn)
4.写中断服务子程序 (EXTI15_10_IRQHandler)
首先要明白中断IO对应的中断线EXTIx
其中

PA0-PE0 对应的是EXTI0
PA4-PE4 对应的是EXTI4
PA5-PE5 对应的是EXTI9_5
PA9-PE9 对应的是EXTI9_5
PA10-PE10 对应的是EXTI15_10
PA15-PE15 对应的是EXTI15_10

例子:

GPIO_InitTypeDef GPIO_InitStructur;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);   //使能中断所需AFIO时钟
 GPIO_InitStructur.GPIO_Pin=GPIO_Pin_11;   
 GPIO_InitStructur.GPIO_Mode=GPIO_Mode_Out_PP;
 GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOD,&GPIO_InitStructur);
 GPIO_SetBits(GPIOD,GPIO_Pin_11);
 
 GPIO_InitStructur.GPIO_Pin=GPIO_Pin_10;    //中断引脚
 GPIO_InitStructur.GPIO_Mode=GPIO_Mode_IPD;
 GPIO_InitStructur.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOD,&GPIO_InitStructur);
 
 
 EXTI_InitTypeDef EXTI_InitStructure;   
 EXTI_ClearITPendingBit(EXTI_Line10);   //清除对应的外部中断线对应标志位
 GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource10);//中断io配置
 EXTI_InitStructure.EXTI_Line= EXTI_Line10;  //根据引脚设定外部中断线
 EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; //中断模式
 EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//上升沿
 EXTI_InitStructure.EXTI_LineCmd=ENABLE;  //中断线使能
 EXTI_Init(&EXTI_InitStructure);
 
NVIC_InitTypeDef NVIC_InitStructure;   
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  //中断优先级组
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //中断io对应的通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //响应优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //中断通道使能
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler()                      //中断服务程序
{
 
 
 if (EXTI_GetITStatus(EXTI_Line11) != RESET)
         EXTI_ClearITPendingBit(EXTI_Line10);  //清除中断标志位
}

注意在usos中不需要配置NVIC,一般只需要用ucos中的两个函数代替

BSP_IntVectSet(BSP_INT_ID_EXTI15_10(中断IO对应通道),中断处理函数);
BSP_IntEn(BSP_INT_ID_EXTI15_10); //使能对应的中断通道
例如:

void KeyIntCfg(void)
{
	EXTI_InitTypeDef EXTI_InitStruct;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource0);

	//INT
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOE, &GPIO_InitStructure);	
	
	EXTI_InitStruct.EXTI_Line = EXTI_Line0;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;

	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	
	EXTI_Init(&EXTI_InitStruct);
	
	BSP_IntVectSet(BSP_INT_ID_EXTI0, Key_ISRHandler);
	BSP_IntEn(BSP_INT_ID_EXTI0);
}

void Key_ISRHandler(void)
{
	OSQPost(Msg_Key_Proc,(void *)(CommonKeyOn));
	EXTI_ClearITPendingBit(EXTI_Line0);
}

你可能感兴趣的:(STM32,ucos,stm32,单片机,嵌入式硬件)