标签:
背景:项目中需要使用hall传感器检测门锁状态,触发中断,发送门锁状态信息。(这儿中断服务中只是点了led灯说明了状况)
硬件情况:采用PB3管脚,因而使用中断线:EXTI_Line3和中断服务函数:EXTI2_3_IRQn。
具体过程:
1、设置中断时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
2、配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //拉高
GPIO_Init(GPIOB, &GPIO_InitStructure);
3、GPIO与中断线的映射
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource3);
4、配置EXTI
EXTI_InitTypeDef EXTI_InitStructure
EXTI_InitStructure.EXTI_Line = EXTI_Line3; //中断线3
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //边沿触发
EXTI_Init(&EXTI_InitStructure);
5、配置NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_3_IRQn; //顶层头文件重定义好的
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00; //中断优先级
NVIC_Init(&NVIC_InitStructure);
6、中断服务程序及标志位清除
void EXTI2_3_IRQHandler() //启动文件中定义好的
{
if (LOCK_SENSOR == 0)
{
GPIO_SetBits(GPIOF, GPIO_Pin_6);
GPIO_ResetBits(GPIOF, GPIO_Pin_7);
}
if (LOCK_SENSOR == 1)
{
GPIO_SetBits(GPIOF, GPIO_Pin_7);
GPIO_ResetBits(GPIOF, GPIO_Pin_6);
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
STM32外部中断易出错总结(详见链接)
http://blog.csdn.net/yskcg/article/details/8201505
标签:
原文地址:http://www.cnblogs.com/chenfeima/p/5729024.html