AnsweredAssumed Answered

Timer 2 of STM32F4 Discovery don't work !

Question asked by Robert on Mar 2, 2013
Latest reply on Nov 7, 2015 by Clive One
Hello,
In my project i need to measure the frequency of 8 external signals with the STM32F4 Discovery Board. So i begin with the first signal by using Timer 1 in Input Capture mode and it works. Now i'm trying to do this with Timer 2 by applying the same configuration but it don't work.  
The code of Timer 1 is the following:
                                   
#include "stm32f4xx.h"
                        #include "stm32f4_discovery.h"
           
           
TIM_ICInitTypeDef  TIM_ICInitStructure;
           
           
void TIM_Config(void);
           
           
int main(void)
            {  
              TIM_Config();
           
              TIM_ICInitStructure.TIM_Channel     = TIM_Channel_2;  
              TIM_ICInitStructure.TIM_ICPolarity  = TIM_ICPolarity_Rising; 
              TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 
              TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
              TIM_ICInitStructure.TIM_ICFilter    = 0x0;  
           
           
  TIM_ICInit(TIM1, &TIM_ICInitStructure);
           
  TIM_Cmd(TIM1, ENABLE);
           
  TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
           

              while (1);
            }
           
            void TIM_Config(void)
            {
              GPIO_InitTypeDef GPIO_InitStructure;
              NVIC_InitTypeDef NVIC_InitStructure;
           
           
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
           
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
           
           
  GPIO_InitStructure.GPIO_Pin   = 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(GPIOE, &GPIO_InitStructure);
           
           
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
           
              NVIC_InitStructure.NVIC_IRQChannel                   = TIM1_CC_IRQn;
              NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
              NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
              NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
              NVIC_Init(&NVIC_InitStructure);
           
            }

           

The code of the IRQHandler :

                                   
void TIM1_CC_IRQHandler(void)
            {
              if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)
              {
                /* Clear TIM1 Capture compare interrupt pending bit */
                TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
                if(CaptureNumber == 0)
                {
                  /* Get the Input Capture value */
                  IC3ReadValue1 = TIM_GetCapture2(TIM1);
                  CaptureNumber = 1;
                }
                else if(CaptureNumber == 1)
                {
                  /* Get the Input Capture value */
                  IC3ReadValue2 = TIM_GetCapture2(TIM1);
           
           
                  /* Capture computation */
                  if (IC3ReadValue2 > IC3ReadValue1)
                  {
                    Capture = (IC3ReadValue2 - IC3ReadValue1);
                  }
                  else if (IC3ReadValue2 < IC3ReadValue1)
                  {
                    Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
                  }
                  else
                  {
                    Capture = 0;
                  }
                  /* Frequency computation */
                  TIM1Freq = (uint32_t) SystemCoreClock / Capture;
                  CaptureNumber = 0;
                }
              }
            } 

The same code with Timer 2 is:
                                   
#include "stm32f4xx.h"
            #include "stm32f4_discovery.h"
           
            TIM_ICInitTypeDef  TIM_ICInitStructure;
           
            void TIM_Config(void);
           
            int main(void)
            {
              TIM_Config();
           
              TIM_ICInitStructure.TIM_Channel     = TIM_Channel_3; 
              TIM_ICInitStructure.TIM_ICPolarity  = TIM_ICPolarity_Rising;  
              TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 
              TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
              TIM_ICInitStructure.TIM_ICFilter    = 0x0;  
           
              TIM_ICInit(TIM2, &TIM_ICInitStructure);
              TIM_Cmd(TIM2, ENABLE);
           
              TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
           
              while (1);
            }
           
            void TIM_Config(void)
            {
              GPIO_InitTypeDef GPIO_InitStructure;
              NVIC_InitTypeDef NVIC_InitStructure;
           
              RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
              RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
           
              GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
              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_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2);
           
              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);
            }

           

The IRQHandler of Timer 2 is:

                                   
 void TIM2_IRQHandler(void)
            {
              if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)
              {
                /* Clear TIM2 Capture compare interrupt pending bit */
                TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
                if(CaptureNumber == 0)
                {
                  /* Get the Input Capture value */
                  IC3ReadValue1 = TIM_GetCapture2(TIM2);
                  CaptureNumber = 1;
                }
                else if(CaptureNumber == 1)
                {
                  /* Get the Input Capture value */
                  IC3ReadValue2 = TIM_GetCapture2(TIM2);
           
           
                  /* Capture computation */
                  if (IC3ReadValue2 > IC3ReadValue1)
                  {
                    Capture = (IC3ReadValue2 - IC3ReadValue1);
                  }
                  else if (IC3ReadValue2 < IC3ReadValue1)
                  {
                    Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
                  }
                  else
                  {
                    Capture = 0;
                  }
                  /* Frequency computation */
                  TIM1Freq = (uint32_t) SystemCoreClock / Capture;
                  CaptureNumber = 0;
                }
              }
            }

I don't know what's wrong and what i have to do to make the Timer 2 working. 
Thanks. 

Outcomes