请教串口1和串口2一起使用的问题

2019-07-21 03:23发布

工程里面需要用到两个串口,串口1和串口2的配置代码如下,波特率为115200,现在发现串口2能够传数据,但是会丢掉一些数据,不知道是什么原因,是不是因为两个串口不在同一条总线上的原因,但是也不知道怎样解决这个问题,谢谢!
void uart1_init(u32 bound){

  GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_AHB1Periph_GPIOA,ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);


GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure); 


USART_InitStructure.USART_BaudRate = bound;
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_ClearFlag(USART1, USART_FLAG_TC);


#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 


}

void uart2_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); 
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure); 


USART_InitStructure.USART_BaudRate = bound;
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_InitStructure.NVIC_IRQChannel = USART2_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
 
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
   
USART_Cmd(USART2, ENABLE);                    
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。