Forum: Mikrocontroller und Digitale Elektronik PWM Funktioniert nicht


von STM Neuling (Gast)


Lesenswert?

Hallo, ich sitze seit heute morgen daran eine PWM ans Laufen zu kriegen. 
Langsam bin ich so durch, dass ich meine Fehler anscheinend nicht mehr 
entdecke...Könnte mal einer drüberschauen? Vielleicht ist es was ganz 
banales aber ich komme nicht drauf.

Ziel ist es, PWM mit 0-100%, 10% Startwert.

Ich verwende einen Cortex M030 48Pin, Als PWM Ausgang habe ich den 
PortAPin9(Tim1,Ch2) verwendet.
1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
2
    TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
3
    TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
4
    TIM_TimeBase_InitStructure.TIM_Period = 100;
5
    TIM_TimeBase_InitStructure.TIM_Prescaler = 200;
6
    TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
7
8
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
9
    TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
10
    TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Set;
11
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
12
    TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High;
13
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
14
    TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
15
    TIM_OCInitStruct.TIM_Pulse = 10;
16
    TIM_OC2Init(TIM1, &TIM_OCInitStruct);
17
    TIM_Cmd(TIM1, ENABLE);

von da1l6 (Gast)


Lesenswert?

Hallo

Pin Konfiguration (AF)?
Takt für GPIO Block eingeschaltet?

da1l6

von STM Neuling (Gast)


Lesenswert?

Hallo, ich denke mal ja  :-)

Sorry, den Code hatte ich vergessen

[c]
  InitGpio.GPIO_Pin = (GPIO_Pin_9);
  /*
  //     *   Pin 9  -  PWM Motor            -  A
  */
  InitGpio.GPIO_Mode = GPIO_Mode_AF;
  InitGpio.GPIO_Speed = GPIO_Speed_Level_3;
  InitGpio.GPIO_OType = GPIO_OType_PP;
  InitGpio.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &InitGpio);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);[\n]


und
[c]  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);[\c]

von Ingo L. (corrtexx)


Lesenswert?

Versuch das mal, musste aber noch anpassen:
1
void Init_PWM ( void )
2
{
3
  GPIO_InitTypeDef GPIO_InitStructure_A, GPIO_InitStructure_B;
4
  GPIO_StructInit(&GPIO_InitStructure_A);
5
  GPIO_StructInit(&GPIO_InitStructure_B);
6
7
  // IOs als Ausgang initialisieren (PortA und PortB)
8
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
9
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
10
11
  GPIO_InitStructure_A.GPIO_Mode = GPIO_Mode_AF;
12
  GPIO_InitStructure_A.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ;
13
  GPIO_InitStructure_A.GPIO_OType = GPIO_OType_PP;
14
  GPIO_InitStructure_A.GPIO_PuPd =  GPIO_PuPd_DOWN;
15
  GPIO_InitStructure_A.GPIO_Speed = GPIO_Speed_100MHz;
16
  GPIO_Init(GPIOA, &GPIO_InitStructure_A);
17
18
  /////////////////////////////////////////////////////////
19
  GPIO_InitStructure_B.GPIO_Mode = GPIO_Mode_AF;
20
  GPIO_InitStructure_B.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
21
  GPIO_InitStructure_B.GPIO_OType = GPIO_OType_PP;
22
  GPIO_InitStructure_B.GPIO_PuPd =  GPIO_PuPd_DOWN;
23
  GPIO_InitStructure_B.GPIO_Speed = GPIO_Speed_100MHz;
24
  GPIO_Init(GPIOB, &GPIO_InitStructure_B);
25
26
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1);
27
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
28
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_TIM1);
29
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
30
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM1);
31
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM1);
32
33
  /////////////////////////////////////////////////////////
34
  /////////////////////////////////////////////////////////
35
  // Timer
36
  TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
37
  TIM_TimeBaseStructInit(&TIM_TimeBase_InitStructure);
38
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
39
40
  TIM_TimeBase_InitStructure.TIM_Prescaler = PRESCALER_8KHZ;
41
  TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
42
  TIM_TimeBase_InitStructure.TIM_Period = PWM_PERIOD;
43
  TIM_TimeBase_InitStructure.TIM_ClockDivision = 0;
44
  TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
45
46
  TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
47
  TIM_UpdateRequestConfig(TIM1, TIM_UpdateSource_Global);
48
  TIM_UpdateDisableConfig(TIM1,DISABLE);
49
50
51
  /////////////////////////////////////////////////////////
52
  /////////////////////////////////////////////////////////
53
54
  // PWM
55
  TIM_OCInitTypeDef TIM_OCInitStructure;
56
  TIM_OCStructInit(&TIM_OCInitStructure);
57
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
58
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
59
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
60
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
61
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
62
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
63
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
64
65
  TIM_OCInitStructure.TIM_Pulse = 0;
66
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
67
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
68
69
  TIM_OCInitStructure.TIM_Pulse = 0;
70
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
71
  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
72
73
  TIM_OCInitStructure.TIM_Pulse = 0;
74
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
75
  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
76
77
  TIM_ARRPreloadConfig(TIM1, ENABLE);
78
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
79
80
81
  /////////////////////////////////////////////////////////
82
  /////////////////////////////////////////////////////////
83
  // Dead-Time
84
  TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
85
  TIM_BDTRStructInit(&TIM_BDTRInitStructure);
86
  /* Automatic Output enable, Break, dead time and lock configuration*/
87
  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
88
  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
89
  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
90
  TIM_BDTRInitStructure.TIM_DeadTime = 223;
91
  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
92
  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
93
  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
94
  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
95
96
  /* Enable Timer */
97
  TIM_Cmd( TIM1, ENABLE );
98
99
  DISABLE_PWM();
100
  // Set Duty
101
  TIM_SetCompare1(TIM1,PWM_PERIOD/2);
102
  TIM_SetCompare2(TIM1,PWM_PERIOD/2);
103
  TIM_SetCompare3(TIM1,PWM_PERIOD/2);
104
}

von Dominik B. (odysseus1710)


Lesenswert?

"As the preload registers are transferred to the shadow registers only 
when an update event occurs, before starting the counter, you have to 
initialize all the registers by setting the UG
bit in the TIMx_EGR register."
1
TIM1->EGR |= (1 << TIM_EGR_UG);

Könnte ein Grund sein. Denke mal, das ist beim M030 ähnlich

von STM Neuling (Gast)


Lesenswert?

TIM_CtrlPWMOutputs(TIM1, ENABLE);


Der hat gefehlt...

Danke für die Unterstützung...

von Ingo L. (corrtexx)


Lesenswert?

Gern

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.