von
Guest (Gast)
18.11.2014 17:28
Hallo, ich habe eine Frage bezüglich dem Timer bzw. vielleicht hat
jemand ein schärferes Auge als ich :)
Ich habe mir das STM32F0Discovery Board organisiert und versuche darauf
mit den Timer 1 eine PWM zu erzeugen, sozusagen als Einsteiger-Projekt.
Da ich nun aber den ganzen Tag mit der Fehlersuche verbracht habe, wende
ich mich mal an dieses super Forum in der Hoffnung das jemand eventuell
einen kleinen Bug entdeckt, denn am Pin 8 (Tim1 Ch1) kommt mit
nachfolgendem Code kein Signal an
1
2 int main ( void )
3 {
4 GPIO_InitTypeDef GPIO_InitStructure ;
5 TIM_OCInitTypeDef TIM_OC_InitStructure ;
6 TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure ;
7
8 RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_GPIOA , ENABLE );
9 RCC_APB2PeriphClockCmd ( RCC_APB2Periph_TIM1 , ENABLE );
10
11 GPIO_InitStructure . GPIO_Pin = GPIO_Pin_8 ;
12 GPIO_InitStructure . GPIO_Mode = GPIO_Mode_AF ;
13 GPIO_InitStructure . GPIO_OType = GPIO_OType_PP ;
14 GPIO_InitStructure . GPIO_Speed = GPIO_Speed_50MHz ;
15 GPIO_InitStructure . GPIO_PuPd = GPIO_PuPd_NOPULL ;
16 GPIO_PinAFConfig ( GPIOA , GPIO_PinSource8 , GPIO_AF_2 );
17
18 TIM_TimeBase_InitStructure . TIM_ClockDivision = TIM_CKD_DIV1 ;
19 TIM_TimeBase_InitStructure . TIM_CounterMode = TIM_CounterMode_Up ;
20 TIM_TimeBase_InitStructure . TIM_Period = 999 ;
21 TIM_TimeBase_InitStructure . TIM_Prescaler = 10 ;
22 TIM_TimeBase_InitStructure . TIM_RepetitionCounter = 1 ;
23 TIM_TimeBaseInit ( TIM1 , & TIM_TimeBase_InitStructure );
24
25 TIM_OC_InitStructure . TIM_OCMode = TIM_OCMode_PWM1 ;
26 TIM_OC_InitStructure . TIM_OCIdleState = TIM_OCIdleState_Reset ;
27 TIM_OC_InitStructure . TIM_OCPolarity = TIM_OCPolarity_High ;
28 TIM_OC_InitStructure . TIM_OCNPolarity = TIM_OCNPolarity_Low ;
29 TIM_OC_InitStructure . TIM_OutputState = TIM_OutputState_Enable ;
30 TIM_OC_InitStructure . TIM_OutputNState = TIM_OutputNState_Disable ;
31 TIM_OC_InitStructure . TIM_Pulse = 100 ;
32 TIM_OC1PreloadConfig ( TIM1 , TIM_OCPreload_Enable );
33 TIM_OC1Init ( TIM1 , & TIM_OC_InitStructure );
34
35 TIM_ARRPreloadConfig ( TIM1 , DISABLE );
36 TIM_CtrlPWMOutputs ( TIM1 , ENABLE );
37 TIM_Cmd ( TIM1 , ENABLE );
38 while ( 1 ){
39 }
40 }
Wahrscheinlich habe ich irgend eine Kleinigkeit vergessen aber es fällt
mir einfach nicht auf.
Für jeden Ratschlag bin ich schon im Voraus dankbar.
Grüße
Systeminit am anfang deiner main?
Oder wird die schon vor der main aufgerufen?
Gruß
Rainer
von
Programmierer (Gast)
18.11.2014 17:47
Du rufst GPIO_Init ja gar nicht auf...
von
Guest (Gast)
18.11.2014 17:55
Sorry, habs bemerkt, ist mir wie Schuppen von den Augen gefallen, aber
das ändert leider nichts daran das es nicht funktioniert :/
1 int main ( void )
2 {
3 SystemInit ();
4
5 GPIO_InitTypeDef GPIO_InitStructure ;
6 TIM_OCInitTypeDef TIM_OC_InitStructure ;
7 TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure ;
8
9 RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_GPIOA , ENABLE );
10 RCC_APB2PeriphClockCmd ( RCC_APB2Periph_TIM1 , ENABLE );
11
12 GPIO_StructInit ( & GPIO_InitStructure );
13 GPIO_InitStructure . GPIO_Pin = GPIO_Pin_8 ;
14 GPIO_InitStructure . GPIO_Mode = GPIO_Mode_AF ;
15 GPIO_InitStructure . GPIO_OType = GPIO_OType_PP ;
16 GPIO_InitStructure . GPIO_Speed = GPIO_Speed_50MHz ;
17 GPIO_InitStructure . GPIO_PuPd = GPIO_PuPd_DOWN ;
18 GPIO_PinAFConfig ( GPIOA , GPIO_PinSource8 , GPIO_AF_2 );
19
20 TIM_TimeBaseStructInit ( & TIM_TimeBase_InitStructure );
21 TIM_TimeBase_InitStructure . TIM_ClockDivision = TIM_CKD_DIV1 ;
22 TIM_TimeBase_InitStructure . TIM_CounterMode = TIM_CounterMode_Up ;
23 TIM_TimeBase_InitStructure . TIM_Period = 999 ;
24 TIM_TimeBase_InitStructure . TIM_Prescaler = 10 ;
25 TIM_TimeBase_InitStructure . TIM_RepetitionCounter = 1 ;
26 TIM_TimeBaseInit ( TIM1 , & TIM_TimeBase_InitStructure );
27
28 TIM_OCStructInit ( & TIM_OC_InitStructure );
29 TIM_OC_InitStructure . TIM_OCMode = TIM_OCMode_PWM1 ;
30 TIM_OC_InitStructure . TIM_OCIdleState = TIM_OCIdleState_Reset ;
31 TIM_OC_InitStructure . TIM_OCPolarity = TIM_OCPolarity_High ;
32 TIM_OC_InitStructure . TIM_OCNPolarity = TIM_OCNPolarity_Low ;
33 TIM_OC_InitStructure . TIM_OutputState = TIM_OutputState_Enable ;
34 TIM_OC_InitStructure . TIM_OutputNState = TIM_OutputNState_Disable ;
35 TIM_OC_InitStructure . TIM_Pulse = 100 ;
36 TIM_OC1PreloadConfig ( TIM1 , TIM_OCPreload_Enable );
37 TIM_OC1Init ( TIM1 , & TIM_OC_InitStructure );
38
39 TIM_ARRPreloadConfig ( TIM1 , DISABLE );
40 TIM_CtrlPWMOutputs ( TIM1 , ENABLE );
41 TIM_Cmd ( TIM1 , ENABLE );
42 while ( 1 ){
43 }
44 }
von
Guest (Gast)
18.11.2014 18:02
Habs jetzt doch noch gefunden :)
Dank dem Tipp von "Programmierer". Danke nochmals dafür!
Hier der funktionierende Code:
1 int main ( void )
2 {
3 SystemInit ();
4
5 GPIO_InitTypeDef GPIO_InitStructure ;
6 TIM_OCInitTypeDef TIM_OC_InitStructure ;
7 TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure ;
8
9 RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_GPIOA , ENABLE );
10 RCC_APB2PeriphClockCmd ( RCC_APB2Periph_TIM1 , ENABLE );
11
12 GPIO_StructInit ( & GPIO_InitStructure );
13 GPIO_InitStructure . GPIO_Pin = GPIO_Pin_8 ;
14 GPIO_InitStructure . GPIO_Mode = GPIO_Mode_AF ;
15 GPIO_InitStructure . GPIO_OType = GPIO_OType_PP ;
16 GPIO_InitStructure . GPIO_Speed = GPIO_Speed_50MHz ;
17 GPIO_InitStructure . GPIO_PuPd = GPIO_PuPd_DOWN ;
18 GPIO_Init ( GPIOA , & GPIO_InitStructure );
19 GPIO_PinAFConfig ( GPIOA , GPIO_PinSource8 , GPIO_AF_2 );
20
21 TIM_TimeBaseStructInit ( & TIM_TimeBase_InitStructure );
22 TIM_TimeBase_InitStructure . TIM_ClockDivision = TIM_CKD_DIV1 ;
23 TIM_TimeBase_InitStructure . TIM_CounterMode = TIM_CounterMode_Up ;
24 TIM_TimeBase_InitStructure . TIM_Period = 999 ;
25 TIM_TimeBase_InitStructure . TIM_Prescaler = 10 ;
26 TIM_TimeBase_InitStructure . TIM_RepetitionCounter = 1 ;
27 TIM_TimeBaseInit ( TIM1 , & TIM_TimeBase_InitStructure );
28
29 TIM_OCStructInit ( & TIM_OC_InitStructure );
30 TIM_OC_InitStructure . TIM_OCMode = TIM_OCMode_PWM1 ;
31 TIM_OC_InitStructure . TIM_OCIdleState = TIM_OCIdleState_Reset ;
32 TIM_OC_InitStructure . TIM_OCPolarity = TIM_OCPolarity_High ;
33 TIM_OC_InitStructure . TIM_OCNPolarity = TIM_OCNPolarity_Low ;
34 TIM_OC_InitStructure . TIM_OutputState = TIM_OutputState_Enable ;
35 TIM_OC_InitStructure . TIM_OutputNState = TIM_OutputNState_Disable ;
36 TIM_OC_InitStructure . TIM_Pulse = 100 ;
37 TIM_OC1PreloadConfig ( TIM1 , TIM_OCPreload_Enable );
38 TIM_OC1Init ( TIM1 , & TIM_OC_InitStructure );
39
40 TIM_ARRPreloadConfig ( TIM1 , DISABLE );
41 TIM_CtrlPWMOutputs ( TIM1 , ENABLE );
42 TIM_Cmd ( TIM1 , ENABLE );
43 while ( 1 ){
44 }
45 }
Es fehlt immer noch ein: 1 GPIO_Init ( GPIOA , & GPIO_InitStructure );
von
Guest (Gast)
18.11.2014 19:38
Danke, daran hing es auch letztendlich.
Ich dachte der Befehl 1 GPIO_PinAFConfig ( GPIOA , GPIO_PinSource8 , GPIO_AF_2 );
reicht aus.
Grüße
von
Programmierer (Gast)
19.11.2014 08:18
Wie soll der denn den Pin korrekt konfigurieren, wenn du dem das
"GPIO_InitStructure" nicht übergibst? Ist dir nicht aufgefallen, dass du
GPIO_InitStructure zwar initialisierst, aber nie verwendest?
Guest schrieb:
> GPIO_Init(GPIOA, &GPIO_InitStructure);
Im Beitrag von 18:02 Uhr ist es jedenfalls drin, wenn auch etwas
versteckt.
von
Programmierer (Gast)
19.11.2014 09:26
Matthias Sch. schrieb:
> Im Beitrag von 18:02 Uhr ist es jedenfalls drin, wenn auch etwas
> versteckt.
Da funktioniert es ja dann auch. Es ging mir um die ersten beiden
Posts...
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.