cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure Timer for STM8s as input capture compare and how to get the capture compare values in interrupt handler.?

Tk.2
Associate

I have configured the timer as below is its is correct and also in interrupt handler I am not getting time the values.

static void TIM3_Config(void)

{

 /* TIM3 configuration:

  - TIM3CLK is set to 16 MHz, the TIM2 Prescaler is equal to 16 so the TIM2 counter

  clock used is 16 MHz / 16 = 1 000 000 Hz

 - With 1 000 000 Hz we can generate time base:

   max time base is 256us if TIM2_PERIOD = 255 --> (255 + 1) / 1 000 000 = 256us

   min time base is 1us  if TIM2_PERIOD = 1  --> ( 1 + 1) / 1 000 000 = 2us

 - In this example we need to generate a time base equal to 10us

  so TIM2_PERIOD = (0.000010 * 1000000 - 1) = 9 */

  

 /* Time base configuration */

 //TIM3_DeInit();//me 

 //TIM3_TimeBaseInit(TIM3_PRESCALER_16 , 9);

  

 /* Config channel1 as input capture */

 //TIM3_ICSELECTION_TRGI

 TIM3_ICInit(TIM3_CHANNEL_2,TIM3_ICPOLARITY_RISING,TIM3_ICSELECTION_DIRECTTI,TIM3_ICPSC_DIV8, 0);//TIM3_CHANNEL_1//TIM3_ICPSC_DIV2

  

 /* Clear TIM2 update flag */

 TIM3_ClearFlag(TIM3_FLAG_CC2);//TIM3_FLAG_CC1

 /* Enable update interrupt */

 TIM3_ITConfig(TIM3_FLAG_CC2, ENABLE);//TIM3_IT_CC1

 //------------------------------------------------------------

 TIM3_UpdateRequestConfig(TIM3_UPDATESOURCE_REGULAR);//UPDATED

// TIM3->IER &= ~TIM3_IER_CC2IE;//Disable OC Compare

// TIM3->SR1 &= ~TIM3_SR1_CC2IF;//Clear Capture comapre flag

 TIM3->IER |= TIM3_IER_CC2IE;//Enable Capture Compare

 //-------------------------------------------------------------

 TIM3_ClearITPendingBit(TIM3_FLAG_UPDATE);//added//TIM3_IT_CC2//TIM3_IT_UPDATE //TIM1_ClearFlag(TIM1_FLAG_CC1 | TIM1_FLAG_UPDATE);

 /* enable interrupts */

 enableInterrupts();

 /* Enable TIM2 */

 TIM3_CCxCmd(TIM3_CHANNEL_2, ENABLE);

 //TIM3_GenerateEvent(TIM3_EVENTSOURCE_CC2);

 TIM3_Cmd(ENABLE);

}

///------------------------Interrupt Handelr--------------------

if((TIM3->SR1 & TIM3_FLAG_CC2)!=0)//sr1

 {   

  if(numberofcapture==0)

  {

    t1 = TIM3_GetCapture2();

    // count_ms1=TIM3_GetCapture2();   //First Capture

    numberofcapture=1;

    // TIM3_ClearFlag(TIM3_FLAG_CC2);

  }

  else 

  {

    t2 = TIM3_GetCapture2();      //Second Capture

    numberofcapture=0;

     

  }

  TIM3_ClearFlag(TIM3_FLAG_CC2);

  count_ms1 = ((t2 - t1));

}

}

Is this is correct to configure the timer to get the values as capture compare..?

----------------------------------Please Help------------------------------

1 REPLY 1
JAhme.2
Associate

Did you resolve this issue ?