AnsweredAssumed Answered

Issues with TIMER2 for INPUT CAPTURE

Question asked by anton.bogdan on Jul 4, 2015
Latest reply on Jul 5, 2015 by anton.bogdan
Hello all,
i am trying to capture a ppm signal, for the moment i use a steady fixed widith signal of 740hz, with 1050us on period, and 300us off period.

The signal going to PA0 and PA1 of my STM32F407.

I have several questions:

I noticed if i use as trigger TIM_TS_TI1FP1 , the event interupt ocures at the specific frequency of the signal wich i have it on the input of mcu, this somehow overides my timebase period/prescale setings. Is this true?

When performing a readout of the counter register, it is reseted just by reading it?

The counter registers are automaticly cleard when a new rise/fall it event ocurrs?

I am afraid i did not understand exactly the corelation between timer clock and the counting register...

With the code bellow my raw values for the given 740hz signal are
Capture1 = 4430
Capture2 = 1890
Is there a specific formula for calculate the "ON" period in microseconds?





My timer settings are like in the following code

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); //Enable the GPIOx AHB clock
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2  , ENABLE);
 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 
   
   /* Configure Timer2 timebase */
   TIM_TimeBaseStructure.TIM_Prescaler = 9;
   TIM_TimeBaseStructure.TIM_Period = 6999;
   TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  
  
    
    
 /* configure Timer2 Channel 1 */
   TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; /*Capture performed each time an edge is detected on the capture input. */
   TIM_ICInitStructure.TIM_ICFilter = 0x00;
   TIM_ICInit(TIM2, &TIM_ICInitStructure);
    
    
  /* configure Timer2 Channel 2 */
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
        TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
        TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
        TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
        TIM_ICInitStructure.TIM_ICFilter = 0x00;
        TIM_ICInit(TIM2, &TIM_ICInitStructure);
    
  
 /* Configure Timer2 Interupt Sources  */ 
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); // enable Channel 1 capture interupt
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);// enable Channel 2 capture interupt
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //enable interupt events
   
 TIM_SelectInputTrigger(TIM2,TIM_TS_TI1FP1);
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); /* Rising edge of the selected trigger input
 
 
 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
   
TIM_Cmd(TIM2, ENABLE);  // Start the Timer
 
Bellow are the Interupts
 
 
void TIM2_IRQHandler(void)
{
   
     if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == 1)// If compare capture has occured
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
       capture1 = TIM_GetCapture1(TIM2);   // Get timer counts for Period
         
        GPIO_ToggleBits(GPIOD,GPIO_Pin_5); // just some toggle for logic analyzer
        GPIO_ToggleBits(GPIOD,GPIO_Pin_5);
        
     }
   
   
     if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == 1)// If compare capture has occured
    {
      TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
 
      capture2= TIM_GetCapture2(TIM2);
       
     GPIO_ToggleBits(GPIOD,GPIO_Pin_9); // just some toggle for logic analyzer
     GPIO_ToggleBits(GPIOD,GPIO_Pin_9);
       }
   
   
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
         TIM_ClearITPendingBit(TIM2, TIM_IT_Update);    // Counter overflow, reset interrupt
         GPIO_ToggleBits(GPIOD,GPIO_Pin_12);
   }
 }






Outcomes