Skip to main content
amitpatil
Associate
June 20, 2019
Question

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

  • June 20, 2019
  • 3 replies
  • 974 views

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

This topic has been closed for replies.

3 replies

waclawek.jan
Super User
June 20, 2019

Your timer does not run:

> sTIM_TimeBaseInitStruct.TIM_Period = 0;

JW

amitpatil
amitpatilAuthor
Associate
June 20, 2019

Thanks,

I have changed this to

sTIM_TimeBaseInitStruct.TIM_Period = 65535;

still interrupt is not generated...

waclawek.jan
Super User
June 20, 2019

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