我试图按照这个链接的答案来配置我的 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++);
}
}