0
votes

I tried to follow the answer of this link to configure my USART of my STM32F0 Discovery board: stm32f0 uart programming

I use USART2 to send data to my PC with baudrate 9600 and 115200 both have tried.

I send characters from '0' -> '9' to PC but I received 'cg3fe2d' and some invisible characters always, there seems some regulation, can anyone help?

My STM32F0 is configured to use internal osc, 48MHz.

enter image description here

my reference code is like:

void test_uart()
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    uint16_t usart_data;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

    //Configure USART2 pins:  Rx and Tx ----------------------------
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    //Configure USART2 setting:       ----------------------------
    USART_InitStructure.USART_BaudRate = 9600;
    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;
    USART_Init(USART2, &USART_InitStructure);

    USART_Cmd(USART2,ENABLE);

    usart_data = '0';
    while(1)
    {
        while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
        if (usart_data > '9')
            usart_data  = '0';
        USART_SendData(USART2, usart_data++);
    }
}
2
Has anyone see this before?yunfei
Looking at the bitstrings of the received characters vs. the transmitted characters, if the baudrate really is correct I have a suspicion your logic levels might be getting inverted one too many (or one too few) times.Notlikethat
Yeah, possibly. I am now suspecting on the OSC, may be try an out side standalone OSC.yunfei
and also planing on trying auto baudrateyunfei
Dear all, The problem has been resolved, I am really appreciate for your kind help, and I want to share you the result. The reason is I was using USBto232, I use 3 wires to connect it directly with the discovery board, I didnot know that RS232 is using 12V, that is the root cause. So after I use RS232 adapter, the problem resolved. Thank you all againyunfei

2 Answers

0
votes

I'd suspect it's something to do with library having a different idea about clock than you do. Especially if you get the same outputs with different baudrates.

Do you need to tell the library what your current clock is?

0
votes

The problem has been resolved, I am really appreciate for your kind help, and I want to share you the result. The reason is I was using USBtoRS232, I use 3 wires to connect it directly with the discovery board, I didnot know that RS232 is using 12V, that is the root cause. So after I use RS232 adapter, the problem resolved. Thank you all again