2013-05-02 02:25 AM
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 Wynand2013-05-02 06:45 AM
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. Mike2013-05-02 08:29 AM
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 Wynand2013-05-02 08:38 AM
Cool glad I could help. Now if someone could only help me with my CAN problem.
2013-05-03 04:23 AM
Sorry, never used CAN before.
2013-11-06 02:13 AM
2016-06-25 02:23 AM