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 August 21, 2013 at 13:56

That'll be a problem as they don't route

  * @param  TIM_TIxExternalCLKSource: Trigger source.

  *          This parameter can be one of the following values:

  *            @arg TIM_TIxExternalCLK1Source_TI1ED: TI1 Edge Detector

  *            @arg TIM_TIxExternalCLK1Source_TI1: Filtered Timer Input 1

  *            @arg TIM_TIxExternalCLK1Source_TI2: Filtered Timer Input 2

Also note Period = N - 1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
anze
Associate
Posted on August 21, 2013 at 14:50

So that means I can only use ch1? TIM5 ch1 pin is used for user button and TIM2 ch1 is also used for SCL/SPC and for other TIM5 ch1 says it's ETR. Does that means it's external trigger? I tryed and it didn't worked.

Thanks for fast reply

m_vaccaro
Associate II
Posted on September 03, 2013 at 02:35

Hi Clive, I have to read a sensor that work with pulse. I have to counter the Low Pulse Occupancy time for get the read . I attach a image cutted from datasheet.

Can you help me? I use a stm32f4 discovery device

Thanks in advance

Posted on September 03, 2013 at 03:09

That didn't work, try citing the part#, data sheet, and page.

From the sound of it you'd want to use input capture mode, have it latch a free running count in TIMx->CNT into TIMx->CCRx and interrupt on the edges, or perhaps PWM input mode.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
m_vaccaro
Associate II
Posted on September 03, 2013 at 03:19

thanks clive for your fast response. I attached the a image . I have to count the low pulse width for a definite time as 30seconds. What solution do you suggest?

Mic

________________

Attachments :

Img.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0mb&d=%2Fa%2F0X0000000bda%2FWb_xASb3Vo2cMcNMgrFP9ROf3L488poYfg.o5ah4cf0&asPdf=false
Posted on September 03, 2013 at 03:48

So how does the 10-90 ms low pulse widths relate to 30 seconds? Do you want to know the pulse widths, or just integrate them over 30 seconds?

Input Capture? STM32F4xx_DSP_StdPeriph_Lib_V1.1.0\Project\STM32F4xx_StdPeriph_Examples\TIM\TIM_InputCapture

#define TIM_ICPolarity_Rising ((uint16_t)0x0000)
#define TIM_ICPolarity_Falling ((uint16_t)0x0002)
#define TIM_ICPolarity_BothEdge ((uint16_t)0x000A)

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

Hi Clive, thanks a lot for you support. I'll know the low pulse widths duration and to do the sum of it. Sample for three pulse.... first 40ms is low, second 30ms is low and last 40ms is low....  40+30+40 = 110 and apply the formula for gas concentration .

Can you write a sample of code?

You are the best in the forum.. thanks you

jeanmarc
Associate
Posted on April 24, 2014 at 15:37

Hi Clive1

I have the same problem. I am not sure I understood ''they don't route''.

How can you see this, what documentation? What datasheet? I need some help it is 2 days I am trying to find a solution.

Posted on April 24, 2014 at 15:49

Well assuming you're using an F407 part, then that would be the Reference Manual, RM0090

The signal routing is limited, you cannot invent connectivity to suit. ''Does not route'' basically means the software/hardware to not provide a method to get from where you are, to where you want to go.

This would be the salient diagram in the Reference Manual, that shows the non ETR/ITR sources for an external clock. This re-enforces the Firmware Library definitions I posted earlier.

0690X000006054wQAA.png

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