你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

关于STM32F030C8T6 串口1与串口2共同使用的问题

[复制链接]
龙龙龙龙龙龙龙龙 提问时间:2015-9-30 11:30 /
单独配置串口1和串口2使用没问题,但是一旦配置两个之后串口1就使用不了了? 管脚映射正常,不清楚哪里出现问题,望高手出招。
收藏 1 评论24 发布时间:2015-9-30 11:30

举报

24个回答
alisa123 回答时间:2015-9-30 16:03:02
我在stm32f030C8同时用过Usart1,并且通信ok;现在在stm32f030R8上同时用Usart1和Usart2,结果Usart2就是不能进入接收和发送中断,楼主你单独用usart2是ok的吗?
chifen 回答时间:2015-10-4 14:20:12


  1. /*******************************************************************************
  2. * Name  : UART1_Configuration  ´®¿Ú³õʼ»¯
  3. * Deion        : Configures the uart1
  4. * Input                    : None
  5. * Output                 : None
  6. * Return                 : None
  7. *******************************************************************************/
  8. void USART_Configuration(void)
  9. {
  10.           GPIO_InitTypeDef  GPIO_InitStructure;
  11.     USART_InitTypeDef USART_InitStructure;
  12.     NVIC_InitTypeDef NVIC_InitStructure;   
  13.             
  14.                 /*Enable the USART1 Interrupt(ʹÄÜUSART1ÖжÏ)*/                                                                                                        
  15.                 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;   
  16.                 NVIC_InitStructure.NVIC_IRQChannelPriority =0;                
  17.                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  18.                 NVIC_Init(&NVIC_InitStructure);

  19.             /*Enable the USART1 Interrupt(ʹÄÜUSART1ÖжÏ)*/                                                                                                        
  20.                 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;   
  21.                 NVIC_InitStructure.NVIC_IRQChannelPriority =2;                
  22.                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  23.                 NVIC_Init(&NVIC_InitStructure);   
  24.                
  25.         
  26.                 RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  27.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
  28.                
  29.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
  30.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);        
  31.         /*
  32.         *  USART1_TX -> PA9 , USART1_RX ->        PA10
  33.            //GPIO_Mode_IN(ÊäÈë),GPIO_Mode_OUT(Êä³ö),GPIO_Mode_AF(¸´ÓÃ),GPIO_Mode_AN(Ä£Äâ)
  34.             // GPIO_OType_PP(ÍÆÍì),GPIO_OType_OD(¿ªÂ©)
  35.             GPIO_PuPd_NOPULL(ÎÞ),GPIO_PuPd_UP(ÉÏÀ­),GPIO_PuPd_DOWN(ÏÂÀ­)
  36.         */                                
  37.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;                 
  38.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  39.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  40.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  41.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  42.     GPIO_Init(GPIOA, &GPIO_InitStructure);        
  43.         
  44.     USART_InitStructure.USART_BaudRate = 115200;
  45.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  46.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  47.     USART_InitStructure.USART_Parity = USART_Parity_No;
  48.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  49.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  50.     USART_Init(USART1, &USART_InitStructure);

  51.     USART_Cmd(USART1, ENABLE);
  52.                 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  53.                 //--------------------               
  54.                 //RCC_AHBPeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  55.                 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  56.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
  57.     GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);                        
  58.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;                 
  59.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  60.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  61.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  62.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  63.     GPIO_Init(GPIOA, &GPIO_InitStructure);        
  64.         
  65.     USART_InitStructure.USART_BaudRate = 9600;
  66.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  67.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  68.     USART_InitStructure.USART_Parity = USART_Parity_No;
  69.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  70.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  71.     USART_Init(USART2, &USART_InitStructure);

  72.     USART_Cmd(USART2, ENABLE);
  73.                 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  74.                
  75.   
  76. }
复制代码
龙龙龙龙龙龙龙龙 回答时间:2015-10-4 10:20:13
void USART_INIT(void)
{
                GPIO_InitTypeDef  GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;
                NVIC_InitTypeDef         NVIC_InitStructure;
       
                NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
       
                GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
                GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
       
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );       
                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(USART1, &USART_InitStructure);
                USART_Cmd(USART1, ENABLE);
}
void USART2_INIT(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;
                NVIC_InitTypeDef         NVIC_InitStructure;
       
                NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
       
                GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
                GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
       
                RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );       
                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);
                USART_Cmd(USART2, ENABLE);
}

