AnsweredAssumed Answered

STM32F103 USART - PC receives wrong data

Question asked by verdano.gianmichele on Jun 8, 2016
Latest reply on Jun 9, 2016 by AvaTar
Hi guys,

I've implemented the code to send data from STM32f103 to PC with USART interface, but the data that I receive are wrong. This is the code that I've implemented :

void RCC_Configuration( void )
{
    /* RCC system reset(for debug purpose) */
    RCC_DeInit();
 
    /* Enable HSE */
    RCC_HSEConfig( RCC_HSE_ON );
 
    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp();
 
    if ( HSEStartUpStatus == SUCCESS )
    {
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
 
        /* Flash 2 wait state */
        FLASH_SetLatency( FLASH_Latency_2 );
 
        /* HCLK = SYSCLK */
        RCC_HCLKConfig( RCC_SYSCLK_Div1 );//Define AHB prescale @72Mhz
 
        /* PCLK2 = HCLK */
        RCC_PCLK2Config( RCC_HCLK_Div1 ); //Define APB2 prescale @72Mhz
 
        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config( RCC_HCLK_Div2 ); //Define APB1 prescale @36Mhz
 
        /* ADCCLK = PCLK2/4 */
        RCC_ADCCLKConfig( RCC_PCLK2_Div6 ); //Max 14 Mhz now @12Mhz
 
        /* PLLCLK = 8MHz*9 = 72MHz */
        RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
     
        /*Set USB clock @48Mhz*/
        RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
 
        /* Enable PLL */
        RCC_PLLCmd( ENABLE );
 
        /* Wait till PLL is ready */
        while ( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET )
            {;}
 
        /* Select PLL as system clock source */
        RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
 
        /* Wait till PLL is used as system clock source */
        while ( RCC_GetSYSCLKSource() != 0x08 )
            {;}
    }
 
    /* Enable peripheral clocks --------------------------------------------------*/
 
    /* Enable SPI1 clocks */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 , ENABLE );
 
    /* Enable GPIOs clocks */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
 
    /* Enable USART1 clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
}
 
void USART_init(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
 
    /* USARTx configured as follow:
          - BaudRate = 115200 baud
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Hardware flow control disabled (RTS and CTS signals)
          - Receive and transmit enabled
    */
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
    /* Configure USART Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    /* Configure USART Rx as input floating */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    /* USART configuration */
    USART_Init(USART1, &USART_InitStructure);
 
    /* Enable USART */
    USART_Cmd(USART1, ENABLE);
}
 
void USART_putc(char c)
{
    /* e.g. write a character to the USART */
    USART_SendData(USART1, c);
 
    /* Loop until the end of transmission */
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ;
}
 
void USART_puts(const char *s)
{
    int i;
    for(i=0; s[i]!=0; i++)
    {
        USART_putc(s[i]);
    }
}
 
void printf1(const char *format, ...)
{
    va_list list;
    va_start(list, format);
 
    int len = vsnprintf(0, 0, format, list);
    char *s;
 
    s = (char *)malloc(len + 1);
    vsprintf(s, format, list);
 
    USART_puts(s);
 
    free(s);
    va_end(list);
    return;
}

But if I try to send "Hello world\n", I receive this:

output.jpg

I use RS232USB converte with three cables for connect the STM32F103 to PC. Some suggestion?

P.S. With another code (I have only the .hex) the communication works fine. 

Outcomes