Forum: Mikrocontroller und Digitale Elektronik STM32 PWM Output const 3.3V


von JASEN (Gast)


Lesenswert?

Hi,

ich hab das Problem das ich nur konstante 3,3V am Ausgnag von PA6,Pa7, 
PB0 und PB1 sehe.

Ist n STm32F103RBT6 STm32H103-Board von Olimex.

Nach dem Programm sollten z.B. an PA6 TIM3_CH1 eine PWM mit 36khz und 
Duty Cycle von 50% zu sehen sein aber das Oszi zeigt mir was anderes, 
eingestellt hab ich dieses auf 10µs pro div.

Ich hab n Tektronix 2225 50Mhz ich denke damit sollte ich ja das 
Ausgangssignal angucken können.
1
#include "stm32f10x_lib.h"
2
#include "stm32f10x_map.h"
3
#include "stm32f10x_rcc.h"
4
#include "stm32f10x_gpio.h"
5
6
#include "bits.h"
7
8
#define STACK_TOP 0x20000800
9
#define NVIC_CCR ((volatile unsigned long *)(0xE000ED14))
10
11
//--------------- Private Functions ---------------------------------------------
12
13
void nmi_handler(void);
14
void hardfault_handler(void);
15
int main(void);
16
void myDelay(unsigned long delay );
17
void RCC_Configuration(void);
18
void GPIO_Configuration(void);
19
20
21
//-------------------------------------------------------------------------------
22
// Define the vector table
23
  unsigned int * myvectors[4]
24
     __attribute__ ((section("vectors")))= {
25
     (unsigned int *)  0x20000800,  // stack pointer
26
     (unsigned int *)   main,    // code entry point
27
     (unsigned int *)  nmi_handler,    // NMI handler (not really)
28
     (unsigned int *)  hardfault_handler    // hard fault handler (let's hope not)
29
};
30
31
// VARIABLES
32
33
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
34
35
TIM_OCInitTypeDef  TIM_OCInitStructure;
36
u16 CCR1_Val = 500;
37
u16 CCR2_Val = 375;
38
u16 CCR3_Val = 250;
39
u16 CCR4_Val = 125;
40
ErrorStatus HSEStartUpStatus;
41
42
GPIO_InitTypeDef GPIO_InitStructure;
43
44
//------------------------- MAIN -----------------------------------------------
45
46
int main(void)
47
{
48
49
  *NVIC_CCR = *NVIC_CCR | 0x200; // Set STKALIGN in NVIC
50
51
  // System Clocks Configuration
52
  RCC_Configuration();
53
54
  // GPIO Configuration
55
  GPIO_Configuration();
56
57
   /* -----------------------------------------------------------------------
58
      TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
59
      TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
60
      TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
61
      TIM3 Frequency = 36 KHz.
62
      TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
63
      TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
64
      TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
65
      TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
66
     ----------------------------------------------------------------------- */
67
68
  // Time base configuration
69
    TIM_TimeBaseStructure.TIM_Period = 999;
70
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
71
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
72
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
73
74
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
75
76
    // PWM1 Mode configuration: Channel1
77
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
78
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
79
    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
80
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
81
82
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
83
84
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
85
86
    // PWM1 Mode configuration: Channel2
87
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
88
    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
89
90
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
91
92
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
93
94
    // PWM1 Mode configuration: Channel3
95
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
96
    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
97
98
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
99
100
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
101
102
    // PWM1 Mode configuration: Channel4
103
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
104
    TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
105
106
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
107
108
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
109
110
    TIM_ARRPreloadConfig(TIM3, ENABLE);
111
112
    // TIM3 enable counter
113
    TIM_Cmd(TIM3, ENABLE);
114
115
    while (1)
116
    {}
117
118
119
}
120
121
//SUB Routines-----------------------------------------------------------------
122
123
void nmi_handler(void)
124
{
125
  return ;
126
}
127
128
void hardfault_handler(void)
129
{
130
  return ;
131
}
132
//Functions definitions
133
void myDelay(unsigned long delay )
134
{
135
  while(delay) delay--;
136
}
137
138
/*************************************************************************
139
 * Function Name: Clk_Init
140
 * Parameters: Int32U Frequency
141
 * Return: Int32U
142
 *
143
 * Description: Init clock system
144
 *
145
 *************************************************************************/
