cancel
Showing results for 
Search instead for 
Did you mean: 

the doubt in M4-Discovery input capture library

0owarrioro0
Associate II
Posted on April 19, 2012 at 19:43

like this: I don't know why selecting opposite polarity !

/* Select the Opposite Input Polarity */

  if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)

  {

    icoppositepolarity = TIM_ICPolarity_Falling;

  }

  else

  {

    icoppositepolarity = TIM_ICPolarity_Rising;

  }

wait for help .

thanks in advance!

#great?thanks-! #wait-for-you--clive1-!-!-! #thanks-for-your-help-!-! #great?thanks-! #great?thanks-! #the-doubt-in-m4-discovery-input #haha #wait-for-you #xie-xie-ni-clivel1-!!-thanks-!
12 REPLIES 12
Posted on April 19, 2012 at 20:16

Doesn't it related to setting the paired channel with the reverse/opposite conditions.

TI1_Config vs TI2_Config

/**

* @brief Configures the TIM peripheral according to the specified parameters

* in the TIM_ICInitStruct to measure an external PWM signal.

* @param TIMx: where x can be 1, 2, 3, 4, 5,8, 9 or 12 to select the TIM

* peripheral.

* @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure that contains

* the configuration information for the specified TIM peripheral.

* @retval None

*/

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_LIST2_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);

}

else

{

/* 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);

}

}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
0owarrioro0
Associate II
Posted on April 20, 2012 at 15:45

Hello!

I still have something with problem . I don't comprehend which specific pins is set to capture the external PWM ,for example ''IC1PS'' in TIM1 , and which register is set to store the value when in input capture mode .

like this:

int main(void)

{

  /*!< At this stage the microcontroller clock setting is already configured, 

       this is done through SystemInit() function which is called from startup

       file (startup_stm32f4xx.s) before to branch to application main.

       To reconfigure the default setting of SystemInit() function, refer to

       system_stm32f4xx.c file

     */

       

  /* TIM Configuration */

  TIM_Config();

  /* TIM4 configuration: PWM Input mode ------------------------

     The external signal is connected to TIM4 CH2 pin (PB.07), 

     The Rising edge is used as active edge,

     The TIM4 CCR2 is used to compute the frequency value 

     The TIM4 CCR1 is used to compute the duty cycle value

  ------------------------------------------------------------ */

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);

  /* Select the TIM4 Input Trigger: TI2FP2 */

  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */

  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */

  TIM_Cmd(TIM4, ENABLE);

  /* Enable the CC2 Interrupt Request */

  TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

  while (1);

}

/**

  * @brief  Configure the TIM4 Pins.

  * @param  None

  * @retval None

  */

void TIM_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM4 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* GPIOB clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  

  /* TIM4 chennel2 configuration : PB.07 */

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  

  /* Connect TIM pin to AF2 */

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

  /* Enable the TIM4 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

void TIM4_IRQHandler(void)

{

  RCC_ClocksTypeDef RCC_Clocks;

  RCC_GetClocksFreq(&RCC_Clocks);

  /* Clear TIM4 Capture compare interrupt pending bit */

  TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);

  /* Get the Input Capture value */

  IC2Value = TIM_GetCapture2(TIM4); //be used for measure pulse width

  if (IC2Value != 0)

  {

    /* Duty cycle computation */

    DutyCycle = (TIM_GetCapture1(TIM4) * 100) / IC2Value;

    /* Frequency computation 

       TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */

    Frequency = (RCC_Clocks.HCLK_Frequency)/2 / IC2Value;

  }

  else

  {

    DutyCycle = 0;

    Frequency = 0;

  }

}

in these contents , it's only set TIM4_CH2--PB7 for capturing . But in the interrupt function it uses ''DutyCycle = (TIM_GetCapture1(TIM4) * 100) / IC2Value;''  for duty value ,however ''TIM4_CH1'' and ''TIM4_CH2'' at different pins . Please help me solve these problem !

Thanks inadvance!   

Posted on April 20, 2012 at 17:12

You should review the block diagrams in the manual.

Timer channels 1 & 2 pair, and 3 & 4 pair. You can route CH2_TIM input into the CCR1 counter/latch. Look at the muxes to see what/how the signals route.

The CCR2 register latches the ticks of the timebase, get two measurements out of CCR2 the delta ticks provides the period. CCR1 gets you the ability to determine the duty.

Say your timebase is 72 MHz

at first interrupt CCR2 = 1000

at second interrupt CCR2 = 2000

These 1000 ticks between the two interrupts mean you've measured 72 KHz at PB7 (TIM4_CH2)
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
0owarrioro0
Associate II
Posted on April 21, 2012 at 05:24

hello!

I have read the M-R . But I have some doubt in it .

like the figure 65 in page 294 I don't understand the relation among CH1/CH2/CH3 .

Whether CH1/CH2 is combined or CH1/CH3 is combined or CH1/CH2/CH3 is combined .

0owarrioro0
Associate II
Posted on April 21, 2012 at 05:25

hello!

I have read the M-R . But I have some doubt in it .

like the figure 65 in page 294 I don't understand the relation among CH1/CH2/CH3 .

Whether CH1/CH2 is combined or CH1/CH3 is combined or CH1/CH2/CH3 is combined .

0owarrioro0
Associate II
Posted on April 21, 2012 at 05:27

hello!

I have read the M-R . But I have some doubt in it .

like the figure 65 in page 294 I don't understand the relation among CH1/CH2/CH3 .

Whether CH1/CH2 is combined or CH1/CH3 is combined or CH1/CH2/CH3 is combined .

0owarrioro0
Associate II
Posted on April 21, 2012 at 05:28

hello!

I have read the M-R . But I have some doubt in it .

like the figure 65 in page 294 I don't understand the relation among CH1/CH2/CH3 .

Whether CH1/CH2 is combined or CH1/CH3 is combined or CH1/CH2/CH3 is combined .

0owarrioro0
Associate II
Posted on April 21, 2012 at 05:29

0owarrioro0
Associate II