首先要配置串口时钟:
// USART1 clock enable
RCC_APB2PeriphClockCmd(
RCC_APB2Periph_USART1 |
RCC_APB2Periph_GPIOA |
RCC_APB2Periph_AFIO, ENABLE);
然后再配置端口,在配置串口,再使能即可。
void USART1_Init(void)
{
/////// config the gpio
GPIO_InitTypeDef GPIO_InitStructure;
/* PA9 USART1_Tx */
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);
/* PA10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置接收端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
GPIO_Init(GPIOA, &GPIO_InitStructure);
//////////////
USART_InitTypeDef USART_InitStructure;//
USART_InitStructure.USART_BaudRate