146
147
void RCC_Configuration (void)
148
{
149
  // RCC system reset(for debug purpose)
150
  RCC_DeInit();
151
152
  // Enable HSE
153
  RCC_HSEConfig(RCC_HSE_ON);
154
155
  // Wait till HSE is ready
156
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
157
158
  if (HSEStartUpStatus == SUCCESS)
159
    {
160
      // Enable Prefetch Buffer
161
      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
162
163
      // Flash 2 wait state
164
      FLASH_SetLatency(FLASH_Latency_2);
165
166
      // HCLK = SYSCLK
167
      RCC_HCLKConfig(RCC_SYSCLK_Div1);
168
169
      // PCLK2 = HCLK
170
      RCC_PCLK2Config(RCC_HCLK_Div1);
171
172
      // PCLK1 = HCLK/4
173
      RCC_PCLK1Config(RCC_HCLK_Div4);
174
175
      // PLLCLK = 8MHz * 9 = 72 MHz
176
      RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
177
178
      // Enable PLL
179
      RCC_PLLCmd(ENABLE);
180
181
      // Wait till PLL is ready
182
          while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
183
          {}
184
185
          // Select PLL as system clock source
186
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
187
188
          // Wait till PLL is used as system clock source
189
          while (RCC_GetSYSCLKSource() != 0x08)
190
          {}
191
        }
192
193
    // TIM3 clock enable
194
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
195
196
    // GPIOA and GPIOB clock enable
197
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
198
199
}
200
201
202
203
void GPIO_Configuration(void)
204
{
205
  GPIO_InitTypeDef GPIO_InitStructure;
206
207
  //GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull
208
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
209
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
210
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
211
212
  GPIO_Init(GPIOA, &GPIO_InitStructure);
213
214
  //GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull
215
 GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
216
217
  GPIO_Init(GPIOB, &GPIO_InitStructure);
218
219
}

Jemand ne Idee was ich falsch mache?

Mfg

von (prx) A. K. (prx)


Lesenswert?

JASEN schrieb:

> // Define the vector table
>   unsigned int * myvectors[4]
>      _attribute_ ((section("vectors")))= {
>      (unsigned int *)  0x20000800,  // stack pointer
>      (unsigned int *)   main,    // code entry point

Wer hat dir denn diese Variante beigebracht? Bischen sehr schräg, den 
kompletten Startup-Code zu überspringen. Neben anderen lustigen Effekten 
hat das auch zur Folge, das initialisierte globale Variablen zwar immer 
noch variabel, aber nicht initialisiert sind.

von JASEN (Gast)


Lesenswert?

Hiho,

als Grundlage hab ich das beispielprogramm genommen, weil ich mich noch 
nicht sooo viel mit dem stm32 beschäftigt hab hatte nur die Main und die 
Funktionen angepasst.

Wieso ist das falsch was soll ich ändern? ;)

Mfg

von (prx) A. K. (prx)


Lesenswert?

Irgendwo hier auf der Site liegt m.W. eine Anleitung rum, wie man dem 
STM32 zu Leibe rückt, wenn man für den Compiler nicht löhnen will. Und 
falls es Löhn/Demoware ist: RTFM.

von Markus M. (Firma: EleLa - www.elela.de) (mmvisual)


Lesenswert?

Eine Initialisierung macht der StartUp-Code. Aber wenn man einen selbst 
geschriebenen Boot-Loader verwendet, dann ist dieser Start-Code so 
ziemlich witzlos und man macht die Initialisierung selbst in der Main:
1
  {  // Initialisieren Speicher
2
    extern unsigned long _etext;
3
    extern unsigned long _data;      /* start address for the .data section. defined in linker script */
4
    extern unsigned long _edata;    /* end address for the .data section. defined in linker script */
5
    extern unsigned long _bss;      /* start address for the .bss section. defined in linker script */
6
    extern unsigned long _ebss;      /* end address for the .bss section. defined in linker script */
7
8
    unsigned long *pulSrc, *pulDest;
9
    // Copy the data segment initializers from flash to SRAM.
10
    pulSrc = &_etext;
11
    for(pulDest = &_data; pulDest < &_edata; )
12
      *(pulDest++) = *(pulSrc++);
13
    // Zero fill the bss segment.
14
    for(pulDest = &_bss; pulDest < &_ebss; )
15
      *(pulDest++) = 0;
16
  }

