cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 2 of STM32F4 Discovery don't work !

naceur2311
Associate II
Posted on March 02, 2013 at 22:52

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:

&sharpinclude ''stm32f4xx.h''

&sharpinclude ''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:

&sharpinclude ''stm32f4xx.h''

&sharpinclude ''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. 

#stm32f4discovery #stm32-timer #pulse-input
17 REPLIES 17
m_vaccaro
Associate II
Posted on September 16, 2013 at 15:53

Thanks Clive. What is the example that I have to use for read a pulse Width ? I found on STM32F4xx_StdPeriph_Examples :

TIM_PWMInput and TIM_InputCapture

Thanks for support

Mik

m_vaccaro
Associate II
Posted on September 16, 2013 at 16:02

Thanks Clive. What is the example that I have to use for read a pulse Width ? I found on STM32F4xx_StdPeriph_Examples :

TIM_PWMInput and TIM_InputCapture

Thanks for support

Mik

Posted on September 16, 2013 at 16:19

The key point to understand is that the CCRx registers latch the value in CNT at the edge event(s). The tick units of the timebase are defined by the Prescaler value divided against the APBx clock of the TIM unit.

Both examples should illustrate the modes in question, and be used in collaboration with the Reference Manual descriptions.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
uprogc
Associate II
Posted on July 02, 2015 at 10:04

Hi.

I use TIM2 in encoder mode (pins PA0, PA1).

I want to use a second timer in capture mode to measure the impulse time on PA0, or PA1.

How can I connect another timer (in capture mode) to PA0 or PA1 ?

Thanks.

Posted on July 02, 2015 at 17:31

Woefully off topic.

Don't know, perhaps you can configure CC1 or CC2 as the Trigger output, and slave one of the other timers off those. Or trigger a DMA action against another free-running timer/timebase?

Review the manuals, experiment.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
uprogc
Associate II
Posted on July 03, 2015 at 10:52

Thankyou Clive.

I think I will use a separate I/O for TIM input capture, and I connect this to encoder output.

ihsan
Associate
Posted on November 07, 2015 at 04:42

Hey Mr. clive, Im newbie here.

i want to generate an interupt when update event is on using timer.

#include ''stm32f4xx.h''                  // Device header

#include ''stm32f4xx_usart.h''            // Keil::Device:StdPeriph Drivers:USART

#include ''stm32f4xx_tim.h''              // Keil::Device:StdPeriph Drivers:TIM

#include ''stm32f4xx_rcc.h''              // Keil::Device:StdPeriph Drivers:RCC

#include ''stm32f4xx_gpio.h''             // Keil::Device:StdPeriph Drivers:GPIO

#include ''stdio.h''

#include ''delay.h''

#include ''led.h''

#include ''usart.h''

#include ''myDefinition.h''

char buffer[32];

void getClock()

{

    //show the SysClock

    RCC_ClocksTypeDef RCC_ClockFreq;

    RCC_GetClocksFreq(&RCC_ClockFreq);

    sprintf(buffer,''\n\rSYSCLK:%d HCLK:%d, PCLK1:%d, PCLK2:%d'',

                    RCC_ClockFreq.SYSCLK_Frequency,RCC_ClockFreq.HCLK_Frequency,

                    RCC_ClockFreq.PCLK1_Frequency,RCC_ClockFreq.PCLK2_Frequency);

    Usart_puts(buffer);

}

void SysTick_Handler(){

}

void TIM2_Config()

{

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_PCLK1Config(RCC_HCLK_Div16);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseStructure.TIM_Prescaler = 21000-1;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseStructure.TIM_Period = 1000-1;

    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

    TIM_Cmd(TIM2,ENABLE);

}

void TIM2_IT_ENABLE(){

    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);

}

void NVIC_config(void){

        NVIC_InitTypeDef NVIC_InitS;

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

        NVIC_InitS.NVIC_IRQChannel=TIM2_IRQn;

        NVIC_InitS.NVIC_IRQChannelPreemptionPriority=0x00;

        NVIC_InitS.NVIC_IRQChannelSubPriority=0x00;

        NVIC_InitS.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init(&NVIC_InitS);

}

int nInterup=0,counter;

void TIM2_IQRHandler(){

    if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET){

        TIM_ClearITPendingBit(TIM2,TIM_IT_Update);

        counter=TIM_GetCounter(TIM6);

        nInterup++;

        sprintf(buffer,''\n\rTIM2->CNT = %d   interupsi ke-%3d'',counter,nInterup);

        Usart_puts(buffer);

        GPIO_ToggleBits(GPIOD,GPIO_Pin_13);

    }

}

void delay1(uint32_t a){

    while(a!=0){

    a--;}

}

int main(void)

{

    

    SysTick_Init();

    USART_Config();

    getClock();

    TIM2_Config();

    NVIC_config();

    LED_Config();

        

    Usart_puts(''\n\rTimer Start'');

    TIM2_IT_ENABLE();    

    for(;;){

        GPIO_ResetBits(GPIOD,GPIO_Pin_15);

        GPIO_SetBits(GPIOD,GPIO_Pin_13);

        delay1(100);

        GPIO_ResetBits(GPIOD,GPIO_Pin_13);

        GPIO_SetBits(GPIOD,GPIO_Pin_14);

        delay1(100);

        GPIO_ResetBits(GPIOD,GPIO_Pin_14);

        GPIO_SetBits(GPIOD,GPIO_Pin_15);

        delay1(100);

    }

}

i want output show like this

0690X00000603LBQAY.jpg

but what i got like this

0690X00000603L6QAI.jpg

whats wrong with that program?

Posted on November 07, 2015 at 15:50

Misspelled the name of the handler, and referencing TIM6?

void TIM2_IQRHandler(){

    if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET){

        TIM_ClearITPendingBit(TIM2,TIM_IT_Update);

        counter=TIM_GetCounter(TIM6);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..