我先用PA9作为串口1的TX引脚,用PA10作为串口1的RX引脚,我对各个引脚的状态功能不是特别清楚,不过通过写了一些代码觉得应该是与该引脚的具体功能相对应有关,例如在串口通信时就应该设置成浮空输入和复用推挽输出,在这里提出我的理解如果理解有误欢迎各位前辈积极指正。
所以我如果实现串口通信首先需要配置GPIO和USART为指定状态:
void usart_init( void )
 {//初始化结构体定义
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 //使能串口时钟
 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE );
 //指定PA9和PA10用于串口
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置速度
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//设置推输出
 GPIO_Init( GPIOA, &GPIO_InitStructure );//调用初始化函数完成初始化
 //定义RX引脚
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//设置浮空输入
 GPIO_Init( GPIOA, &GPIO_InitStructure );
 USART_InitStructure.USART_BaudRate = 38400;
 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( USART1, &USART_InitStructure );
 USART_Cmd( USART1, ENABLE );
USART_ITConfig(USART1,USRT_IT_TXE,ENABLE);
}
接收函数:
void void Uart1_Put(unsigned charch)
 {
 USART_SendData(USART1,ch);//调用发送函数发送数据
 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//检查是否发送完毕
 }