Forum: Mikrocontroller und Digitale Elektronik USART1 bei STM32 Discovery und Coocox will nicht


von Holger T. (holger1979)


Lesenswert?

Hallo,

viele Beiträge in diesem Forum beschäftigen sich mit meinem Problem. 
Doch eine Lösung habe ich nicht gefunden.

Ich habe einen STM32F0-Discovery und möchte einfach ein Byte über USART1 
senden oder empfangen.
Sollte eigentlich kein Problem sein, geht aber nicht.

Als IDE verwende ich Coocox, folgenden Code habe ich momentan:
1
#include "stm32f0xx.h"
2
#include "stm32f0xx_usart.h"
3
#include "stm32f0xx_gpio.h"
4
#include "stm32f0xx_rcc.h"
5
6
void MX_USART1_UART_Init(void)
7
{
8
    GPIO_InitTypeDef GPIO_InitStructure;
9
    USART_InitTypeDef USART_InitStructure;
10
11
    USART_InitStructure.USART_BaudRate = 9600;
12
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
13
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
14
    USART_InitStructure.USART_Parity = USART_Parity_No;
15
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
16
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
17
18
    /* Enable GPIO clock */
19
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
20
21
    /* Enable USART clock */
22
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
23
24
    /* Connect PXx to USARTx_Tx */
25
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
26
27
    /* Connect PXx to USARTx_Rx */
28
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
29
30
    /* Configure USART Tx, Rx as alternate function push-pull */
31
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
32
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
33
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
34
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
35
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
36
    GPIO_Init(GPIOA, &GPIO_InitStructure);
37
38
    /* USART configuration */
39
    USART_Init(USART1, &USART_InitStructure);
40
41
    /* Enable USART */
42
    USART_Cmd(USART1, ENABLE);
43
}
44
45
int main(void)
46
{
47
  MX_USART1_UART_Init();
48
49
  // Buzzer
50
    RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // GPIOB Periph clock enable
51
    GPIOB->PUPDR |= (1 << 24) | (1 << 26); // PB12 und PB13 pull up
52
53
    // LD3 LD4
54
  RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // GPIOC Periph clock enable
55
  GPIOC->MODER |= (1 << 12) | (1 << 14) | (1 << 16) | (1 << 18); // PC6, PC7, PC8 und PC9 output
56
57
    int taste = 0;
58
    int output = 0;
59
  int data = 0;
60
    unsigned char blocked = 0;
61
62
    while(1)
63
    {
64
      taste = GPIOB->IDR;
65
      if (!blocked)
66
      {
67
        output = 0;
68
      if (!(taste & (1 << 12)))
69
      {
70
        blocked = 1;
71
        GPIOC->ODR |= ((1 << 7) | (1 << 9));
72
        output += 1;
73
      }
74
      if (!(taste & (1 << 13)))
75
      {
76
        blocked = 1;
77
        GPIOC->ODR |= ((1 << 6) | (1 << 8 ));
78
        output += 2;
79
      }
80
      if (output)
81
      {
82
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
83
        USART_SendData(USART1, output + 48);
84
      }
85
      }
86
    if(GPIOA->IDR & GPIO_IDR_0)
87
    {
88
      blocked = 0;
89
      GPIOC->ODR &= ~((1 << 6) | (1 << 7) | (1 << 8) | (1 << 9));
90
      while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
91
      USART_SendData(USART1, 48);
92
    }
93
94
    data = 0;
95
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != 0)
96
    {
97
      data = (USART_ReceiveData(USART1) & 0x7F);
98
    }
99
100
      if (data != 0)
101
      {
102
      blocked = 0;
103
      GPIOC->ODR &= ~((1 << 6) | (1 << 7) | (1 << 8) | (1 << 9));
104
      }
105
    }
106
}

Das Ganze lässt sich compilieren und sieht gut aus. Leider kann das 
Programm weder Senden noch Empfangen. Ich weiß einfach nicht mehr 
weiter.
Wer findet einen Fehler?

von dummy (Gast)


Lesenswert?

>    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

Heisst das nicht GPIO_AF_USART1

von Holger T. (holger1979)


Lesenswert?

Ich habs mal eingegeben. Kennt der aber leider nicht.

von Ingo L. (corrtexx)


Lesenswert?

