我试着按照这个链接的答案来配置我的STM32F0发现板的USART:stm32f0 uart programming
我使用USART2将数据发送到我的PC上,波德率为9600和115200,两者都曾尝试过。
我从'0‘-> '9’发送字符到PC,但我收到'cg3fe2d‘和一些无形的字符总是,似乎有一些规则,谁能帮忙吗?
我的STM32F0被配置为使用内部osc,48 My。

我的参考代码是:
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++);
}
}发布于 2014-03-15 05:40:07
问题已经解决了,我真的很感谢你的帮助,我想和大家分享结果。原因是我使用了USBtoRS232,我用3根线直接连接到发现板,我不知道RS232使用12V,这是根本原因。所以在我使用RS232适配器之后,问题就解决了。再次感谢大家
发布于 2014-02-26 13:25:00
我怀疑这是因为图书馆对时钟的看法与你不同。特别是当你得到相同的输出与不同的波特率。
你需要告诉图书馆你现在的时钟是什么吗?
https://stackoverflow.com/questions/22041816
复制相似问题