AnsweredAssumed Answered

STM32F0 timer as counter to count external pulses

Question asked by R V on Oct 16, 2017
Latest reply on Dec 31, 2017 by John Craven

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);
}

Outcomes