AnsweredAssumed Answered

STM32F4 Timers synchronization

Question asked by mikajelo on Aug 6, 2013
Hello.
I would like to ask you, how could I connect 2 timers to count at the same time?
My configuration is like this:
TIM2 is reference (externally via ETR I connect reference oscillator 16 MHz).
TIM3 is measuring counter (externally, via ETR I connect measured frequency cca 9-11 kHz)
What I need is that after I enable the TIM3, nothing happens. But as soon as there is valid edge on the TIM3 ETR, TIM2 starts counting at the same time.
But as soon as TIM3 counts let´s say 36000 valid edges, TIM2 should immediately stop.

I tried it like this (numbers are bit different in my code, than in the text above):

001.////////////////////////////////////////////////////////////////////////////////////////////
002./////////////////////////////////////  REFERENCE COUNTER ///////////////////////////////////
003.////////////////////////////////////////////////////////////////////////////////////////////
004. 
005.void initReferenceCounter(void)
006.{
007.    GPIO_InitTypeDef GPIO_InitStructure;
008.//  NVIC_InitTypeDef    NVIC_InitStructure;
009.    TIM_ICInitTypeDef   TIM_ICInitStructure;
010.    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
011.     
012.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
013.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
014. 
015.  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5;
016.  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
017.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
018.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
019.  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
020.  GPIO_Init(GPIOA, &GPIO_InitStructure);
021. 
022.  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);
023.     
024.    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
025.  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
026.  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
027.  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
028.  TIM_ICInitStructure.TIM_ICFilter = 0;
029.     
030.    TIM_ICInit(TIM2, &TIM_ICInitStructure);
031.         
032.    TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
033.  TIM_TimeBaseStructure.TIM_Prescaler = 0;
034.  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
035.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
036.  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
037.             
038.    TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);
039.     
040.    TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);
041.     
042.    TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
043.     
044.    TIM_Cmd(TIM2, ENABLE);
045.}
046. 
047.////////////////////////////////////////////////////////////////////////////////////////////
048.//////////////////////////////////////  SENSOR COUNTER /////////////////////////////////////
049.////////////////////////////////////////////////////////////////////////////////////////////
050. 
051.void initSensor1Counter(void)
052.{
053.    TIM_ICInitTypeDef   TIM_ICInitStructure;
054.    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
055.    GPIO_InitTypeDef GPIO_InitStructure;
056.    NVIC_InitTypeDef    NVIC_InitStructure;
057.    TIM_OCInitTypeDef TIM_OCInitStructure;
058. 
059.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
060.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
061.     
062.  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
063.  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
064.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
065.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
066.  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;                                 //Changed from NOPULL
067.  GPIO_Init(GPIOD, &GPIO_InitStructure);
068.  GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);
069. 
070.    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
071.  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
072.  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
073.  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
074.  TIM_ICInitStructure.TIM_ICFilter = 0;
075.     
076.    TIM_ICInit(TIM3, &TIM_ICInitStructure);
077.     
078.    TIM_TimeBaseStructure.TIM_Period = SENx_TIM_PERIOD;
079.  TIM_TimeBaseStructure.TIM_Prescaler = 0;
080.  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
081.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
082.  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
083. 
084.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
085.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
086.  TIM_OCInitStructure.TIM_Pulse = SENx_TIM_PULSE;
087.  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
088.         
089.    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
090.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
091.  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
092.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
093.  NVIC_Init(&NVIC_InitStructure);
094.     
095.    /*MASTER SLAVE MODE SELECTION*/
096.     
097.    TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, (uint16_t)0);
098.     
099.    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref);
100.     
101.    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
102.     
103.    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);
104.     
105.    TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
106.     
107.    /*ENDING*/
108.         
109.    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
110.     
111.    TIM_Cmd(TIM3, ENABLE);
112.}

Outcomes