STM32F4编码器问题

2019-07-20 00:37发布

求大神解答,为何PB6,PB7无法实现编码器接口功能?(寄存器的值一直为0)
void ENC_Init2(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;

/* Encoder unit connected to TIM3, 4X mode */   
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM3 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  /* Enable GPIOA, clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
       

  GPIO_StructInit(&GPIO_InitStructure);
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);       
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;   
        GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Enable the TIM3 Update Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Timer configuration in Encoder mode */
  TIM_DeInit(TIM4);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  // No prescaling
  TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;  
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
  TIM_ICInit(TIM4, &TIM_ICInitStructure);

// Clear all pending interrupts
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
  //Reset counter
//  TIM2->CNT = COUNTER_RESET;

//  ENC_Clear_Speed_Buffer();

  TIM_Cmd(TIM4, ENABLE);  
}
void TIM4_IRQHandler(void)
{  
  /* Clear the interrupt pending flag */
        if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET)
        {
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  }
//  if (hEncoder_Timer_Overflow != U16_MAX)  
//  {
//   hEncoder_Timer_Overflow++;
//  }
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
9条回答
董董
1楼-- · 2019-07-20 04:58
 精彩回答 2  元偷偷看……
正点原子
2楼-- · 2019-07-20 10:35
帮顶
stardust
3楼-- · 2019-07-20 12:53
 精彩回答 2  元偷偷看……
yyfnfqp
4楼-- · 2019-07-20 18:04
 精彩回答 2  元偷偷看……
walker_he
5楼-- · 2019-07-20 22:20
增量式编码器用IO做和中断做都可以,其实没必要用定时器来做。
sky克西
6楼-- · 2019-07-20 23:53
请问楼主问题解决了么  求一份代码

一周热门 更多>