Skip to main content
Michael Kühebacher
Associate II
February 16, 2017
Solved

Timer 2 capture mode for period measurement - STM32F407

  • February 16, 2017
  • 3 replies
  • 1330 views
Posted on February 16, 2017 at 20:44

// Configure timer 2

    RCC_APB1ENR.TIM2EN = 1;

    TIM2_CR1.CEN = 0;

    TIM2_CR2.TI1S = 0;

    TIM2_PSC = ENCODER_TIM_PSC;

    TIM2_ARR = ENCODER_TIM_RELOAD;

    TIM2_CR1 |= 0x10;

// Configure input capture

    TIM2_CCMR1_Input |= 0x01;

    TIM2_CCER.CC1P = 0;

    TIM2_CCER.CC1NP = 0;

    TIM2_CCER.CC1E = 1;

    TIM2_DIER.CC1IE = 1;

Question: how do I define input PC1 as TI1 for timer 2

thanks!

#period-measurement
    This topic has been closed for replies.
    Best answer by waclawek.jan
    Posted on February 16, 2017 at 20:49

    In GPIO. Select AF in GPIOC_MODER, and then look into the table in DS to find out which AF is TIM2 and set that into GPIOC_AFRL/AFRH (AFRL here).

    3 replies

    waclawek.jan
    waclawek.janBest answer
    Super User
    February 16, 2017
    Posted on February 16, 2017 at 20:49

    In GPIO. Select AF in GPIOC_MODER, and then look into the table in DS to find out which AF is TIM2 and set that into GPIOC_AFRL/AFRH (AFRL here).

    Michael Kühebacher
    Associate II
    February 18, 2017
    Posted on February 18, 2017 at 20:39

    Gib Text oder eine Webadresse ein oder

    https://translate.google.com/?tr=f&hl=de

    .

    https://translate.google.com/?tr=t&hl=de

    https://ssl.gstatic.com/translate/arrow_ltr.png

    MEHR ÜBERSETZEN

    Fehler bei der Übersetzung

    Is working !

    Many Thanks!

    Michael Kühebacher
    Associate II
    February 20, 2017
    Posted on February 20, 2017 at 19:48

    New problem, measuring unstable due to internal oscillator!

    I want to use an external oscillator for Timer 2:

        GPIO_Alternate_Function_Enable(&_GPIO_MODULE_TIM2_CH1_PA0);   // Configure alternate function for A0 as Timer 2 Channel 1

        TIM2_CCMR1_Input |= 0x01;      // Set capture channel 1 as input on TI1 (CC1S = 01)

        TIM2_CCER.CC1P = 0;              // Set capture on rising edge event

        TIM2_CCER.CC1NP = 0;

        Tim2_CCMR1_Input |= 0x100;     // for Extern Clock - Set IC2 as input on TI2 (SS2S = 01)

        TIM2_CCER.CC2P = 0;                // Set Extern Clock rising edge event

        TIM2_CCER.CC2NP = 0;

        TIM2_SMCR.SMS = 0x111;           // Set Timer External Clock Mode 1

        GPIOA_MODER|=0x800;               // Port-A Pin-5 AF alternate Funktion mode

        GPIOA_AFRL|=0x100000;             // AF1 Port-A Pin-5

        TIM2_CCER.CC1E = 1;                  // Enable capture on channel 1

        TIM2_DIER.CC1IE = 1;                   // Enable interrupt on capture channel 1

    Have no idea how I can link TI2 to a physical input pin and whether the combination of Timer Capure Mode and external ckock is possible

    waclawek.jan
    Super User
    February 21, 2017
    Posted on February 21, 2017 at 00:13

    To use CH2 of TIM2 as an external clock:

    - don't enable another TIM2_CH1 pin (PA5)

    - do enable as AF a TIM2_CH2 pin (e.g. PA1) and set it to AF1

    - don't enable input capture for CH2 ( Tim2_CCMR1_Input |= 0x100;)

    - select TI2FP2 (which is the filtered version of CH2, see timer's block diagram) as Slave Mode Input, TIMx_SMCR.TS= 0b110

    - not 0x111 but 0b111 (if your compiler supports it, or simply 7) in  TIM2_SMCR.SMS = 0x111;  to select extern clock mode 1

    JW