近期在使用一块STM32的板子的时候,用库函数和外部中断来接收霍尔传感器信号,配置了很久,就是进不去外部中断。
IO口的初始化函数如下:
void Hall_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能PORTA,PORTC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能HALL所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
/* 配置Hall接口IO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//HALL信号检测端口
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//清空中断标志
EXTI_ClearITPendingBit(EXTI_Line7); //清除中断标志位
EXTI_ClearITPendingBit(EXTI_Line8); //清除中断标志位
EXTI_ClearITPendingBit(EXTI_Line9); //清除中断标志位
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7 | GPIO_PinSource8 | GPIO_PinSource9 );
EXTI_InitStructure.EXTI_Line=EXTI_Line7 | EXTI_Line8 | EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
/* I/O线中断,中断线为PC7、PC8、PC9 */
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line7) != RESET) //确保是否产生了EXTI Line中断
{
EXTI_ClearITPendingBit(EXTI_Line7); //清除中断标志位
}
if(EXTI_GetITStatus(EXTI_Line8) != RESET) //确保是否产生了EXTI Line中断
{
EXTI_ClearITPendingBit(EXTI_Line8); //清除中断标志位
}
if(EXTI_GetITStatus(EXTI_Line9) != RESET) //确保是否产生了EXTI Line中断
{
EXTI_ClearITPendingBit(EXTI_Line9); //清除中断标志位
}
LED1=!LED1;//每进一次外部中断,LED1切换一次
}
上述初始化代码,咋一看,没发现啥问题,可就是进不了中断。
后来发现是这一语句的问题:
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7 | GPIO_PinSource8 | GPIO_PinSource9 );
通过GPIO_PinSource的定义,我们可以看到:
#define GPIO_PinSource0 ((uint8_t)0x00)
#define GPIO_PinSource1 ((uint8_t)0x01)
#define GPIO_PinSource2 ((uint8_t)0x02)
#define GPIO_PinSource3 ((uint8_t)0x03)
#define GPIO_PinSource4 ((uint8_t)0x04)
#define GPIO_PinSource5 ((uint8_t)0x05)
#define GPIO_PinSource6 ((uint8_t)0x06)
#define GPIO_PinSource7 ((uint8_t)0x07)
#define GPIO_PinSource8 ((uint8_t)0x08)
#define GPIO_PinSource9 ((uint8_t)0x09)
#define GPIO_PinSource10 ((uint8_t)0x0A)
#define GPIO_PinSource11 ((uint8_t)0x0B)
#define GPIO_PinSource12 ((uint8_t)0x0C)
#define GPIO_PinSource13 ((uint8_t)0x0D)
#define GPIO_PinSource14 ((uint8_t)0x0E)
#define GPIO_PinSource15 ((uint8_t)0x0F)
这里跟GPIO口的初始化就很大不同了,GPIO口初始化,你可以取或后进行赋值,但是现在:
GPIO_PinSource7 | GPIO_PinSource8 | GPIO_PinSource9 =0x07|0x08|0x09=0x0F
所以,你能还初始化到正确的外部中断吗?!
正确的初始化函数如下:
void Hall_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能PORTA,PORTC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能HALL所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
/* 配置Hall接口IO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//HALL信号检测端口
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//清空中断标志
EXTI_ClearITPendingBit(EXTI_Line7); //清除中断标志位
EXTI_ClearITPendingBit(EXTI_Line8); //清除中断标志位
EXTI_ClearITPendingBit(EXTI_Line9); //清除中断标志位
//老老实实一行一行来
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7 );
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource8 );
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource9 );
EXTI_InitStructure.EXTI_Line=EXTI_Line7 | EXTI_Line8 | EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
这样初始化就能正常进入外部中断了。