Skip to main content
Palmer Kim
Associate
August 28, 2017
Question

I want to use pwm input capture TIM3 CH1/CH2.

  • August 28, 2017
  • 4 replies
  • 973 views
Posted on August 28, 2017 at 10:52

Dear All,

I want to use pwm input capture GPIOA.6 and GPIOA.7 using TIM3 on STM32F407IG(176pin)

Channel 1 and Channel 2 work's fine if I set just one of them.

But channel 1 and channel 2 does not work if I set at the same time.

void TIM3_IRQHandler(void)

{

#if 1

 if((TIM_GetITStatus(TIM3, TIM_IT_CC1)==SET))

 {

  TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

  GetFrequencyCycleCount(NUM1, TIM_GetCapture1(TIM3));

 }

#endif

#if 2

 if((TIM_GetITStatus(TIM3, TIM_IT_CC2)==SET))

 {

  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

  GetFrequencyCycleCount(NUM2, TIM_GetCapture2(TIM3));

 }

#endif

 }

void TIM3_TimerSet(void)

{

 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  TIM_ICInitTypeDef  TIM_ICInitStructure;

  /* TIM2 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

 

  /* Enable the TIM3 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6|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(GPIOA, &GPIO_InitStructure);

  //GPIO_PinAFConfig(GPIOA, GPIO_PinSource6|GPIO_PinSource7, GPIO_AF_TIM3);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);

 

#if 1

 TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD;

 TIM_TimeBaseStructure.TIM_Prescaler = 84-1;

 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

#endif

#if 1

  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_ICInitStructure.TIM_Channel = TIM_Channel_1;

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

#endif

#if 1

  /* Select the TIM3 Input Trigger*/

  TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1|TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */

  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

#endif

  /* TIM enable counter */

  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */

  TIM_ITConfig(TIM3, TIM_IT_CC1|TIM_IT_CC2, ENABLE);

}

#endif
    This topic has been closed for replies.

    4 replies

    waclawek.jan
    Super User
    August 28, 2017
    Posted on August 28, 2017 at 11:18

    That's because the 'PWM input capture', while using only one input, uses both input channels.

    JW

    Tesla DeLorean
    Guru
    August 30, 2017
    Posted on August 30, 2017 at 03:38

    And in this case also resets the only counting element in the timer.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Palmer Kim
    Associate
    August 30, 2017
    Posted on August 30, 2017 at 02:41

    I want to use CH1 and CH2 in encoder mode and also CH3 in input capture mode, all this using one timer (TIM3).

    How can I set?

    Tesla DeLorean
    Guru
    August 30, 2017
    Posted on August 30, 2017 at 03:40

    There is a single counting element, there should be an encoder example in the SPL, the third channel could be configured in Input Capture mode to latch the encoder count into TIM3->CCR3

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..