cancel
Showing results for 
Search instead for 
Did you mean: 

incrementation of a timer by an input signal

claire
Associate III
Posted on May 09, 2012 at 17:18

hi,

i'm trying ton increment a timer in timing mode at each rising of a signal which arrive on an other pin

here my code :

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);

 

    /*Configuration de la pin1 du portA(arriver du signal detection de traverse)*/

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

    GPIO_Init(GPIOB,&GPIO_InitStructure);

    /* timing Mode configuration */

    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Timing;

    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OCInitStruct.TIM_Pulse = CCR1Val;

    TIM_OC1Init(TIM4, &TIM_OCInitStruct);

    /*timer2 chanel 2 pin PA1*/

    TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;

    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(TIM4, ENABLE);

    /*Configures the External clock Mode1: le compteur compte à chaque front montant d'une horloge externe ici

    l'entrée sur laquel il y à le signal qui detecte les traverses*/

    TIM_ETRClockMode1Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted,0x00);

  while (1){

    sprintf(str,''%u'',TIM4->CNT);

    LCD_GLASS_DisplayString( (unsigned char *) str );

  }

but it doesn't work and i don't know why

i think it is the function which Configures the External clock in Mode1

could you help me please !!

5 REPLIES 5
Posted on May 09, 2012 at 20:50

What part are we talking about?

What's PB.6 doing? If it's coming from the timer you'd need to be using an AF setting for the GPIO, and configuring the AF Pin Source Mux if part appropriate.

You only enable one of the timers.

In TIM_OCMode_Timing, I'm pretty sure you need to initialize the time base.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
claire
Associate III
Posted on May 10, 2012 at 11:54

hi

i made the changes but it seem that i don't recieve the bit of PA1

i'm trying to read it but it doesn't work

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);

    /* 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);

    LCD_GLASS_Configure_GPIO();

    LCD_GLASS_Init();

  while (1){

      

    bitA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);

    sprintf(str,'' %u'',bitA);

    LCD_GLASS_DisplayString( (unsigned char *) str );

  }

and i don't understand why ? did i forget something ?

claire
Associate III
Posted on May 10, 2012 at 11:55

hi

i made the changes but it seem that i don't recieve the bit of PA1

i'm trying to read it but it doesn't work

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);

    /* 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);

    LCD_GLASS_Configure_GPIO();

    LCD_GLASS_Init();

  while (1){

      

    bitA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);

    sprintf(str,'' %u'',bitA);

    LCD_GLASS_DisplayString( (unsigned char *) str );

  }

and i don't understand why ? did i forget something ?

claire
Associate III
Posted on May 10, 2012 at 12:16

it's like i don't recieve the signal !!!

Posted on May 11, 2012 at 04:20

Ok, I'm going to assume an STM32L15x part, absent information to the contrary.

You definitely need enable peripheral clocks BEFORE configuring the peripherals, as the device is synchronous.

/* Configure Clocks for Application need, DO THIS BEFORE TOUCHING PERIPHERALS */
RCC_Configuration();

/* Configure PA.01 for TIM2_CH2 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
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_PinSource1, GPIO_AF_TIM2);
/* 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);
LCD_GLASS_Configure_GPIO();
LCD_GLASS_Init();
while (1){
bitA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
sprintf(str,'' %u'',bitA);
LCD_GLASS_DisplayString( (unsigned char *) str );
}

Now, if I wanted to count pulses on an external pin, I'd need to configure the timers time base, and the select an external clock to make it count. I don't think Input Compare mode does this as the CCR registers are latches, not counters. My expectation is that something like this would count external pulses.

void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// PA.01 is TIM2_CH2
TIM_TimeBaseStructure.TIM_Period = 65535; 
TIM_TimeBaseStructure.TIM_Prescaler = 0; 
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM2,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0); //TI2FP2
TIM_Cmd(TIM2, ENABLE);
}

Then use TIM_GetCounter(TIM2) to get the count.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..