AnsweredAssumed Answered

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

Question asked by Palmer Kim on Aug 28, 2017
Latest reply on Aug 30, 2017 by Palmer Kim

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

Outcomes