AnsweredAssumed Answered

Timer in PWM Capture Mode

Question asked by p.sorean on Nov 23, 2015
Latest reply on Apr 7, 2016 by Clive One
Hello every One

I am trying to read PWM signal parameters (Frequency & Duty Cycle) with timer 2 of STM32f4 IC. According to data sheet There is two modes of reading PWM signal.
1- capture mode which reads Pulse widths
2- Pairing Mode with use of two ICs configuration.

Since I am not expert in working with stm32f4, I provided the code mentioned below to read Frequency & Duty Cycle, with the help of other codes written in this forum.
First of all I want to know : Is that possible to read Duty Cycle with the first mode or not, and if yes how it should be done.?!

Secondly when i run the below code I can not read frequency and Duty Cycle as correct as should be. The entrance signal has the frequency of 45 Hz and a duty of 40 percent. \
I think the problem is with the time base configurations in which the prescaler and period should be defined. 

I will be Please if someone can help me to overcome above issues.
Thanks.
Soren








void TIM2_IRQHandler(void)
{
 
 
     RCC_ClocksTypeDef RCC_Clocks;
     RCC_GetClocksFreq(&RCC_Clocks);
     
     if (TIM_GetITStatus(TIM2, TIM_IT_CC1)!= RESET)
    {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
    IC2Value_ = TIM_GetCapture1(TIM2);
     
if (IC2Value_ != 0) {
     
        DutyCycle_ = (TIM_GetCapture2(TIM2) * 100) / IC2Value_;
        Frequency_ = (RCC_Clocks.HCLK_Frequency)/2 / IC2Value_;
     
    } else {
            /* Reset data */
            DutyCycle_ = 0;
            Frequency_ = 0;
        }
  }
}
 
 
    /* --------------------------------------PWM Input --------------------------------------*/
  TIM_ICInitTypeDef TIM_ICInitStruct;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//      /* --------------------------------------Timer2--------------------------------------*/
 
 
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
   
    /* GPIOC clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
   
    /* TIM2 GPIO pin configuration : CH1=PA0, C2=PA1, CH3=PB10, CH4=PB11 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
  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_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11;
    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_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    /* Connect pins to TIM3 AF2 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2 );
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2 );
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2 );
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_TIM2 );
   
    /* Intrupt Config */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
     
 
 
  /* Time base configuration - SystemCoreClock = 168000000 for 168 MHz board ABP1 @ 42 MHz (DIV4) */
  TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (((SystemCoreClock / 1000000) / 2) - 1); // Shooting for 1 MHz, (1us)
  TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    /* Enable capture*/
  TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
  TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStruct.TIM_ICFilter = 0;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
    TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStruct.TIM_ICFilter = 0;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;
  TIM_ICInit(TIM2, &TIM_ICInitStructure);
   
    TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
   
//  /* Select the TIM2 Input Trigger: TI2FP2 */
//  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
     
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
 
 
//  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
     
    /* Enable TIM2 */
  TIM_Cmd(TIM2, ENABLE);
   
  /* Enable CC1-4 interrupt */
  TIM_ITConfig(TIM2,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3| TIM_IT_CC4, ENABLE);

Outcomes