cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 PWM Input

wynand
Associate II
Posted on May 02, 2013 at 11:25

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
6 REPLIES 6
Maddi.Mike
Associate II
Posted on May 02, 2013 at 15:45

Hi Wynand,

I don't know if this will help but I have used TIM2 for measuring frequency but I don't use the library calls. I have coded my own. However, in your code I don't see where you are zeroing the counters. The counters do not zero themselves. So in my interrupt function I do

TIM2->CNTRH = 0

TIM2->CNTRL = 0

Perhaps you need to do something similar for TIM1?

I hope that helps.

Mike

wynand
Associate II
Posted on May 02, 2013 at 17:29

Thank you Mike, zeroing the counters did help. I was under the impression that if TIM1_SMCR.SMS is set to 100 the counter will reset on rising edge of the trigger. But it does not happen you are 100% correct.

Thank you for the help.

Regards

Wynand

Maddi.Mike
Associate II
Posted on May 02, 2013 at 17:38

Cool glad I could help. Now if someone could only help me with my CAN problem.

wynand
Associate II
Posted on May 03, 2013 at 13:23

Sorry, never used CAN before.

kolagani_r
Associate
Posted on November 06, 2013 at 11:13

Hi Wynand,

I am Ramana. I am trying to measure PWM input signal measurement.

I am planning to use your example. You said your program is working after zeroing the counters. Can you send me your working code. It may be helpful for me

Regards

Ramana 

Posted on June 25, 2016 at 11:23