AnsweredAssumed Answered

PWM Input capture doesnt work on Ch3&4 pair

Question asked by m.a.sajeed on Jun 24, 2013
Latest reply on Mar 7, 2015 by Clive One
Hello
I am using STM32F05XXX uc and PWM input capture example provided with the standard peripheral library for measuring frequencies of 2 different pulses.Since the example had support only for the input capture using TIM2 channel 1 & 2 pair, I have modified the code to have the input capture done for Channel 3 & 4 pair. The measuring works perfectly for TIM2 Ch1 & 2 pair but gives random values for TIM2 channel 3 & 4 pair. Could you please let me know what might be going wrong or the reason for such a behavior.

Below is the code
/***************************************************************************/
void TIM2_IRQHandler(void)
{
  if (TIM2 ->SR & TIM_IT_CC2 )
  {
    /* Clear TIM2 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

    /* Get the Input Capture value */
    IC2Value = TIM_GetCapture2(TIM2);

    if (IC2Value != 0)
    {
      /* Duty cycle computation */
      DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value;
      /* Frequency computation
         TIM2 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
      ReadFrequency = RCC_Clocks.HCLK_Frequency / IC2Value;
      //TIM2->CNT = 0;
    }
    else
    {
      DutyCycle = 0;
      ReadFrequency = 0;
    }
  }/*If it is ch4 interrupt*/
  if (TIM2 ->SR & TIM_IT_CC4 )
  {
    /* Clear TIM2 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

    /* Get the Input Capture value */
    IC4Value = TIM_GetCapture4(TIM2);

    if (IC4Value != 0)
    {
      /* Duty cycle computation */
      DutyCycle = (TIM_GetCapture3(TIM2) * 100) / IC4Value;
      /* Frequency computation
         TIM2 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
      ReadFrequency = RCC_Clocks.HCLK_Frequency / IC4Value;
      //TIM2->CNT = 0;
    }
    else
    {
      DutyCycle = 0;
      ReadFrequency = 0;
    }
  }/*If it is ch2 interrupt*/
}/*End of interrupt handler*/
/***************************************************************************/

Changes in the file stm32f0xx_tim.c
/***************************************************************************/
void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
{
  uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
  uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
  /* Check the parameters */
  assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  /* Select the Opposite Input Polarity */
  if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
  {
    icoppositepolarity = TIM_ICPolarity_Falling;
  }
  else
  {
    icoppositepolarity = TIM_ICPolarity_Rising;
  }
  /* Select the Opposite Input */
  if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
  {
    icoppositeselection = TIM_ICSelection_IndirectTI;
  }
  else
  {
    icoppositeselection = TIM_ICSelection_DirectTI;
  }
  if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  {
    /* TI1 Configuration */
    TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
               TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
    /* TI2 Configuration */
    TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  }
  if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)  
  {
    /* TI2 Configuration */
    TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
               TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
    /* TI1 Configuration */
    TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  }
  if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
  {
    /* TI1 Configuration */
    TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
               TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
    /* TI2 Configuration */
    TI4_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  }
  if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_4)  
  {
    /* TI2 Configuration */
    TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
               TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
    /* TI1 Configuration */
    TI3_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
    /* Set the Input Capture Prescaler value */
    TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  }
}
/***************************************************************************/

Outcomes