0

我试图按照这个链接的答案来配置我的 STM32F0 Discovery board 的 USART: stm32f0 uart programming

我使用 USART2 将数据发送到我的电脑,波特率 9600 和 115200 都试过了。

我将字符从 '0' -> '9' 发送到 PC,但我总是收到 'cg3fe2d' 和一些不可见字符,似乎有一些规定,有人可以帮忙吗?

我的 STM32F0 配置为使用内部 osc,48MHz。

在此处输入图像描述

我的参考代码是这样的:

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++);
    }
}
4

2 回答 2

0

我怀疑这与图书馆对时钟的看法不同有关。特别是如果您获得具有不同波特率的相同输出。

你需要告诉图书馆你现在的时钟是多少吗?

于 2014-02-26T13:25:00.253 回答
0

问题已经解决,非常感谢您的帮助,我想与您分享结果。原因是我用的是USBtoRS232,我用3根线直接和发现板连接,我不知道RS232用的是12V,这是根本原因。所以在我使用 RS232 适配器后,问题就解决了。再次感谢大家

于 2014-03-15T05:40:07.670 回答