STM32同时使用2个串口出现了问题!请求帮助!!

2019-07-16 08:12发布

  /* Enable USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Enable USART2 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// I/O口的配置略
/* USART1  Configure */
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;
  
  /* Configure the USART1 */
  USART_Init(USART1, &USART_InitStructure);
  /* INIT  USART1 */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  /* Enable the USART1 */
  USART_Cmd(USART1, ENABLE);

/* USART2  Configure */
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;
  
  /* Configure the USART2 */
  USART_Init(USART2, &USART_InitStructure);
  /* INIT  USART2 */
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  /* Enable the USART2 */
  USART_Cmd(USART2, ENABLE);

     我同时用串口1和串口2.串口1就有时候发出来的数据会错乱(串口2发出来的数据时没有问题的)。然后我就把串口2的配置全部屏蔽了,只剩下串口1的配置,也是出现发出来的数据有时候错乱。接着我又把第句话屏蔽掉(RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);)这样串口1发送的数据就完成没有问题了。

请问这个是什么问题呢?

再次强调:只要把第句话屏蔽掉(RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);)串口就能正常发送!!!即使串口1和串口2同时开和发数据,串口1发数据有是会出错,串口2是没有出错!

神马问题?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。