Skip to main content
piotrek700
Associate
May 17, 2013
Question

STM32F4 Timer External Pulse counting

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
This topic has been closed for replies.

35 replies

denis
Associate III
September 5, 2013
Posted on September 05, 2013 at 09:42

Ok clive1 :)

Tesla DeLorean
Guru
March 24, 2014
Posted on March 25, 2014 at 00:57

Quick port for F1 series parts

void TIM3_Configuration(void) // STM32F1xx
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIO Configuration: TIM3 CH1 (PC6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); // Get TIM3 to PC6
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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
fireon89
Visitor II
August 7, 2014
Posted on August 07, 2014 at 20:55

/* pulse_counter.phr */

#include ''main.h'' int cnt; void TIM3_Configuration(void); void TIM3_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* GPIOC clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_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_2MHz; 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_0); 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_TI2, TIM_ICPolarity_Rising, 0); TIM_Cmd(TIM3, ENABLE); } int main(void) { while(1) { TIM3_Configuration(); cnt=TIM3->CNT ; } } i used this code to count the external pulse , but the counter is not counting . is there any mistakes in the code??
Tesla DeLorean
Guru
August 7, 2014
Posted on August 07, 2014 at 21:19

You must copy-n-paste better,

TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0); // NOT TI2

And don't keep reconfiguring the timer in the while() loop
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
fireon89
Visitor II
August 8, 2014
Posted on August 08, 2014 at 06:28

Thanks for the guidance Clive1

I moved the timer config statement out of the while loop and replaced TI2 with TI1 

but still the counter is not working 

I am using STM32f051 eval board 

fireon89
Visitor II
August 8, 2014
Posted on August 08, 2014 at 21:46

Hi clive1 ,

I did try using different timer (TIM1) this one showed some response to the external pulse but it counted randomly. I tried to my best to make it count but didn't workout . can I learn how to make it work from you. thanks

#include ''main.h''
int count=0;
void main()
{ 
// INTI CLOCK FOR TIM AND GPIO
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; 
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
TIM_Cmd(TIM1, ENABLE);
while(1)
{
count = TIM1->CNT;
}
}

Tesla DeLorean
Guru
August 9, 2014
Posted on August 09, 2014 at 06:24

As I don't have a board, try this blind modification

#include ''main.h''
int count=0;
void main()
{
// INTI CLOCK FOR TIM AND GPIO
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOC clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); // TIM1_CH2
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM1, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
while(1)
{
count = TIM1->CNT;
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
fireon89
Visitor II
August 9, 2014
Posted on August 09, 2014 at 17:48

Hi Clive ,

I got some example code form the reference manual, and it was working but i was not able to understand these lines.where can I learn to code like this.

GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9))| (GPIO_MODER_MODER9_1); /* (3) */
//GPIOA->PUPDR = 0x24000000;
GPIOA->PUPDR = 0x1 <<
17
;
GPIOA->AFR[1] |= 0x2 << ((9-8)*4); /* (4) */

Tesla DeLorean
Guru
August 9, 2014
Posted on August 09, 2014 at 18:41

If you want to learn register level minutia, then you'll need to absorb all the detail in the Reference Manual, and the bits and functions, and pin level associations. Hours of fun, not a style of programming I would recommend.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
vimukthi_0123
Visitor II
April 6, 2015
Posted on April 06, 2015 at 18:17

I cannot figure out why i do not get the external pulse values print in my USART. please help.

float x_dot;

void TIM3_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  

  /* GPIOC clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

 

  /* TIM3 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  

  /* GPIOA Configuration: TIM3 CH1 (PD12) */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

  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(GPIOD, &GPIO_InitStructure);

 

    /* Connect TIM3 pins to AF */

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

  

  TIM_TimeBaseStructure.TIM_Period = 65535;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  

  TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);

  

  TIM_Cmd(TIM4, ENABLE);

}

int main()

{

char str[120];

SystemInit();

TM_DELAY_Init();

/* Initialize USART, TX: PB6 */

TM_USART_Init(USART1, TM_USART_PinsPack_2, 9600);

while(1){

/* Every 500ms */

if (TM_DELAY_Time() >= 500) {

/* Reset time */

TM_DELAY_SetTime(0);

x_dot = TIM_GetCounter(TIM4);

sprintf(str, ''x_dot- %f\r\n'', x_dot);

TM_USART_Puts(USART1, str);

}

}

}