cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timers synchronization

mkusenda
Associate
Posted on August 06, 2013 at 10:51

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):


////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////// REFERENCE COUNTER ///////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////


void
initReferenceCounter(
void
)

{

GPIO_InitTypeDef GPIO_InitStructure;

// NVIC_InitTypeDef NVIC_InitStructure;

TIM_ICInitTypeDef TIM_ICInitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);


TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

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 = 0;


TIM_ICInit(TIM2, &TIM_ICInitStructure);


TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);


TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);


TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);


TIM_Cmd(TIM2, ENABLE);

}


////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////// SENSOR COUNTER /////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////


void
initSensor1Counter(
void
)

{

TIM_ICInitTypeDef TIM_ICInitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
//Changed from NOPULL

GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);


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 = 0;


TIM_ICInit(TIM3, &TIM_ICInitStructure);


TIM_TimeBaseStructure.TIM_Period = SENx_TIM_PERIOD;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = SENx_TIM_PULSE;

TIM_OC2Init(TIM3, &TIM_OCInitStructure);


NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);


/*MASTER SLAVE MODE SELECTION*/


TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, (uint16_t)0);


TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref);


TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);


TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);


TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);


/*ENDING*/


TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);


TIM_Cmd(TIM3, ENABLE);

}

#synchronization #timers #stm32f4
0 REPLIES 0