cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103R8T6 Timer4 Input Capture problem

gambit
Associate II
Posted on March 24, 2014 at 11:17

Hi!

There is a problem with Timer4 Input Capture use.  Initially everything was developed under the STM32L100R8T6 controler, but so happened that it was necessary to pass to STM32F103R8T6. 

This code perfectly works at STM32L100:

  RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);

  TIM_TimeBaseStructInit (& timer);

  timer.TIM_Prescaler = 72 - 1;

  timer.TIM_Period = 65535;

  TIM_TimeBaseInit (TIM4, & timer);

  TIM_ICInitTypeDef timICStruct;

  timICStruct.TIM_Channel = TIM_Channel_1;

  timICStruct.TIM_ICPolarity = TIM_ICPolarity_BothEdge;

  timICStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;

  timICStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  timICStruct.TIM_ICFilter = 0x08;

  TIM_ICInit (TIM4, & timICStruct);

  TIM_ITConfig (TIM4, TIM_IT_CC1, ENABLE);

  TIM_Cmd(TIM4, ENABLE);

 

On STM32F103 this code doesn't catch both Edges, works similarly with RisingEdge.

Help to solve a problem, or describe how ''on the fly'' to change Edges.

Thanks in advance.
13 REPLIES 13
zzdz2
Associate II
Posted on March 25, 2014 at 18:10

I think the problem is that in F1 series only advanced timer can catch both edges, in general timer it's only single polarity.

Posted on March 25, 2014 at 18:48

TIM_ICPolarity_BothEdge isn't defined in V3.3.0

In V3.5.0 it will assert if used on TIM1, TIM8, TIM2, TIM3, TIM4, TIM5

Configure CH1 for the rising edge, and CH2 for the falling edge indirect.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 25, 2014 at 19:13

  RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);

  TIM_TimeBaseStructInit (& timer);

  timer.TIM_Prescaler = 72 - 1;

  timer.TIM_Period = 65535;

  TIM_TimeBaseInit (TIM4, & timer);

  TIM_ICInitTypeDef timICStruct;

  timICStruct.TIM_Channel = TIM_Channel_1;

  timICStruct.TIM_ICPolarity = TIM_ICPolarity_RisingEdge;

  timICStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;

  timICStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  timICStruct.TIM_ICFilter = 0;

  TIM_ICInit(TIM4, &timICStruct);

  timICStruct.TIM_Channel = TIM_Channel_2;

  timICStruct.TIM_ICPolarity = TIM_ICPolarity_FalingEdge;

  timICStruct.TIM_ICSelection = TIM_ICSelection_IndirectTI; // CH1

  timICStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  timICStruct.TIM_ICFilter = 0;

  TIM_ICInit(TIM4, &timICStruct);

  TIM_ITConfig(TIM4, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);

  TIM_Cmd(TIM4, ENABLE);

 

void TIM4_IRQHandler(void)

{

  uint16_t CurCapt;

  if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)

  {

    TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);

    curCapt = TIM_GetCapture1(TIM4);

    Capt = curCapt - oldCapt;

    oldCapt = curCapt;

  }

  if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)

  {

    TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);

    curCapt = TIM_GetCapture2(TIM4);

    Capt = curCapt - oldCapt;

    oldCapt = curCapt;

  }

}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gambit
Associate II
Posted on March 26, 2014 at 10:39

Thank you Clive !

Your code works perfectly. Small inaccuracy, I have no TIM_ICPolarity_RisingEdge, TIM_ICPolarity_FalingEdge. They are called as TIM_ICPolarity_Rising, TIM_ICPolarity_Falling.

Thanks once again!