Forum: Mikrocontroller und Digitale Elektronik STM32 Timer Cookbook PWM - Beispiel 6.2


von Massi87 (Gast)



Lesenswert?

Hi zusammen,

ich beschäftige mich gerade mit Timern und möchte bestimmte Beispiele 
vom STM32 General-purpose timer cookbook mit meinem STM32 Nucleo-F446RE 
Board nachprogrammieren.

Das Beispiel von Kapitel 3.2 mit TIM1 funktioniert wie erwartet:
TIM1 startet und zählt bis Capture Compare Register 1 (CCR1) hoch und 
wechselt die Polarität am Ausgang (GPIOA_PIN_8) von LOW auf HIGH und 
zählt bis zum Wert des Auto-Reload Registers (ARR) und wechselt die 
Polarität wieder auf LOW zurück. Das Ganze funktioniert kontinuierlich, 
da TIM1 kein Slave von einem Master ist. Der PWM Mode 2 wird verwendet


Jetzt möchte ich das Beispiel 6.2 ausprobieren bei dem folgendes 
Szenario gegeben ist (der Code mit dem Schreiben der Register ist in der 
PDF von STM enthalten:
TIM1 wird für die Erzeugung des PWM Signals verwendet und ist ein 
Slave-Timer, der von TIM2 (Master) getriggert wird.
TIM1 verwendet nun PWM Mode 1 und beim Start von TIM1 ist die Polarität 
des Ausgangs HIGH bis zum Erreichen des Wertes von CCR1 und wechselt zu 
LOW bis der Wert ARR erreicht ist. Das ganze wird 5 mal wiederholt und 
danach bleibt die Polarität auf LOW, bis TIM2 dann TIM1 wieder triggert 
und sich das ganze wiederholt.
Ich habe den Code entsprechend kopiert und einige Werte geändert, die 
sich zwischen dem Board des Beispiels und meinem unterscheiden. Doch 
leider bekomme ich das Signal nicht so wie erwartet: das Signal ist 
genauso wie in 3.2, obwohl hier statt PWM Mode 2 nun PWM Mode 1 
verwendet wird.
Damit ich den Start von TIM2 (Master) sehen kann, habe ich noch einen 
zweiten Timer (TIM3) als Slave konfiguiert, der einen Interrupt erzeug 
und immer beim Start von TIM2 eine LED toggelt.

Kann mir bitte jemand sagen, was ich falsch mache? Ich drehe mich gerade 
echt im Kreis bei der Suche nach der Lösung.

Im Anhang ist die PDF von dem Timer Cookbook, der Code den ich 
geschrieben habe und ein Bild, was die erwartete Ausgabe und die 
momentane Ausgabe von TIM1 darstellt.


Hier auch noch mal der Code, falls Bedenken bezüglich des Downloads der 
C-Datei bestehen:
1
void timer1_Init(){
2
  /* TIM1 is on APB2 with clock speed 180MHz */
3
  /* activate clock for TIM1 peripheral */
4
  __HAL_RCC_TIM1_CLK_ENABLE();
5
6
  /* neue Struktur */
7
  /* Prescaler */
8
  TIM1->PSC = 35999;
9
  /* Auto-Reload-Register */
10
  TIM1->ARR = 19999;
11
  /* repetition counter if pulse should be displayed more than 1 time */
12
  TIM1->RCR = 0;
13
  /* Set the Capture Compare Register: Pulse */
14
  TIM1->CCR1 = 4999;
15
  /* Set Clock */
16
  TIM1->CR1 &= ~ TIM_CR1_CKD;
17
  TIM1->CR1 |= TIM_CLOCKDIVISION_DIV1;
18
  /* set counter mode */
19
  TIM1->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
20
  TIM1->CR1 |= TIM_COUNTERMODE_UP;
21
  /* Reset the Output Compare Mode Bits */
22
  TIM1->CCMR1 &= ~TIM_CCMR1_OC1M;
23
  TIM1->CCMR1 &= ~TIM_CCMR1_CC1S;
24
  /* Select the Output Compare (OC) Mode */
25
  /*
26
   * Mode 1: In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
27
   * else inactive. In downcounting, channel 1 is inactive (OC1REF=‘0’) as long as
28
   * TIMx_CNT>TIMx_CCR1 else active (OC1REF=’1’).
29
   *
30
   * Mode 2: In upcounting, channel 1 is inactive as long as TIMx_CNT<TIMx_CCR1
31
   * else active. In downcounting, channel 1 is active as long as TIMx_CNT>TIMx_CCR1 else
32
   * inactive.
33
   */
34
  TIM1->CCMR1 |= TIM_OCMODE_PWM1; //(TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1);
35
36
  /********** One Pulse Mode Configuration **********/
37
  /* One Pulse Mode */
38
  TIM1->CR1 |= TIM_CR1_OPM;
39
  /********** Slave Mode configuration: Trigger Mode **********/
40
  TIM1->SMCR &= ~TIM_SMCR_TS;
41
  TIM1->SMCR |= TIM_TS_ITR1;
42
  TIM1->SMCR &= ~TIM_SMCR_SMS;
43
  TIM1->SMCR |= (TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1); // = TIM_SLAVEMODE_TRIGGER
44
  /*************************************************/
45
  /* Set the Output Compare Preload enable bit for channel 1 */
46
  TIM1->CCMR1 |= TIM_CCMR1_OC1PE;
47
  /* update event to reload registers - set the UG Bit to enable UEV  */
48
  TIM1->EGR = TIM_EGR_UG;
49
  /* Enable the TIM1 Main Output */
50
  TIM1->BDTR |= TIM_BDTR_MOE;
51
  /* Reset and set the Output N Polarity level to LOW */
52
  TIM1->CCER &= ~TIM_CCER_CC1P;
53
54
  /* Set the Output Compare Polarity */
55
  TIM1->CCER |= TIM_OCPOLARITY_LOW; //TIM_CCER_CC1P;
56
  /* Enable the Capture compare channel 1 on High Level*/
57
  TIM1->CCER |= TIM_CCER_CC1E;
58
  
59
  /* Initialization of GPIO_PIN_8 for PWM output */
60
  GPIO_InitTypeDef GPIO_InitStruct = {0};
61
  __HAL_RCC_GPIOA_CLK_ENABLE();
62
  GPIO_InitStruct.Pin = GPIO_PIN_8;
63
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
64
  GPIO_InitStruct.Pull = GPIO_PULLUP;
65
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
66
  GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
67
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
68
  
69
}
70
71
void timer2_Init(){
72
  /* TIM2 is on APB1 with clock speed 90MHz */
73
  /* activate clock for TIM2 peripheral */
74
  __HAL_RCC_TIM2_CLK_ENABLE();
75
  /* Prescaler */
76
  TIM2->PSC = 44999; //44999; /* Oszillator might have problems. Prescaler of 44999 should get at period of 0,0005 but doesn't */
77
  /* set counter mode */
78
  TIM2->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
79
  TIM2->CR1 |= TIM_COUNTERMODE_UP;
80
  /* Auto-Reload Register */
81
  TIM2->ARR = 19999; //19999;
82
  /* Set Clock Division */
83
  TIM2->CR1 &= ~ TIM_CR1_CKD;
84
  TIM2->CR1 |= TIM_CLOCKDIVISION_DIV1;
85
86
  /* Update Event - if this timer is configured as Master with output TRGO_UPDATE
87
   * the slave timer TIM 1 will get a trigger and run one time
88
   *
89
   * This bit can be set by software, it is automatically cleared by hardware.
90
   * 0: No action
91
   * 1: Reinitialize the counter and generates an update of the registers. Note that the prescaler
92
   * counter is cleared too (anyway the prescaler ratio is not affected). For more see manual. */
93
  TIM2->EGR = TIM_EGR_UG;
94
95
  /* Set Clock Source */
96
  TIM2->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_TS | TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
97
98
  /* Master Configuration */
99
  TIM2->CR2 &= ~TIM_CR2_MMS;
100
  TIM2->CR2 |= TIM_TRGO_UPDATE;
101
102
103
  /* Enable Counter: */
104
  TIM2->CR1 = TIM_CR1_CEN;
105
106
}

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.