0
\$\begingroup\$

I trying to use a timer with an external clock source on a stm32f207ZE microcontroller. But it isn't working. Here is my code:

  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOE clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

  /* GPIOE Configuration: PE.11(TIM1 CH2)  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);

  /* TIM1 Input trigger configuration: External Trigger connected to TI2 */
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
  TIM_ICInit(TIM1, &TIM_ICInitStructure);

  TIM_TIxExternalClockConfig (TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0) ;
  TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_External1);

  TIM_Cmd (TIM1, ENABLE);

Where can be my mistake? I have some misunderstanding on what source pin must be used for Timer1 TI2. Is it the same that Timer1_CH2?

I also tried ETR mode by inizializing ETR pin

  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOE clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);


    /* GPIOE Configuration: PE.7(TIM1 ETR)  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);

    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;      // 5000*20 uS - 100 ms
    TIM_TimeBaseStructure.TIM_Prescaler = 0;    // frequency - 20 MHz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 

    //for ETR IN RM0033 p. 372
    TIM1->SMCR = 0;
    TIM1->SMCR |= TIM_ExtTRGPSC_DIV4; //2. set prescaller
    TIM1->SMCR |= TIM_ICPolarity_Rising; //3. rising edge
    TIM1->SMCR |= 0x4000; // 4. Enable external clock mode 2 by writing ECE=1

  TIM_Cmd (TIM1, ENABLE);

But it also doesn't work. Maybe frequency is to high? And i saw some strnge behaviour. When i inizialize once more counter:

void inizializeSimpleCounter(){
  NVIC_InitTypeDef                  NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;

/****************Timer4 Inizialization**************************/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 5000;        // 5000*20 uS - 100 ms
    TIM_TimeBaseStructure.TIM_Prescaler = 3-1;  // frequency - 20 MHz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 

    TIM4->DIER = TIM_DIER_UIE;

    TIM_Cmd(TIM4, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel =  TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
/*************************************************************/

}

The timer, which was initialize to count from extrenal counter counts some values, and then stops

\$\endgroup\$
  • \$\begingroup\$ It might be helpful if you described current behavior versus the expected behavior \$\endgroup\$ – angelatlarge Apr 24 '13 at 16:41
  • \$\begingroup\$ Some external source with frequency 20 MHz is connected to this pin. I expext that counter will begin to count - so the value in TIM1->CNT register will increment. But nothing counts \$\endgroup\$ – user2105282 Apr 25 '13 at 7:44
  • \$\begingroup\$ It seems that error was in schematic. Inizialization is right \$\endgroup\$ – user2105282 Apr 26 '13 at 14:28
1
\$\begingroup\$

I had the same problem but solved it with the code below:

TIM2->SMCR =0x0067;
TIM2->CCMR1=0x0100;
TIM2->CCER=0x0010;
TIM2->CNT = 0x1; 
\$\endgroup\$
  • 3
    \$\begingroup\$ I've formatted your answer but it might be worth adding a further explanation of what the code does and why it works versus the original. \$\endgroup\$ – PeterJ Apr 19 '15 at 10:16
1
\$\begingroup\$

Try to use

TIM_SelectInputTrigger(TIM1, TIM_TS_ETRF);

Function. For me works with first yours example.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.