Holger Timm schrieb:
> Kennt der aber leider nicht.
Müsste er aber, Geh mal in die Funktionsdefinition, da steht was dazu 
drin!
Auszug für einen F4:
1
/**
2
  * @brief  Changes the mapping of the specified pin.
3
  * @param  GPIOx: where x can be (A..I) to select the GPIO peripheral.
4
  * @param  GPIO_PinSource: specifies the pin for the Alternate function.
5
  *         This parameter can be GPIO_PinSourcex where x can be (0..15).
6
  * @param  GPIO_AFSelection: selects the pin to used as Alternate function.
7
  *          This parameter can be one of the following values:
8
  *            @arg GPIO_AF_RTC_50Hz: Connect RTC_50Hz pin to AF0 (default after reset) 
9
  *            @arg GPIO_AF_MCO: Connect MCO pin (MCO1 and MCO2) to AF0 (default after reset) 
10
  *            @arg GPIO_AF_TAMPER: Connect TAMPER pins (TAMPER_1 and TAMPER_2) to AF0 (default after reset) 
11
  *            @arg GPIO_AF_SWJ: Connect SWJ pins (SWD and JTAG)to AF0 (default after reset) 
12
  *            @arg GPIO_AF_TRACE: Connect TRACE pins to AF0 (default after reset)
13
  *            @arg GPIO_AF_TIM1: Connect TIM1 pins to AF1
14
  *            @arg GPIO_AF_TIM2: Connect TIM2 pins to AF1
15
  *            @arg GPIO_AF_TIM3: Connect TIM3 pins to AF2
16
  *            @arg GPIO_AF_TIM4: Connect TIM4 pins to AF2
17
  *            @arg GPIO_AF_TIM5: Connect TIM5 pins to AF2
18
  *            @arg GPIO_AF_TIM8: Connect TIM8 pins to AF3
19
  *            @arg GPIO_AF_TIM9: Connect TIM9 pins to AF3
20
  *            @arg GPIO_AF_TIM10: Connect TIM10 pins to AF3
21
  *            @arg GPIO_AF_TIM11: Connect TIM11 pins to AF3
22
  *            @arg GPIO_AF_I2C1: Connect I2C1 pins to AF4
23
  *            @arg GPIO_AF_I2C2: Connect I2C2 pins to AF4
24
  *            @arg GPIO_AF_I2C3: Connect I2C3 pins to AF4
25
  *            @arg GPIO_AF_SPI1: Connect SPI1 pins to AF5
26
  *            @arg GPIO_AF_SPI2: Connect SPI2/I2S2 pins to AF5
27
  *            @arg GPIO_AF_SPI3: Connect SPI3/I2S3 pins to AF6
28
  *            @arg GPIO_AF_I2S3ext: Connect I2S3ext pins to AF7
29
  *            @arg GPIO_AF_USART1: Connect USART1 pins to AF7
30
  *            @arg GPIO_AF_USART2: Connect USART2 pins to AF7
31
  *            @arg GPIO_AF_USART3: Connect USART3 pins to AF7
32
  *            @arg GPIO_AF_UART4: Connect UART4 pins to AF8
33
  *            @arg GPIO_AF_UART5: Connect UART5 pins to AF8
34
  *            @arg GPIO_AF_USART6: Connect USART6 pins to AF8
35
  *            @arg GPIO_AF_CAN1: Connect CAN1 pins to AF9
36
  *            @arg GPIO_AF_CAN2: Connect CAN2 pins to AF9
37
  *            @arg GPIO_AF_TIM12: Connect TIM12 pins to AF9
38
  *            @arg GPIO_AF_TIM13: Connect TIM13 pins to AF9
39
  *            @arg GPIO_AF_TIM14: Connect TIM14 pins to AF9
40
  *            @arg GPIO_AF_OTG_FS: Connect OTG_FS pins to AF10
41
  *            @arg GPIO_AF_OTG_HS: Connect OTG_HS pins to AF10
42
  *            @arg GPIO_AF_ETH: Connect ETHERNET pins to AF11
43
  *            @arg GPIO_AF_FSMC: Connect FSMC pins to AF12
44
  *            @arg GPIO_AF_OTG_HS_FS: Connect OTG HS (configured in FS) pins to AF12
45
  *            @arg GPIO_AF_SDIO: Connect SDIO pins to AF12
46
  *            @arg GPIO_AF_DCMI: Connect DCMI pins to AF13
47
  *            @arg GPIO_AF_EVENTOUT: Connect EVENTOUT pins to AF15
48
  * @retval None
49
  */
50
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
51
{
52
  uint32_t temp = 0x00;
53
  uint32_t temp_2 = 0x00;
54
  
55
  /* Check the parameters */
56
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
57
  assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
58
  assert_param(IS_GPIO_AF(GPIO_AF));
59
  
60
  temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
61
  GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
62
  temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
63
  GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
64
}

von dummy (Gast)


Lesenswert?

>Leider kann das Programm weder Senden noch Empfangen.

Dann fang einfach mal mit senden an und lass
den ganzen anderen Klimbim weg.
1
int main(void)
2
{
3
  MX_USART1_UART_Init();
4
  while(1)
5
  {
6
     USART_SendData(USART1, 'U');
7
     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
8
     
9
     // todo hier vieleicht noch ein bisschen warten, oder auch nicht
10
  }
11
}

Da kannst du auch schön dein Osci dranhängen.

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.