野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10781|回复: 5

串口2和串口3怎么才能同时使用

[复制链接]
发表于 2018-11-9 17:12:11 | 显示全部楼层 |阅读模式
我是小白一个,现在正在学习野火MINI板子,我想同时使用两个串口(串口2和串口3),但是却只能使用一个,求大神指教!

int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart2_init();
uart3_init();

while(1) {}
}
这样子串口2就可以用,但串口3不能用;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

uart3_init();

  uart2_init();

while(1) {}
}

如果这样子,则是串口3可以用,串口2不能用

求高手指点




回复

使用道具 举报

发表于 2018-11-9 17:52:12 | 显示全部楼层
要看看你具体的初始化程序里是怎么实现的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-9 18:03:40 | 显示全部楼层
这是串口的配置
void uart2_init(void)   
{   
   USART_InitTypeDef USART_InitStructure;   
   NVIC_InitTypeDef NVIC_InitStructure;     
   GPIO_InitTypeDef GPIO_InitStructure;      
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //ʹÄÜUART2ËùÔÚGPIOAʱÖÓ   
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);   

   //´®¿ÚʹÓõÄGPIO¿ÚÅäÖà        
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
   GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
   GPIO_Init(GPIOA, &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;   


   //Íê³É´®¿Ú2³õʼ»¯ÅäÖà    
   USART_Init(USART2, &USART_InitStructure);   

  //ʹÄÜ´®¿Ú2½ÓÊÕÖжϠ 
   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   
   //ʹÄÜ´®¿Ú2·¢ËÍÖжϠ  
   USART_ITConfig(USART2, USART_IT_TXE, ENABLE);   

   //ʹÄÜ´®¿Ú2     
   USART_Cmd(USART2, ENABLE);   

   //´®¿ÚÖжÏÅäÖà        
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);   
   
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;   
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;         
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
   NVIC_Init(&NVIC_InitStructure);   
}


//´®¿Úuart3
void uart3_init(void)   
{   
   USART_InitTypeDef USART_InitStructure;   
   NVIC_InitTypeDef NVIC_InitStructure;     
   GPIO_InitTypeDef GPIO_InitStructure;        
   //ʹÄÜ´®¿ÚµÄRCCʱÖÓ   
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);     
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);   

   //´®¿ÚʹÓõÄGPIO¿ÚÅäÖà       
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
   GPIO_Init(GPIOB, &GPIO_InitStructure);   
  
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
   GPIO_Init(GPIOB, &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(USART3, &USART_InitStructure);   

   //ʹÄÜ´®¿Ú½ÓÊÕÖжϠ  
   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);   
   //ʹÄÜ´®¿Ú·¢ËÍÖжϠ  
   USART_ITConfig(USART3, USART_IT_TXE, ENABLE);   
     
   USART_Cmd(USART3, ENABLE);   

   //´®¿ÚÖжÏÅäÖà          
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);   
   
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;         
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
   NVIC_Init(&NVIC_InitStructure);   


}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-19 11:34:27 | 显示全部楼层
请大神指教一下,非常感谢
回复 支持 反对

使用道具 举报

发表于 2018-11-21 13:25:20 | 显示全部楼层
你试着把中断的优先级一个2,2;另一个改3.3。还有我感觉USART_IT_TXE这个发送中断使能就没必要加了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-21 15:49:24 | 显示全部楼层
wx_Zg9v1fC6 发表于 2018-11-21 13:25
你试着把中断的优先级一个2,2;另一个改3.3。还有我感觉USART_IT_TXE这个发送中断使能就没必要加了

非常感谢,问题已经解决了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-9-22 19:35 , Processed in 0.043899 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表