Ausserdem ist Assembler für den ARM ziemlich schwierig.

von JASEN (Gast)


Lesenswert?

Hi, vielen Dank das ihr mir helfen wollt aber leider hab ich NULL 
verstannden.

Wo ist jetzt genau das Problem an meinem Code?

Das die Variablen nicht richtig initialisiert werden oder was?

Ich bin sicher noch nicht so weit das ich euch beiden folgen kann also 
bitte habt Verstädnis dafür.

Naja ich hab nun einfach das Beispielprogramm von ST übernommen das 
funktioniert auch nicht :/
1
#include "stm32f10x_lib.h"
2
#include "bits.h"
3
4
//--------------- Private Functions ---------------------------------------------
5
6
void nmi_handler(void);
7
void hardfault_handler(void);
8
int main(void);
9
void myDelay(unsigned long delay );
10
void RCC_Configuration(void);
11
void GPIO_Configuration(void);
12
void NVIC_Configuration(void);
13
14
15
//-------------------------------------------------------------------------------
16
17
// VARIABLES
18
19
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
20
21
TIM_OCInitTypeDef  TIM_OCInitStructure;
22
u16 CCR1_Val = 500;
23
u16 CCR2_Val = 375;
24
u16 CCR3_Val = 250;
25
u16 CCR4_Val = 125;
26
ErrorStatus HSEStartUpStatus;
27
28
GPIO_InitTypeDef GPIO_InitStructure;
29
30
//------------------------- MAIN -----------------------------------------------
31
32
int main(void)
33
{
34
#ifdef DEBUG
35
  debug();
36
#endif
37
38
  // System Clocks Configuration
39
  RCC_Configuration();
40
41
  // NVIC Configuration
42
  NVIC_Configuration();
43
44
  // GPIO Configuration
45
  GPIO_Configuration();
46
47
   /* -----------------------------------------------------------------------
48
      TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
49
      TIM3CLK = 36 MHz, Prescaler = 0x0, TIM3 counter clock = 36 MHz
50
      TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
51
      TIM3 Frequency = 36 KHz.
52
      TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
53
      TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
54
      TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
55
      TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
56
     ----------------------------------------------------------------------- */
57
58
  // Time base configuration
59
    TIM_TimeBaseStructure.TIM_Period = 999;
60
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
61
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
62
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
63
64
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
65
66
    // PWM1 Mode configuration: Channel1
67
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
68
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
69
    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
70
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
71
72
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
73
74
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
75
76
    // PWM1 Mode configuration: Channel2
77
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
78
    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
79
80
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);
81
82
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
83
84
    // PWM1 Mode configuration: Channel3
85
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
86
    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
87
88
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
89
90
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
91
92
    // PWM1 Mode configuration: Channel4
93
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
94
    TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
95
96
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
97
98
    TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
99
100
    TIM_ARRPreloadConfig(TIM3, ENABLE);
101
102
    // TIM3 enable counter
103
    TIM_Cmd(TIM3, ENABLE);
104
105
    while (1)
106
    {}
107
108
109
}
110
111
//SUB Routines-----------------------------------------------------------------
112
113
void nmi_handler(void)
114
{
115
  return ;
116
}
117
118
void hardfault_handler(void)
119
{
120
  return ;
121
}
122
//Functions definitions
123
void myDelay(unsigned long delay )
124
{
125
  while(delay) delay--;
126
}
127
128
/*************************************************************************
129
 * Function Name: Clk_Init
130
 * Parameters: Int32U Frequency
131
 * Return: Int32U
132
 *
133
 * Description: Init clock system
134
 *
135
 *************************************************************************/
