使用stm32外部中断循迹发现的问题
在用STM32的外部中断作为红外检测的引脚时, 发现当压住红线时,红外管能返回一个低电平,也能进入中断程序
但遗憾的是中断里边似乎处理不了数据,在想,中断里边是不是只能计数啊,也就是数数,比如配置为下降沿触发的
外部中断,然后再里边计数,最后累计一个下降沿的总数。其他的复杂的程序难道这里边不能够处理吗,废话少说先看源码,mark一下,日后解决
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_7; //将PA4 7 作为外部中断源
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //内不上啦
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA ,GPIO_PinSource4|GPIO_PinSource7); //映射原
EXTI_InitStructure.EXTI_Line = EXTI_Line4|EXTI_Line7;//线 4 7
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中端模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
在中断前套内使能
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
看it。c里的中断处理函数
void EXTI4_IRQHandler(void)
{
u8 waitquit;//超时变量
u8 RES;
//EXTI_ClearFlag(EXTI_Line4);
if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{
if(IR_LEFT_SIG()==F_OK)//左侧是否压线
{
GPIO_SetBits(GPIOB, GPIO_Pin_11);//置高
BLK_flg=1;
P_on=0;
motor_stop();
waitquit=200;
do {
motor_right();//否则拐一个定角度
delay_ms(100);
motor_stop();
RES=Read_Ir_Blk(IR_LEFT);//左侧是否压线
// waitquit--;
delay_ms(100);
}while(!RES&&(waitquit));
}
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
void EXTI9_5_IRQHandler(void)
{
u8 waitquit;//超时变量
u8 RES;
//EXTI_ClearFlag(EXTI_Line7);
if(EXTI_GetITStatus(EXTI_Line7) != RESET)
{
if(IR_RIGHT_SIG() ==F_OK)
{
GPIO_SetBits(GPIOB, GPIO_Pin_11);
BLK_flg=1;
P_on=0;
motor_stop();//
waitquit=200;
do {
motor_left();//否则左拐一个定角度
delay_ms(100);
motor_stop();
RES=Read_Ir_Blk(IR_RIGHT);//左侧是否压线
// waitquit--;
delay_ms(100);
}while((!RES)&&(waitquit));
}
}
EXTI_ClearITPendingBit(EXTI_Line7);
}