Forum: Mikrocontroller und Digitale Elektronik [STM32] Der USART mal wieder


von Patrick L. (crashdemon)


Lesenswert?

Hallo,
bin neu im ARM programmieren und versuche mich gerade daran den USART2 
zum rennen zu bringen, der USART1 wird schon zum Download des Programmes 
benutzt.
Das Board habe ich hierher http://www.futurlec.com/ET-STM32_Stamp.shtml

Hier dann mal der Code
1
/******** USART initialisieren ********/
2
void USART_Configuration(void)
3
{
4
  USART_InitTypeDef USART_InitStructure;
5
  GPIO_InitTypeDef GPIO_InitStructure; 
6
7
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Enable GPIOA on APB2 peripheral clock
8
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // Enable AFIO Clock
9
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Enable USART2 Clock
10
  
11
  GPIO_StructInit(&GPIO_InitStructure); // Set GPIO to default values
12
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // Set TX(PA2)
13
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Set as output Push-Pull
14
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Set freq. of PortPin
15
  GPIO_Init(GPIOA, &GPIO_InitStructure);   // Initialize 
16
17
  /*
18
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // Set RX(PA3)
19
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; // Set as floating input
20
  GPIO_Init(GPIOA, &GPIO_InitStructure); // Initialize
21
  */
22
  
23
  USART_StructInit(&USART_InitStructure); // Initializes structure with default values
24
    USART_DeInit(USART2); // Put everything back to power-on defaults
25
26
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Enable USART2 on APB1 peripheral clock
27
28
  // ------------------------------ USART2 Configuration ------------------------------
29
  USART_InitStructure.USART_BaudRate = 115200; // The Baudrate
30
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // Software or Hardware FlowControl
31
  //USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Enable RX and TX
32
  USART_InitStructure.USART_Mode = USART_Mode_Tx; // Enable RX and TX
33
  USART_InitStructure.USART_Parity = USART_Parity_No; // No Parity Bit
34
  USART_InitStructure.USART_StopBits = USART_StopBits_1; // One Stopbit
35
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8-Bit Word length of Data
36
  USART_Init(USART2, &USART_InitStructure); // Now do the setup
37
38
  USART_Cmd(USART2, ENABLE); // Enable USART2
39
}

Wenn ich ein Oszi an besagten TX Pin hänge(PA2) tut sich leider nix, hab 
auch schon ein wenig das Forum durchstöbert, konnte aber keine Fehler 
erkennen.

von Lasse S. (cowz) Benutzerseite


Lesenswert?

Den Rest des Chips hast du initialisiert?

Wirklich am Pin was tun, wird sich ja erst, wenn du da auch Daten 
verschickst, das würd ich nochmal einbauen.

edit: Pins, die du für Alternate Functions benutzen willst (also bspw. 
USART), musst du entsprechend setzen. Also statt GPIO_Mode_PP lieber 
GPIO_Mode_AF_OD nehmen.

Gruß
Lasse

von Matthias K. (matthiask)


Lesenswert?

TxD PA2 muss auf alternative Funktion konfiguriert werden.
1
GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_AF_PP;     // Pin-Mode

Und zeig mal die eigentliche Senderoutine.

AFIO brauchst Du nicht zu enablen, solange die Standardpins für die 
UART2 verwendet werden.

von Patrick L. (crashdemon)


Lesenswert?

Joar, senden tu ich natürlich
1
/******** RCC initialisieren ********/
2
void RCC_Configuration(void)
3
{
4
  ErrorStatus HSEStartUpStatus;
5
6
    RCC_DeInit(); // RCC system reset(for debug purpose)
7
    RCC_HSEConfig(RCC_HSE_ON); // Enable HSE
8
9
    HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready
10
11
    if(HSEStartUpStatus == SUCCESS)
12
    {
13
      FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable Prefetch Buffer
14
      FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state 
15
16
      RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK 
17
      RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK
18
      RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2
19
20
      RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz
21
      RCC_PLLCmd(ENABLE); // Enable PLL
22
      while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait till PLL is ready
23
24
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock source
25
26
      while(RCC_GetSYSCLKSource() != 0x08); // Wait till PLL is used as system clock source
27
    }
28
}
29
30
int main(void)
31
{
32
  RCC_Configuration(); // System Clocks Configuration
33
  USART_Configuration(); // USART initialisieren
34
35
  while(1)
36
  {
37
     USART_SendData(USART2, 'T');
38
  while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
39
  }
40
}

von Patrick L. (crashdemon)


Lesenswert?

Matthias K. schrieb:
> TxD PA2 muss auf alternative Funktion konfiguriert werden.
>
1
> GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_AF_PP;     // Pin-Mode
2
>
>
> Und zeig mal die eigentliche Senderoutine.
>
> AFIO brauchst Du nicht zu enablen, solange die Standardpins für die
> UART2 verwendet werden.

Optimal, jetzt fluppt es, danke, lag am GPIO_Mode_Out_PP, hab es ersetzt 
und jetzt sieht man auch was feines am Oskar.

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.