Forum: Mikrocontroller und Digitale Elektronik STM32F4 Timer2 im Encoder Mode plus Capture Compare


von Mach F. (machfax)


Lesenswert?

Hallo

Ich habe Timer2 als Encoder Timer konfiguriert, was auch super 
funktioniert. Jetzt möchte ich zusätzlich einen TimerWert als Compare 
Wert setzen, der mir einen Interrupt auslöst sobald die Encoder-Position 
diesen Wert erreicht, der Timer soll aber weiterzählen.
Geht das überhaupt? Mit der Konfiguration unten geht es leider nicht.
Beispiel:
Encoder Timer Werte steht auf 1000, sobald ich den Encoder drehe, wird 
der Timerstand erhöht bis 10000 usw. Wird die Position von 5000 erreicht 
möchte ich einen Interrupt, der Zähler soll aber normal weiterlaufen.

void PositionEncoder_Config(void)
{
    GPIO_InitTypeDef   GPIO_InitStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 0xffff;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    /* Configure the timer */
    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, 
TIM_ICPolarity_Rising, TIM_ICPolarity_Falling);

    TIM_SetCounter (TIM2, (uint32_t) Encoder_Position_Default); 
//set the Timer to the default init value (middle)

    /* TIM2 counter enable */
    TIM_Cmd(TIM2, ENABLE);

    //TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);

  /////////////////////
  // configure PWM mode and duration
  TIM_OCStructInit (&TIM_OCInitStructure);
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; 
//TIM_OCMode_Timing
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; 
//TIM_OutputState_Enable
  //TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set 
the duty cycle / pulse here!
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);
}

von Stefan Schulz (Gast)


Lesenswert?

1
void PositionEncoder_Config(void)
2
{
3
    GPIO_InitTypeDef   GPIO_InitStructure;
4
    TIM_OCInitTypeDef  TIM_OCInitStructure;
5
6
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
7
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
8
9
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
10
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
11
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
12
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5;
13
14
    GPIO_Init(GPIOA, &GPIO_InitStructure);
15
16
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
17
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);
18
19
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
20
    TIM_TimeBaseStructure.TIM_Period = 0xffff;
21
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
22
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
23
24
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
25
26
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
27
    /* Configure the timer */
28
    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, 
29
TIM_ICPolarity_Rising, TIM_ICPolarity_Falling);
30
31
    TIM_SetCounter (TIM2, (uint32_t) Encoder_Position_Default); 
32
//set the Timer to the default init value (middle)
33
34
    /* TIM2 counter enable */
35
    TIM_Cmd(TIM2, ENABLE);
36
37
    //TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
38
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
39
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
40
41
  /////////////////////
42
  // configure PWM mode and duration
43
  TIM_OCStructInit (&TIM_OCInitStructure);
44
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; 
45
//TIM_OCMode_Timing
46
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; 
47
//TIM_OutputState_Enable
48
  //TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set 
49
the duty cycle / pulse here!
50
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
51
  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
52
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);
53
}

ich hab das mal in "c" "/c" Brackets gesetzt

von Mach F. (machfax)


Lesenswert?

Stefan Schulz schrieb:
> ich hab das mal in "c" "/c" Brackets gesetzt

Danke :-)

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.