新手入门
- 积分
- 26
- 金钱
- 26
- 注册时间
- 2014-9-1
- 在线时间
- 0 小时
|
5金钱
为什么用stm32控制左右电机,用中断计编码器传来的脉冲数,只有一边有脉冲数,另一边没有???
用示波器能检测到信号啊!为什么只有一边加一呢???
void _EXTI4_5_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //选择中断分组2
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //选择中断通道0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line4); //选择中断管脚PC.2 PC.3 PC.5
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4; //选择中断线路0
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断使能
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //选择中断分组2
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //选择中断通道0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line5); //选择中断管脚PC.2 PC.3 PC.5
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line = EXTI_Line5; //选择中断线路0
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置中断触发方式为上下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断使能
EXTI_Init(&EXTI_InitStructure);
}
void EXTI4_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line4)==1)
{
EXTI_ClearITPendingBit(EXTI_Line4);
motor_left_rev++;
printf("motor_left_rev:%d \r\n",motor_left_rev);
}
}
void EXTI9_5_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line5)==1)
{
EXTI_ClearITPendingBit(EXTI_Line5);
motor_right_rev++;
printf("motor_right_rev:%d \r\n",motor_right_rev);
}
}
|
|