136
137
void RCC_Configuration (void)
138
{
139
  // RCC system reset(for debug purpose)
140
  RCC_DeInit();
141
142
  // Enable HSE
143
  RCC_HSEConfig(RCC_HSE_ON);
144
145
  // Wait till HSE is ready
146
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
147
148
  if (HSEStartUpStatus == SUCCESS)
149
    {
150
      // Enable Prefetch Buffer
151
      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
152
153
      // Flash 2 wait state
154
      FLASH_SetLatency(FLASH_Latency_2);
155
156
      // HCLK = SYSCLK
157
      RCC_HCLKConfig(RCC_SYSCLK_Div1);
158
159
      // PCLK2 = HCLK
160
      RCC_PCLK2Config(RCC_HCLK_Div1);
161
162
      // PCLK1 = HCLK/4
163
      RCC_PCLK1Config(RCC_HCLK_Div4);
164
165
      // PLLCLK = 8MHz * 9 = 72 MHz
166
      RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
167
168
      // Enable PLL
169
      RCC_PLLCmd(ENABLE);
170
171
      // Wait till PLL is ready
172
          while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
173
          {}
174
175
          // Select PLL as system clock source
176
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
177
178
          // Wait till PLL is used as system clock source
179
          while (RCC_GetSYSCLKSource() != 0x08)
180
          {}
181
        }
182
183
    // TIM3 clock enable
184
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
185
186
    // GPIOA and GPIOB clock enable
187
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
188
189
}
190
191
192
193
void GPIO_Configuration(void)
194
{
195
  GPIO_InitTypeDef GPIO_InitStructure;
196
197
  //GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull
198
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
199
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
200
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
201
202
  GPIO_Init(GPIOA, &GPIO_InitStructure);
203
204
  //GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull
205
 GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
206
207
  GPIO_Init(GPIOB, &GPIO_InitStructure);
208
209
}
210
211
/*******************************************************************************
212
* Function Name  : NVIC_Configuration
213
* Description    : Configures Vector Table base location.
214
* Input          : None
215
* Output         : None
216
* Return         : None
217
*******************************************************************************/
218
void NVIC_Configuration(void)
219
{
220
#ifdef  VECT_TAB_RAM
221
  /* Set the Vector Table base location at 0x20000000 */
222
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
223
#else  /* VECT_TAB_FLASH  */
224
  /* Set the Vector Table base location at 0x08000000 */
225
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
226
#endif
227
}
228
229
#ifdef  DEBUG
230
/*******************************************************************************
231
* Function Name  : assert_failed
232
* Description    : Reports the name of the source file and the source line number
233
*                  where the assert_param error has occurred.
234
* Input          : - file: pointer to the source file name
235
*                  - line: assert_param error line source number
236
* Output         : None
237
* Return         : None
238
*******************************************************************************/
239
void assert_failed(u8* file, u32 line)
240
{
241
  /* User can add his own implementation to report the file name and line number,
242
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
243
244
  while (1)
245
  {}
246
}
247
#endif

Ich kann es noch nicht mal auf den uC flashen geschweige den debuggen.

Folgende Meldung kriege ich:
1
Warning: /cygdrive/C/gccfd/projects/robosoccer: No such file or directory.
2
mi_cmd_break_watch: Missing <expression>
3
No registers.
4
target remote localhost:3333
5
0xf8f4f000 in ?? ()
6
symbol-file main.out
7
monitor soft_reset_halt
8
requesting target halt and executing a soft reset
9
monitor flash erase_sector 0 0 31
10
erased sectors 0 through 31 on flash bank 0 in 1.000000s
11
monitor flash write_image main.bin 0x08000000
12
not enough working area available(requested 8192, free 8144)
13
wrote 15876 byte from file main.bin in 1.968750s (7.875000 kb/s)
14
thbreak main
15
Hardware assisted breakpoint 1 at 0x4: file main.c, line 42.
16
cont
17
18
Program received signal 0, Signal 0.
19
0xfffffffe in ?? ()

Komischeerweise bleibt das Programm genau an der Stelle hängen an dem 
RCC_Configuration() aufgerufen wird

Mfg

von Markus M. (Firma: EleLa - www.elela.de) (mmvisual)


Lesenswert?

Mache die Initialisierung der Variablen "CCR1_Val"..."CCR4_Val" in die 
main(), dann gehts.

Genau das wird mit meinem Code-Fragment initialisiert, der in Deinem 
Code Fehlt.
Also eine Variablendeklaration mit Zuweisung ausserhalb einer Funktion 
tut nicht.

PS: hab jetzt nicht den ganzen Code durchgeschaut.

von JASEN (Gast)


Lesenswert?

Yeah es klappt damit. Vielen Dank

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.