cancel
Showing results for 
Search instead for 
Did you mean: 

Problem trying to write a timer program counting edges

randyao12345
Associate II
Posted on June 06, 2014 at 02:06

Hey ST,

I'm using STM32F4DISCOVERY board, and tried to write a counter for rising and falling edges using timer input capture mode. Couldn't capture anything though. Any ideas what's wrong would be appreciated.

Also, does anyone know what TIM_ICFilter, GPIO_OType does?

Thanks in advance.

//Headers

//PA5 -> TIM2

#include ''stm32f4xx_dma.h''

#include ''stm32f4xx_exti.h''

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_tim.h''

#include ''semihosting.h''

//Prototypes

void GPIO_Config(void);

void TIM_IC_Config(void);

//Global Variables

uint32_t count;

int main(void)

{

GPIO_Config();

TIM_IC_Config();

    while(1)

    {

    count = TIM_GetCapture1(TIM2);

    printf(''\nTIM: %d'',count);

    }

}

void GPIO_Config(void)

{

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //tim1? cannot unable?

GPIO_InitTypeDef gpio;

gpio.GPIO_Pin = GPIO_Pin_5;

gpio.GPIO_Mode = GPIO_Mode_AF;

gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;

gpio.GPIO_Speed = GPIO_Speed_50MHz;

gpio.GPIO_OType = GPIO_OType_PP; //What is this actually?

GPIO_Init(GPIOA, &gpio);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM2);

}

void TIM_IC_Config(void)

{

//TIM2 Channel 1

TIM_ICInitTypeDef tim;

tim.TIM_Channel = TIM_Channel_1;

tim.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Both up and down edge

tim.TIM_ICSelection = TIM_ICSelection_DirectTI; //??? direct vs indirect

tim.TIM_ICPrescaler = TIM_ICPSC_DIV2;

tim.TIM_ICFilter = 0x00; //?

TIM_ICInit(TIM2,&tim);

}

3 REPLIES 3
Posted on June 06, 2014 at 04:10

TIM1 is on APB2, enabling a clock on APB1 for it won't help

PP - Push Pull Driver, transistors drive pin HIGH or LOW, in output mode

Input Capture latches a time base count, a time base which you haven't initialized, or enabled. This would allow you to determine where edges are in the time domain, allowing the measurement for period and duty.

If you want to COUNT external pulses, you need to configure the timer with an external clocking mode, and read the value from TIMx->CNT
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
randyao12345
Associate II
Posted on June 06, 2014 at 17:58

Hi Clive, thanks so much for the info. I'll try out the external clocking mode and test it out.

randyao12345
Associate II
Posted on June 06, 2014 at 19:37

Hi Clive,

Thanks so much for the tip. I got the external edge counter working (below). i have another problem though: i'm trying to count rising edges above 0V as positive, while counting rising edges below 0V as negative (attached picture - rising edges in 0-3V range +1, rising edges in -3V to 0V range -1).

Thanks,

Randy

//Headers

//PA5 -> TIM2

#include ''stm32f4xx_dma.h''

#include ''stm32f4xx_exti.h''

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_tim.h''

#include ''semihosting.h''

//Prototypes

void GPIO_Config(void);

void TIM_IC_Config(void);

//Global Variables

uint32_t count;

int main(void)

{

GPIO_Config();

TIM_IC_Config();

while(1)

{

count = TIM2->CNT;

printf(''\nTIM: %d'',count);

}

}

void GPIO_Config(void)

{

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

GPIO_InitTypeDef gpio;

gpio.GPIO_Pin = GPIO_Pin_5;

gpio.GPIO_Mode = GPIO_Mode_AF;

gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;

gpio.GPIO_Speed = GPIO_Speed_50MHz;

gpio.GPIO_OType = GPIO_OType_PP;

GPIO_Init(GPIOA, &gpio);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM2);

}

void TIM_IC_Config(void)

{

//TIM2 Channel 1

TIM_TimeBaseInitTypeDef tim;

tim.TIM_Period = 65535;

tim.TIM_Prescaler = 0;

tim.TIM_ClockDivision = 0;

tim.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2,&tim);

TIM_TIxExternalClockConfig(TIM2,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0);

TIM_Cmd(TIM2,ENABLE);

}

________________

Attachments :

edge_counts.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HztN&d=%2Fa%2F0X0000000bQu%2Fo3tiekR6ueIM4RWvWi.55kXUUJhZtMpoHY2IQ15Msho&asPdf=false