cancel
Showing results for 
Search instead for 
Did you mean: 

incrementation of a timer : problem

claire
Associate III
Posted on May 14, 2012 at 17:40

hi,

i am working on the stm32discovery

i am trying to increment a timer (timer2 channel 1) each time the signal on the pin PA.05 is rising but the timer is random incrementing and not at each rising edge of the signal

i don't understand why ?

here is my code :

int main(void)

{

    /* Configure Clocks for Application need */

    RCC_Configuration();

    /* Configure RTC Clocks */

    //RTC_Configuration();

      /* Configure SysTick IRQ and SysTick Timer to generate interrupts every 500µs */

    RCC_GetClocksFreq(&RCC_Clocks);

    //SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000);

    /* Enable GPIOs clock */     

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    /*PA.05 TIM2 channel 1*/

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

    TIM_InitStruct.TIM_Period = 849;

    TIM_InitStruct.TIM_Prescaler = 0;

    TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2,&TIM_InitStruct);

    /*timer2 chanel 1  ETR pin PA5*/

    TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;

    TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;

    TIM_ICInitStruct.TIM_ICSelection  = TIM_ICSelection_DirectTI;

    TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;

    TIM_ICInitStruct.TIM_ICFilter = 0x0;

    TIM_ICInit(TIM2, &TIM_ICInitStruct);

    /*enable the TIM counter*/

    TIM_Cmd(TIM2, ENABLE);

    TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);

    TIM_ETRClockMode1Config(TIM2,TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted,0);

  while (1){

      SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000);

    bitStatut = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);

    increment = TIM_GetCapture1(TIM2);

    Delay(100);

  }

could you help me ?

thanks
11 REPLIES 11
Posted on February 25, 2013 at 18:21

Bonding PC6 to PA0 button input

void TIM3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); // AFIO required for remapping
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // PC6 TIM3_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
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);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
carl239955_st
Associate
Posted on February 25, 2013 at 20:16

Big THANK YOU! It was the RCC_APB2Periph_AFIO that solved my issue! You saved me alot of time.