STM32的外部中断开发总结:
中断线路: 0,1,2,3,4,9_5,15_10.
关于优先级:
NVIC_IRQChannelPreemptionPriority, NVIC_IRQChannelSubPriority 数值号越小, 则优先级越高.
主要开发过程:
(1) 打开必要的时钟
void RCC_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE); // 打开GPIOA 和 AFIO时钟
}
(2)配置中断通道
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the Priority Group to 2 bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
/* Enable the EXTI Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
(3)配置GPIO,并将GPIO与中断线连接
void EXTI_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line4; //外部中断线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//中断触发方式
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//打开中断
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4); //选择 GPIO管脚用作外部中断线路
}
(4)中断处理函数
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{
// Do something here
/* Clear the EXTI Line 4 */
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
/*
内容:STM32的外部中断EXTI的开发过程
作者:罗世洲 QQ370756740 xyy0215@qq.com
欢迎技术交流
*/