cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Timer External Pulse counting

piotrek700
Associate
Posted on May 18, 2013 at 00:23

Since 4 day I'm trying write code which will be able to count external pulses. I tried to use encoder mode but i supposed that this work only with 2 quadrature encoders. I have only one pulse signal and i have no idea how to do it. Could you help me or show examples how to count external pulses using timer?

#counter #timer #off-topic #pulce
35 REPLIES 35
Posted on May 18, 2013 at 12:59

This is a blind conversion of a previous example to the Discovery-F4

void TIM3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA Configuration: TIM3 CH1 (PC6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
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(GPIOC, &GPIO_InitStructure);
/* Connect TIM3 pins to AF */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_Cmd(TIM3, ENABLE);
} // sourcer32@gmail.com

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
m23
Associate
Posted on May 31, 2013 at 18:56

My first post here, hi all! :)

I just wanted to say: thak you so much for that code, Clive, I've been struggling with that configuration for 2 days.

Cheers,

Mike

denis
Associate II
Posted on August 15, 2013 at 14:32

My MCU is an STM32F103x, but I made your example work on it too, thanks.

The question I have:

is it possible to have a timer to output clock on one channel and same timer to count external pulses on its other channel?

I.e. clocking on one line, and receiving bits/pulses on the other and counting them.

With example here i only see how to do this with 2 timers.

Posted on August 15, 2013 at 15:37

The problem here is that the TIM units have only a single counting element, the channels provide four latches to either compare or hold values with respect to the counter.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
denis
Associate II
Posted on August 15, 2013 at 16:07

Yeah, I just thought i give it a shot :)

But using the above pulse counting. Can this be used in a useful way to record PDM ? As in, clock the PDM mic with one timer, and record PDM pulses with the other by configuring it to count the pulses.

Posted on August 15, 2013 at 18:05

I don't know, you're wading into an area where I have little practical experience.

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00040808.pdf

I can see ways to generate signals, and measure things with the same timer, you'll have to look at the block diagram to see if the task you want to do is possible.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
denis
Associate II
Posted on September 04, 2013 at 14:04

In this external clock source example, what's the behavior of CCR1? I see it's = CNT-1 always for this config, and. .. not sure on why :), i'm not using channel 1 for capture but for clock source.

adapted to TM1 config:

GPIO_InitTypeDef

GPIO_InitStructure

;

TIM_TimeBaseInitTypeDef

TIM_TimeBaseStructure

;

RCC_APB2PeriphClockCmd

(

RCC_APB2Periph_GPIOA

|

RCC_APB2Periph_TIM1

,

ENABLE

);

/*

GPIOA

Configuration:

TIM1

CH1

=

PA8

*/

GPIO_InitStructure

.

GPIO_Pin

=

GPIO_Pin_8

;

GPIO_InitStructure

.

GPIO_Mode

=

GPIO_Mode_IN_FLOATING

;

GPIO_InitStructure

.

GPIO_Speed

=

GPIO_Speed_2MHz

;

GPIO_Init

(

GPIOA

,

&

GPIO_InitStructure

);

TIM_TimeBaseStructure

.

TIM_Period

=

65535

;

TIM_TimeBaseStructure

.

TIM_Prescaler

=

0

;

TIM_TimeBaseStructure

.

TIM_ClockDivision

=

0

;

TIM_TimeBaseStructure

.

TIM_CounterMode

=

TIM_CounterMode_Up

;

TIM_TimeBaseInit

(

TIM1

,

&

TIM_TimeBaseStructure

);

TIM_TIxExternalClockConfig

(

TIM1

,

TIM_TIxExternalCLK1Source_TI1

,

TIM_ICPolarity_Rising

,

0x0

);

TIM_Cmd

(

TIM1

,

ENABLE

);

Posted on September 04, 2013 at 14:23

TIM1 and 8 are special

  /* Main Output (and input) Enable */

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

CCR1 plays NO role
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
denis
Associate II
Posted on September 04, 2013 at 16:18

mmmm, don't know TIM_CtrlPWMOutputs(TIM1, ENABLE);

would show me.

I wanted to use CCR1 for an interrupt at some counts but it updates at same rate as CNT,

that's why I asked what's it doing.

I can use CCR2 fine.