利用STM32F407的编码器接口去测量两个电机的转速,电机编码器信息如下:
现在的问题是:用定时器2的编码器接口测量A电机没问题,能够采集到转速;但是用定时器3的编码器接口采集B电机,始终采集不到转速,已经排除硬件问题!按理说这两个代码应该是一样的呀!?不知道为什么不好使,请大神指点!/**************************************************************************函数功能:把TIM2初始化为编码器接口模式入口参数:无返回 值:无**************************************************************************/void Encoder_Init_TIM2(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器2的时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PA端口时钟
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //端口配置 GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOBA
/*复用*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0;//8400-1;//0x0; //预分频器 TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择时钟分频:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0X01 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0X02; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器*/
//使用编码器模式3 TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 10; TIM_ICInit(TIM2, &TIM_ICInitStructure);
//清除TIM的更新标志位 TIM_ClearFlag(TIM2, TIM_FLAG_Update); //允许更新中断 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//Reset counter TIM_SetCounter(TIM2,0); TIM_Cmd(TIM2, ENABLE); }/**************************************************************************函数功能:把TIM3初始化为编码器接口模式入口参数:无返回 值:无**************************************************************************/void Encoder_Init_TIM3(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能PB端口时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3的时钟
GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//端口配置 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB
/*复用*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3);
TIM_DeInit(TIM3); TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //预分频器 TIM_TimeBaseStructure.TIM_Period = 0xffff;//ENCODER_TIM_PERIOD;//设定计数器自动重装值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //选择时钟分频:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);/* NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0X01 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0X02; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器*/ //使用编码器模式3 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 6; TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);//清除TIM的更新标志位 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
//Reset counter TIM_SetCounter(TIM3,0); TIM_Cmd(TIM3, ENABLE); }
一周热门 更多>