
只需要把STM32F407串口1对应的PA9和PA10的GPIO_Mode 设置为 GPIO_Mode_AF,然后,把PA9、PA10连接到USART1,最后设置一下USART1就可以了。代码如下: //USART1 配置: 115200 8-N-1 void USARTx_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* 使能 GPIOA 时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); /* 使能 USART 时钟 */ USARTx_CLOCKCMD(RCC_APB2Periph_USART1, ENABLE); /* 配置Tx,Rx引脚为复用功能 */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); /* 连接 PA9 到 USART1_Tx*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); /* 连接 PA10 到 USART1_Rx*/ GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); /* 波特率设置:USART_BAUDRATE */ USART_InitStructure.USART_BaudRate = 115200; /* 字长(数据位+校验位):8 */ USART_InitStructure.USART_WordLength = USART_WordLength_8b; /* 停止位:1个停止位 */ USART_InitStructure.USART_StopBits = USART_StopBits_1; /* 校验位选择:无校验 */ USART_InitStructure.USART_Parity = USART_Parity_No; /* 硬件流控制:不使用硬件流 */ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /* USART模式控制:同时使能接收和发送 */ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* 完成USART初始化配置 */ USART_Init(USART1, &USART_InitStructure); /* 使能串口 */ USART_Cmd(USART1, ENABLE); } ———————————————— 版权声明:eewj |