Question

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++);
    }
}
Was it helpful?

Solution 2

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

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top