cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f407 external pulse counter channel selection

anze
Associate
Posted on August 21, 2013 at 12:06

Hi guys,

I am having a big problem; I would like to count external pulses with 2 timers; TIM 2 and TIM5. The problem is, that I need to connect my two timers to the 2 I/O pins that are free. So that means I need to select a channel for each of them. And I just can't find any channel selection for counting external pulses. So I chose TIM2 ch4 on PB11 and TIM5 ch4 on PA3. I need to use 32bit counters. How do I enable any other channel that default channel, which is ch1? Many thanks guys

void PulseCounter(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
/* GPIOx clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* TIM2,TIM5 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
/* GPIOA Configuration: TIM2 CH4 (PB11) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Input/Output controlled by peripheral
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // Button to ground expectation
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* GPIOA Configuration: TIM5 CH4 (PA3) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Input/Output controlled by peripheral
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // Button to ground expectation
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM2 pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_TIM2);
/* Connect TIM5 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Period = 500000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 500000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Falling, 0);
TIM_TIxExternalClockConfig(TIM5, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Falling, 0);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM5, ENABLE);
}

12 REPLIES 12
Posted on April 24, 2014 at 15:55

0690X0000060576QAA.png
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 24, 2014 at 16:07

You can also work out of registers description - here, TIMx_SMCR.

There are 3 possible clock sources:

ECE == 0 and SMS != 111 - internal clock is used

ECE == 1 - external clock mode 2, signal coming from TIMx_ETF pin is used, after being prescaled and filtered, see ETP, ETPS and ETF fields in the same register

ECE == 0 and SMS == 111 - trigger TRGI is used. What is source of TRGI, is given by the TS field in the same register.

JW

jeanmarc
Associate
Posted on April 28, 2014 at 10:26

Clive1 thank you very much.

I can see it now.

Regards.

jean-marc