Forum: Mikrocontroller und Digitale Elektronik STM32F103 Timer 3


von Jan (Gast)


Lesenswert?

Hallo,

ich versuche mit Timer 3 und Timer 1 PWM Signale zu erzeugen. Mit Timer 
1 funktioniert es wie ich mir geadacht habe. Leider kommt bei Timer 3 
nur Unsinn raus. Pins habe ich den Code geschrieben. Sieht jemand den 
Fehler?
Ich teste die Ausgänge mit PulsView.

Dank und Gruß
Jan

main.c
1
TIM_SetCompare1 (TIM1, 200);  // DISPLAY 1
2
TIM_SetCompare2 (TIM1, 400);  // DISPLAY 2
3
TIM_SetCompare3 (TIM1, 500);  // CHARGE PUMP
4
5
TIM_SetCompare1 (TIM3, 499);  // Test1
6
TIM_SetCompare2 (TIM3, 499);  // Test2

pwm.c
1
#define PWMPINCHARGEPUMP GPIO_Pin_15  //TIM1_CH3N
2
#define PWMPINDISPLAY100 GPIO_Pin_14   //TIM1_CH2N
3
#define PWMPINDISPLAY120 GPIO_Pin_13   //TIM1_CH1N
4
5
#define PWMTIM1RCC    RCC_APB2Periph_GPIOB
6
#define PWMTIM1PORT    GPIOB
7
8
void PwmTim1Init()
9
{
10
  GPIO_InitTypeDef GPIO_InitStructure;
11
  TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
12
  TIM_OCInitTypeDef TIM_OC_InitStructure;
13
14
15
  RCC_APB2PeriphClockCmd(PWMTIM1RCC | RCC_APB2Periph_AFIO | RCC_APB2Periph_TIM1, ENABLE);
16
17
18
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
19
20
  GPIO_InitStructure.GPIO_Pin = PWMPINCHARGEPUMP | PWMPINDISPLAY100 | PWMPINDISPLAY120;
21
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
22
  GPIO_Init(PWMTIM1PORT, &GPIO_InitStructure);
23
24
25
  TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
26
  TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
27
  TIM_TimeBase_InitStructure.TIM_Period = 999;
28
  TIM_TimeBase_InitStructure.TIM_Prescaler = 0;
29
  TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
30
31
  TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
32
  TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
33
  TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
34
  TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
35
  TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
36
  //TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
37
  //TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
38
  TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Disable;
39
  TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
40
  TIM_OC_InitStructure.TIM_Pulse = 100;
41
  TIM_OC1Init(TIM1, &TIM_OC_InitStructure);
42
  TIM_OC2Init(TIM1, &TIM_OC_InitStructure);
43
  TIM_OC3Init(TIM1, &TIM_OC_InitStructure);
44
  //TIM_OC4Init(TIM1, &TIM_OC_InitStructure);
45
46
  TIM_Cmd(TIM1, ENABLE);
47
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
48
49
50
}
51
52
#define PWMSPK1     GPIO_Pin_6  //TIM3_CH1
53
#define PWMSPK2     GPIO_Pin_7   //TIM3_CH2
54
55
56
#define PWMTIM3RCC    RCC_APB2Periph_GPIOA
57
#define PWMTIM3PORT    GPIOA
58
59
60
61
62
void PwmTim3Init()
63
{
64
  GPIO_InitTypeDef GPIO_InitStructure;
65
  TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
66
  TIM_OCInitTypeDef TIM_OC_InitStructure;
67
68
  RCC_APB2PeriphClockCmd(PWMTIM3RCC | RCC_APB2Periph_AFIO, ENABLE);
69
70
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
71
72
  //RCC_APB2PeriphClockCmd(0xFFFF, ENABLE);
73
74
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
75
  GPIO_InitStructure.GPIO_Pin = PWMSPK1 | PWMSPK2;
76
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
77
  GPIO_Init(PWMTIM3PORT, &GPIO_InitStructure);
78
79
80
  TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
81
  TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
82
  TIM_TimeBase_InitStructure.TIM_Period = 999;
83
  TIM_TimeBase_InitStructure.TIM_Prescaler = 0;
84
  TIM_TimeBaseInit(TIM1, &TIM_TimeBase_InitStructure);
85
86
  TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
87
  TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
88
  TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
89
  TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
90
  TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
91
TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
92
TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
93
  TIM_OC_InitStructure.TIM_Pulse = 100;
94
  TIM_OC1Init(TIM3, &TIM_OC_InitStructure);
95
  TIM_OC2Init(TIM3, &TIM_OC_InitStructure);
96
97
  TIM_Cmd(TIM3, ENABLE);
98
  TIM_CtrlPWMOutputs(TIM3, ENABLE);
99
100
}

von Jan (Gast)


Angehängte Dateien:

Lesenswert?

Sorry Bilder vergessen

von Jan (Gast)


Lesenswert?

Keiner eine Idee?
Kann ich noch was testen ?

von Volkvorg (Gast)


Lesenswert?

Auf die Schnelle in deiner Timer3 Init Funktion
Hast du bei der TimeBaseInit() TIM1 und nicht TIM3 stehen.

von Jan (Gast)


Lesenswert?

Vielen Dank... Asche auf mein Haupt.

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.