增量式正交编码器能测角度吗?

2019-08-16 23:46发布

我用stm32编码器模式测角度,每次回原点读数都不为0,附上代码,请问是我程序的问题?还是增量式编码器本身就测不了角度?[mw_shl_code=applescript,true]extern u16 count1;
extern u16 count2;
//编码器1初始化,使用定时器3
void coder1_init()
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
        
        GPIO_InitTypeDef GPIO_Structure;
        GPIO_Structure.GPIO_Mode=GPIO_Mode_AF;
        GPIO_Structure.GPIO_OType=GPIO_OType_PP;
        GPIO_Structure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
        GPIO_Structure.GPIO_PuPd=GPIO_PuPd_UP;
        GPIO_Structure.GPIO_Speed=GPIO_High_Speed;
        GPIO_Init(GPIOB,&GPIO_Structure);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3);
        
        NVIC_InitTypeDef NVIC_Structure;
        NVIC_Structure.NVIC_IRQChannel=TIM3_IRQn;
        NVIC_Structure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Structure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_Structure.NVIC_IRQChannelSubPriority=0;
        NVIC_Init(&NVIC_Structure);
        
        TIM_DeInit(TIM3);
        TIM_TimeBaseInitTypeDef TIM3_Structure;
        TIM3_Structure.TIM_ClockDivision=TIM_CKD_DIV1;
        TIM3_Structure.TIM_CounterMode=TIM_CounterMode_Up;
        TIM3_Structure.TIM_Period=399*4;//400线编码器
        TIM3_Structure.TIM_Prescaler=0;
        TIM_TimeBaseInit(TIM3,&TIM3_Structure);
        
        TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge);
        
        TIM_ICInitTypeDef IC_Structure;
        TIM_ICStructInit(&IC_Structure);
        IC_Structure.TIM_ICFilter=6;
        TIM_ICInit(TIM3,&IC_Structure);
        
        TIM_ClearFlag(TIM3, TIM_FLAG_Update);
        TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
        
        TIM3->CNT = 800;//设置一个基数,防止出现负值
        
        TIM_Cmd(TIM3, ENABLE);
}
//定时器中断,读取角度
void TIM1_UP_TIM10_IRQHandler()
{
//        if(TIM_GetITStatus(TIM1,TIM_IT_Update)==SET)
                count1=TIM3->CNT;
               
        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
15条回答
ljfc
2019-08-17 20:17
void ENC_Init(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;
  
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
   
  /* TIM2 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Enable GPIOA, clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  
  GPIO_StructInit(&GPIO_InitStructure);
  /* Configure PA.00,01 as encoder input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Enable the TIM2 Update Interrupt */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_IRQChannelPreemptionPriority_ENC;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_IRQChannelSubPriority_ENC;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* Timer configuration in Encoder mode */
  TIM_DeInit(ENCODER_TIMER);
  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(ENCODER_TIMER, &TIM_TimeBaseStructure);

  TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12,
                             TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
        TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER;
  TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
  
// Clear all pending interrupts
  TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
  TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM2->CNT = COUNTER_RESET;
  
  TIM_Cmd(ENCODER_TIMER, ENABLE);
}

这是我在伺服中的初始化,跟你还是有点差异的。你自己改改咯

一周热门 更多>