为什么TIM2和TIM3测出来的编码器脉冲不一样,就大神帮我看看

2019-07-20 09:44发布

我使用的是探索者的TIM2和TIM3来测试同一个PWM值的电机但是得到的结果是TIM3测试的值是TIM2的3倍左右,不知道为什么求解,要如何调到一致
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
萧萧暮雨
2019-07-20 11:40
[mw_shl_code=applescript,true]void Encoder_Init_TIM2(void)
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
  TIM_ICInitTypeDef TIM_ICInitStructure;  
  GPIO_InitTypeDef GPIO_InitStructure;
       
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//ê1Äü¶¨ê±Æ÷2μÄê±Öó
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüPB¶Ë¿úê±Öó
       
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);//¿aÆôGPIOA0μÄTIM2ê±Öóòy½Å
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);//¿aÆôGPIOA1μÄTIM2ê±Öóòy½Å
       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;        //¶Ë¿úÅäÖÃ
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//ÅäÖö˿úêä3öààDí 0 êä3öíÆí죨¸′λ×′쬣©  1êä3ö¿a©
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;//ÅäÖö˿úéÏà-ÏÂà-μļÄ′æÆ÷£¬00ÎTéÏà-ÏÂà-£¬01éÏà-£¬10 ÏÂà- 11±£áô
  GPIO_Init(GPIOA, &GPIO_InitStructure);                                              //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOA
  
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Ô¤·ÖÆμÆ÷
  TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//Ñ¡Ôñê±Öó·ÖÆμ£o2»·ÖÆμ
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIMÏòéϼÆêy  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
       
  TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ê1óñàÂëÆ÷Ä£ê½3
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = 10;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);//Çå3yTIMμĸüD±ê־λ
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM_SetCounter(TIM2,0);
  TIM_Cmd(TIM2, ENABLE);
}
/**************************************************************************
oˉêy1|Äü£o°ÑTIM33õê¼»ˉÎa±àÂëÆ÷½ó¿úÄ£ê½
èë¿ú2Îêy£oÎT
·μ»Ø  Öμ£oÎT
**************************************************************************/
void Encoder_Init_TIM3(void)
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
  TIM_ICInitTypeDef TIM_ICInitStructure;  
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//ê1Äü¶¨ê±Æ÷3μÄê±Öó
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ê1ÄüPB¶Ë¿úê±Öó
       
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3);//¿aÆôGPIOB4μÄTIM3ê±Öóòy½Å
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3);//¿aÆôGPIOB5μÄTIM3ê±Öóòy½Å
       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;        //¶Ë¿úÅäÖÃ
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//ÅäÖö˿úêä3öààDí 0 êä3öíÆí죨¸′λ×′쬣©  1êä3ö¿a©
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;//ÅäÖö˿úéÏà-ÏÂà-μļÄ′æÆ÷£¬00ÎTéÏà-ÏÂà-£¬01éÏà-£¬10 ÏÂà- 11±£áô
  GPIO_Init(GPIOB, &GPIO_InitStructure);                                              //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB
  
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // Ô¤·ÖÆμÆ÷
  TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//Ñ¡Ôñê±Öó·ÖÆμ£o2»·ÖÆμ
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIMÏòéϼÆêy  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
       
  TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//ê1óñàÂëÆ÷Ä£ê½3
  TIM_ICStructInit(&TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_ICFilter = 10;
  TIM_ICInit(TIM3, &TIM_ICInitStructure);
  TIM_ClearFlag(TIM3, TIM_FLAG_Update);//Çå3yTIMμĸüD±ê־λ
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  //Reset counter
  TIM_SetCounter(TIM3,0);
  TIM_Cmd(TIM3, ENABLE);
}

/**************************************************************************
oˉêy1|Äü£oμ¥λ걼ä¶á衱àÂëÆ÷¼Æêy
èë¿ú2Îêy£o¶¨ê±Æ÷
·μ»Ø  Öμ£oËù¶èÖμ
**************************************************************************/
int Read_Encoder(u8 TIMX)
{
    int Encoder_TIM;   
   switch(TIMX)
         {
           case 2:  Encoder_TIM= (short)TIM2 -> CNT;  TIM2 -> CNT=0;break;
                 case 3:  Encoder_TIM= (short)TIM3 -> CNT;  TIM3 -> CNT=0;break;       
                 case 4:  Encoder_TIM= (short)TIM4 -> CNT;  TIM4 -> CNT=0;break;       
                 default:  Encoder_TIM=0;
         }
                return Encoder_TIM;
}[/mw_shl_code]

一周热门 更多>