STM32 中断使用
扫描二维码
随时随地手机看文章
STM32中断有时候用多了容易乱,特此记录一下,因为之前一直是M3 M0交叉用,固件库有些区别容易弄混,这里说一下M3外部中断的配置
步骤:
1.将对应的IO配置为输入
2.将IO对应的中断的优先等级用NVIC配置好
3.设置好对应IO的中断模式和触发方式
4.将IO所属的中断线设为中断输入源
1---------以PB5为例子
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB, &GPIO_InitStructure);
2-------------
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*USB Insert*/
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
3------------
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
4---------
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5);
第四步比如容易被忽略,因为如果是PA口的话默认是已经作为输入中断源 就因为这点浪费了半天时间。。。
接下来就是中断服务程序了
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5))
{
EXTI_ClearITPendingBit(EXTI_Line5);
}
}