cainiao518 发表于 2021-7-19 16:41

STM32多串口初始化失败怎么回事?

本帖最后由 cainiao518 于 2021-7-19 17:00 编辑

      USART1_Config(); //串口1初始化
      USART2_Config(); //串口2初始化
      USART3_Config(); //串口3初始化
      UART4_Config();   //串口4初始化
      UART5_Config();   //串口5初始化

这里用到5个串口,但除了1号初始化成功,其它都失败,咋回事呢?
其中串口2重映射
MCU= STM32F103VET6
void USART2_Config(void )
{
      GPIO_InitTypeDef GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;

      // 打开串口GPIO的时钟
      DEBUG_USART_GPIO_APBxClkCmd(RCC_APB2Periph_GPIOD, ENABLE);
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
      GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);            //duan kou chong ying she
      // 打开串口外设的时钟
      DEBUG_USART_APBxClkCmd(RCC_APB1Periph_USART2, ENABLE);

      // 将USART Tx的GPIO配置为推挽复用模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOD, &GPIO_InitStructure);

// 将USART Rx的GPIO配置为浮空输入模式
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOD, &GPIO_InitStructure);
      
      // 配置串口的工作参数
      // 配置波特率
      USART_InitStructure.USART_BaudRate = 115200;
      // 配置 针数据字长
      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);
      
      // 串口中断优先级配置
      NVIC_Configuration2();
      
      // 使能串口接收中断
      USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);      
      
      // 使能串口
      USART_Cmd(USART2, ENABLE);               
}
@ddllxxrr,@一路向北lm,@694844985,@McuPlayer



cainiao518 发表于 2021-7-26 09:50

大家有没遇到过呢?

一叶倾城wwq 发表于 2021-7-26 10:39

F103吧?5个串口。
楼主还用库自己开发吗?直接用配置工具配置生成代码吧,省的还要找初始化问题

cainiao518 发表于 2021-7-27 09:12

一叶倾城wwq 发表于 2021-7-26 10:39
F103吧?5个串口。
楼主还用库自己开发吗?直接用配置工具配置生成代码吧,省的还要找初始化问题 ...

用库已经习惯了
页: [1]
查看完整版本: STM32多串口初始化失败怎么回事?