cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 1 as capture input for frequency measurement, not getting Capture interrupt

amitpatil
Associate

I want to use Timer 1 as capture input for frequency measurement: ( STM32F207ZG)

void STMF2Timer_Configure(

       void)

{

   TIM_TimeBaseInitTypeDef sTIM_TimeBaseInitStruct;

   TIM_ICInitTypeDef sTIM_ICInitTypeDef;

   NVIC_InitTypeDef NVIC_InitStructure; 

  //! - TIM1 setup ------------------------------------------------------  

 //! Setup interrupt for TIM1.

   NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;            // Set config for TIM1_IRQ

   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = STMF2PLATFORMCFG_TIM1_PREEMPTION_PRIORITY;  //configLIBRARY_KERNEL_INTERRUPT_PRIORITY - 1;

   NVIC_InitStructure.NVIC_IRQChannelSubPriority = STMF2PLATFORMCFG_TIM1_SUB_PRIORITY;

   NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;            // Enable

   NVIC_Init(&NVIC_InitStructure);                            // ...

   //! Enable clock for the timer TIM1.

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

   TIM_DeInit(TIM1);

   sTIM_ICInitTypeDef.TIM_Channel = TIM_Channel_1;

   sTIM_ICInitTypeDef.TIM_ICPolarity = TIM_ICPolarity_Rising;

   sTIM_ICInitTypeDef.TIM_ICSelection = TIM_ICSelection_DirectTI;

   sTIM_ICInitTypeDef.TIM_ICPrescaler = TIM_ICPSC_DIV1;

   sTIM_ICInitTypeDef.TIM_ICFilter = 0;

   TIM_ICInit(TIM1,&sTIM_ICInitTypeDef);

   sTIM_TimeBaseInitStruct.TIM_Prescaler = 99; // Period is frequency/(prescaler+1).

   sTIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

   sTIM_TimeBaseInitStruct.TIM_Period = 0;

   sTIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;

   sTIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;

   TIM_TimeBaseInit(TIM1,&sTIM_TimeBaseInitStruct);

   //! Enable TIM1event interrupt generation.

   TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);

   TIM_Cmd(TIM1, ENABLE);

}

/*! \brief Handles the timer TIM1 capture interrupt

 *

 * \return None

 *

 * \pre STMF2Timer_Configure() has been successfully called.

 */

void TIM1_CC_IRQHandler(

       void)

{

   Uint32 u32NewTimeStampus  = 0u;

   Uint32 u32LastTimeStampus = 0u;

   Uint32 u8captureNumber    = 0u;

/*   Uint8 u8Port = 'G';

   Uint32 u32Pin = 1;

   //! Configure the pin as output

   STMF2GPIO_ConfigurePin(u8Port, (Uint16) u32Pin, OUTPUT_PUSHPULL);

   // Set pin

   STMF2GPIO_SetPin(u8Port, (Uint16) u32Pin);

*/

   if(TIM_GetFlagStatus(TIM1,TIM_FLAG_CC1) == 1)

   {

       TIM_ClearFlag(TIM1,TIM_FLAG_CC1);

       if(u8captureNumber == 0)

       {

           u32NewTimeStampus = TIM_GetCapture1(TIM1); //The first capture

           u32LastTimeStampus = u32NewTimeStampus;

           u8captureNumber = 1;

       }

       else if(u8captureNumber == 1) //Treatment of second capture

       {

           if(TIM_GetFlagStatus(TIM1,TIM_FLAG_Update) != 1)//Treatment of two capture no overflow occurs

           {

               u32NewTimeStampus = TIM_GetCapture1(TIM1);

               u32AbsTimeStampDiff = u32NewTimeStampus - u32LastTimeStampus; //If the calculation mode update events

           }

           else

           {

               TIM_ClearFlag(TIM1,TIM_FLAG_Update);  //The update event

               u32NewTimeStampus = TIM_GetCapture1(TIM1); //The second capture

               u32AbsTimeStampDiff = 0xFFFF - u32LastTimeStampus + u32NewTimeStampus; //The second capture

           }

       }

   }

   u8captureNumber = 0;

}

I am expecting time stamp diff for further calculating frequency?

but I am not getting it right now

3 REPLIES 3

Your timer does not run:

> sTIM_TimeBaseInitStruct.TIM_Period = 0;

JW

amitpatil
Associate

Thanks,

I have changed this to

sTIM_TimeBaseInitStruct.TIM_Period = 65535;

still interrupt is not generated...

Read out and check the relevant timer and NVIC registers.

Check if capture happens at all, i.e. if the relevant TIMx_CCRy register changes.

Check in disassembly if the ISR's address is properly inserted in the vector table. If you use C++, don't forget about name mangling.

JW