AnsweredAssumed Answered

TIM1 PWM Input

Question asked by weyers.wynand on May 2, 2013
Latest reply on Jun 25, 2016 by Sisto Zànier
Dear All,

I am trying to measure the frequency (and duty cycle) of a 1 kHz to 2 kHz signal which is connected to PD4 using TIM1 capture capability. As far as I can see from the documentation PD4 is connected to TIM1.IC2 which is the default routing. I have setup TIM1 as follows:

/* Init TIMER 1 */
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);
  // configure TIM1 channel 1 to capture a PWM signal
  TIM1_PWMIConfig(TIM1_Channel_2, TIM1_ICPolarity_Rising, TIM1_ICSelection_DirectTI,
                  TIM1_ICPSC_DIV8, ICFilter);
  // Select the TIM1 Input Trigger: TI1FP1
  TIM1_SelectInputTrigger(TIM1_TRGSelection_TI1FP1);
  TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset);
  // Enable CC1 interrupt request
  TIM1_ITConfig(TIM1_IT_CC2, ENABLE);
  // Enable TIM1
  TIM1_Cmd(ENABLE);
 
  enableInterrupts();

The interrupt handler executes the following code:

INTERRUPT_HANDLER(TIM1_CC_IRQHandler,24)
{
  /* Get the Input Capture value by reading CCR1 register */
  /* CCR1 regsiter contains signal frequency value */
  IC1Value = TIM1_GetCapture2();

  if (IC1Value != 0)
  {
    /* Get the Input Capture value by reading CCR2 register */
    /* CCR2 regsiter contains how much time the signal remained at high level */
    IC2Value = TIM1_GetCapture2();

    /* Duty cycle computation */
    SignalDutyCycle = ((uint32_t) IC2Value * 100) / IC1Value;

    /* Frequency computation */
    SignalFrequency = (uint32_t) (CLK_GetClockFreq() / IC1Value);
  }
  else
  {
    SignalDutyCycle = 0;
    SignalFrequency = 0;
  }

  /* Clear TIM1 Capture compare interrupt pending bit */
  TIM1_ClearITPendingBit(TIM1_IT_CC2);

TIM1_ClearFlag(TIM1_FLAG_CC1);
}

When the input signal is applied to PD4 the values in TIM1->CCR1L, TIM1->CCR1H, TIM1->CCR2L and TIM1->CCR2H changes all the time as if the counter is rolling over or does not get reset. Could someone please help me understand the implementation of the TIM1 capture function better and also tell me what is wrong with the above implementation.

I would also like to route different ports to the capture input, but was not successful in loading register RI->IC2CS with any values?

Thank you for any help
Wynand

Outcomes