このリンクの回答に従って、STM32F0 Discovery ボードの USART を構成しようとしました: stm32f0 uart プログラミング
USART2 を使用して、ボーレート 9600 と 115200 の両方で PC にデータを送信しました。
'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++);
}
}