in main(void)
{
        USART_INIT();
        USART2_INIT();
        Delay(25000);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
       
        USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
你好我好大家好! 回答时间:2015-9-30 20:27:47
是不是代码出错了
龙龙龙龙龙龙龙龙 回答时间:2015-10-1 10:20:15
alisa123 发表于 2015-9-30 16:03
我在stm32f030C8同时用过Usart1,并且通信ok;现在在stm32f030R8上同时用Usart1和Usart2,结果Usart2就是不 ...

现在是串口1可以接收和发送,串口2没试过接收 只能发送
alisa123 回答时间:2015-10-3 09:01:45
龙龙龙龙龙龙龙龙 发表于 2015-10-1 10:20
现在是串口1可以接收和发送,串口2没试过接收 只能发送

串口1和串口2的设置代码一样吗?
龙龙龙龙龙龙龙龙 回答时间:2015-10-3 09:39:15
alisa123 发表于 2015-10-3 09:01
串口1和串口2的设置代码一样吗?

想要使用串口1串口2 必须先初始化关闭两个串口的中断标志,然后延时一段时间后再次开启两个中断的标志位,这样就可以正常接收了,你可以试试。
chifen 回答时间:2015-10-3 16:29:36
STM32 串口1和串口2 是两个独立的外设的,你对比一下1的设置2的应该也要同样设置一下, STM32F0和STM32F103我都同时用过两个串口没有问题的
chifen 回答时间:2015-10-3 16:30:41
你最好把你 设置的代码 加上来看看
chifen 回答时间:2015-10-4 14:18:46


  1. /*******************************************************************************
  2. * Name  : UART1_Configuration  ´®¿Ú³õʼ»¯
  3. * Deion        : Configures the uart1
  4. * Input                    : None
  5. * Output                 : None
  6. * Return                 : None
  7. *******************************************************************************/
  8. void USART_Configuration(void)
  9. {
  10.   USART_InitTypeDef USART_InitStructure;
  11.   USART_ClockInitTypeDef  USART_ClockInitStructure;

  12. // Uart1_GPIO_Configuration();
  13.   //ʹÄÜ´®¿Ú1£¬PA£¬AFIO×ÜÏß        ʹÄÜ´®¿Ú1
  14.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
  15.             RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,
  16.             ENABLE);


  17.   USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  18.   USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  19.   USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  20.   USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  21.   /* Configure the USART1 synchronous paramters */
  22.   USART_ClockInit(USART1, &USART_ClockInitStructure);

  23.                                                                            //²¨ÌØÂÊ
  24.   USART_InitStructure.USART_BaudRate = 38400;
  25.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  26.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  27.   USART_InitStructure.USART_Parity = USART_Parity_No ;
  28.   USART_InitStructure.USART_HardwareFlowControl =  USART_HardwareFlowControl_None;

  29.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  30.   /* Configure USART1 basic and asynchronous paramters */
  31.   USART_Init(USART1, &USART_InitStructure);
  32.   /* Enable USART1 Receive interrupts */
  33.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  34.   /* Enable USART1 */
  35.   USART_Cmd(USART1, ENABLE);


  36.   //------------------------RCC_APB1Periph_USART2---------------------
  37.    //RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);  //ʹÄÜ´®¿Ú2ʱÖÓ
  38.      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  //ʹÄÜ´®¿Ú2ʱÖÓ
  39.   USART_InitStructure.USART_BaudRate = 38400;//9600;  //²¨ÌØÂÊ
  40.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  41.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  42.   USART_InitStructure.USART_Parity = USART_Parity_No ;
  43.   USART_InitStructure.USART_HardwareFlowControl =  USART_HardwareFlowControl_None;

  44.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  45.    USART_Init(USART2, &USART_InitStructure);
  46.   /* Enable USART2 Receive interrupts */
  47.   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  48.   /* Enable USART2 */
  49.   USART_Cmd(USART2, ENABLE);

  50.   //---------------------------------------------
  51.   //------------------------RCC_APB1Periph_USART3---------------------
  52.    //RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);  //ʹÄÜ´®¿Ú3ʱÖÓ
  53.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  //ʹÄÜ´®¿Ú3ʱÖÓ
  54.    USART_InitStructure.USART_BaudRate = 115200;  //²¨ÌØÂÊ
  55. // USART_InitStructure.USART_BaudRate = 230400;
  56.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  57.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  58.   USART_InitStructure.USART_Parity = USART_Parity_No ;
  59.   USART_InitStructure.USART_HardwareFlowControl =  USART_HardwareFlowControl_None;

  60.   USART_InitStructure.USART_Mode = USART_Mode_Rx| USART_Mode_Tx; ; //
  61.    USART_Init(USART3, &USART_InitStructure);
  62.   /* Enable USART2 Receive interrupts */
  63.   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  64.   /* Enable USART2 */
  65.   USART_Cmd(USART3, ENABLE);
  66. }
复制代码


你对比一下有没有区别
alisa123 回答时间:2015-10-7 19:00:45
龙龙龙龙龙龙龙龙 发表于 2015-10-4 10:20
void USART_INIT(void)
{
                GPIO_InitTypeDef  GPIO_InitStructure;

你这个代码就是串口1和串口2都能发送和接收的吗?为什么要延迟一会儿再开中断呢?什么道理?
alisa123 回答时间:2015-10-7 19:02:39
chifen 发表于 2015-10-4 14:18
你对比一下有没有区别

你这个是在stm32f030上实现的还是stm32f103上实现的,我之前在stm32f302上两个串口同时用过没问题,但是在stm32f030上就不一定没问题哈
chifen 回答时间:2015-10-7 22:28:09
STM32F103是用原来的固件库做的,STM32F030是原来F050的固件库改的的,STM32CubeMX 最近成生也试过,就是设置麻烦些都可以同时收发的
龙龙龙龙龙龙龙龙 回答时间:2015-10-8 16:52:52
alisa123 发表于 2015-10-7 19:02
你这个是在stm32f030上实现的还是stm32f103上实现的,我之前在stm32f302上两个串口同时用过没问题,但是 ...

stm32f030 系列的,串口两个不冲突,配置都差不多
12下一页
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版