Serial port 在STM32F072RBT6上连接/配置USART时出现故障

Serial port 在STM32F072RBT6上连接/配置USART时出现故障,serial-port,arm,stm32,usart,Serial Port,Arm,Stm32,Usart,我正在尝试将STM32F072RBT6模块连接到pc,并通过USART发送数据。我对这件事还很陌生,我不太懂。不幸的是,本模块没有可用的示例。我使用usb电缆将stm与pc连接。以下是我的代码: GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStructure; void send_char(char c) { while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) ==

我正在尝试将STM32F072RBT6模块连接到pc,并通过USART发送数据。我对这件事还很陌生,我不太懂。不幸的是,本模块没有可用的示例。我使用usb电缆将stm与pc连接。以下是我的代码:

GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;

void send_char(char c)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, c);
}

void send_string(const char* s) 
{
while (*s)
    send_char(*s++);
}

GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1);  //tried this too


GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_1);
我读到Rx引脚需要配置为浮动输入,但没有 像这样的模式设置。我还尝试使用GPIO_Mode_AF并对PinAFConfig进行了注释

GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;              
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;   
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;     
USART_InitStructure.USART_Parity=USART_Parity_No;               
USART_InitStructure.USART_StopBits=USART_StopBits_1;            
USART_InitStructure.USART_WordLength=USART_WordLength_8b;       
USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2, ENABLE);

while (1) {
    send_string("Hello world!\r\n");
}
}

我正试图在这方面看到任何结果。我注意到,当我不小心触到线路板上的引脚时,终端上出现了一些字符。这是否表示连接正常,但我的配置有问题? 我也试着用USART1做这件事,但是什么也没发生。你能不能

这里有一些例子 e、 g.在
Projects/STM32F072RB-Nucleo/Examples/UART下

虽然他们使用USART1,所以你必须稍微调整一下

如果没有例子的话 然后您可以简单地使用生成一个。使用MCU启动一个项目,单击USART2旁边的三角形(在左侧),选择Mode Asynchronous,然后使用project/Generate Code,它就在这里