cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 timer as counter to count external pulses

GenuineDeveloper
Associate III
Posted on October 16, 2017 at 12:18

Hi,

I am using STM32F051R8T6 to simply use the timer to count external pulses. I have referred the reference manual RM0091 and have used the function given in the Standard peripheral library. I am using PA9 to receive the external pulses, as per the code example given in the datasheet. I am yet to figure out what interrupt should be used to capture the value and how should I get the value of counts?

Library used: Standard Peripheral Library

Reference Manual: RM0091

Section referred: TIMER1

Sub section: External clock source mode 1

Any inputs to complete the interrupt part will be really useful.

(Either the documentation provided is really poor or may be its complex enough for a beginner like me to understand. It is really tiresome to do simple tasks such as counting using timer peripheral by referring the entire manual and still not getting any idea as in whole. I agree that I am not expert in the field, but it still should be well documented if it needs to be popular and widely acceptable.) Thank you.

The code so far:

int main(void)

{

    GpioConfig();

    CounterConfig();

    while(1)

    {

    }

}

void CounterConfig()

{

    //Enable Peripheral Clock on Timer 1

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);

    TIM_TIxExternalClockConfig(TIM1,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0x00);

    TIM_Cmd(TIM1,ENABLE);

}

void GpioConfig(void)

{

    GPIO_InitTypeDef GPIO;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    GPIO.GPIO_Pin = GPIO_Pin_9;

    GPIO.GPIO_Mode = GPIO_Mode_AF;

    GPIO.GPIO_OType = GPIO_OType_PP;

    GPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2);

    GPIO_Init(GPIOA,&GPIO);

}

#stm32f0-counter #stm32f0 #stm32f0-timer
7 REPLIES 7
Posted on October 16, 2017 at 12:28

External clock source mode 1

[...]

TIM_ETRClockMode2Config(

This is inconsistent.

how should I get the value of counts

The counter itself (TIMx_CNT) will increment when pulses arrive on the external input, so read TIMx_CNT. Run your initialization code and after that pause it in the debugger (e.g. placing a breakpoint after the init code) put the external pulses and observe TIMx_CNT.

 what interrupt should be used to capture the value

What interrupt would you use to see the value of counter if it would be clocked from internal source?

I don't use SPL.

JW

Posted on October 16, 2017 at 14:06

External clock source mode 1

[...]

TIM_ETRClockMode2Config(

This is inconsistent.

Thanks. Anyway, I am using TI2.

I have added your points. I am observing countVal, when an update interrupt is generated. But it is not being incremented.

What am I doing wrong here?? Am I missing any other functions??

int main(void)

{

    countVal = 0;

    GpioConfig();

    CounterConfig();

    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);

    while(1)

    {

    }

}

void CounterConfig()

{

    //Enable Peripheral Clock on Timer 1

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

    TIM_TimeBaseInitTypeDef    TIM1Struct;

    NVIC_InitTypeDef    NVIC_InitStructure;

    TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x00);

    TIM_TIxExternalClockConfig(TIM1,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0x00);

    TIM1Struct.TIM_ClockDivision = 0;

    TIM1Struct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM1Struct.TIM_Prescaler = 0;

    TIM1Struct.TIM_Period = 0;

    TIM1Struct.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM1,&TIM1Struct);

    TIM_Cmd(TIM1,ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

    NVIC_Init(&NVIC_InitStructure);

}

void GpioConfig(void)

{

    GPIO_InitTypeDef GPIO;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    GPIO.GPIO_Pin = GPIO_Pin_9;

    GPIO.GPIO_Mode = GPIO_Mode_AF;

    GPIO.GPIO_OType = GPIO_OType_PP;

    GPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2);

    GPIO_Init(GPIOA,&GPIO);

}

void TIM1_IRQHandler(void)

{

    if(TIM_GetFlagStatus(TIM1,TIM_FLAG_Update))

    {

        TIM_ClearFlag(TIM1,TIM_FLAG_Update);

        countVal++;

    }

}
Posted on October 16, 2017 at 14:34

Read out and post the content of TIM registers.

JW

GenuineDeveloper
Associate III
Posted on October 17, 2017 at 06:56

Anyone has an insight on this issue?? Will be grateful if you could direct me to some examples or tutorials(I did not find any counting examples).

Thank you.

Posted on October 17, 2017 at 06:11

PFA registers of TIM1

________________

Attachments :

STM32F051R8T6_TIM1_REGISTERS.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyN5&d=%2Fa%2F0X0000000b6j%2FxcMR9zYHgV.s28QxWidbSne.JnJxIBsao_ivhtoMCBM&asPdf=false
Posted on October 17, 2017 at 07:42

I mean, run your code, then stop it (put a breakpoint) and read out what is in the registers.

TIM1Struct.TIM_Period = 0;

As I've said I don't use SPL, but if this means TIMx_ARR=0 then that counter is going to be stopped forever - you want to set it to nonzero, possibly its maximum.

JW

Posted on December 31, 2017 at 16:12

Sorry, a little late, but I was just browsing and found this post.

I am pretty sure you should look at the the timers the support encoder interface modes.

Set the timer mode for counting on TI1 edges only.

The TI2 level determines if the counter (timer) counts up/down (direction).

To get live count, read the CNT register of the timer as suggested above.

If counting to a target, set ARR to target, reset CNT, configure period elapsed interrupt same normal timer setup.