龙龙龙龙龙龙龙龙 发表于 2015-9-30 11:30:44

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

单独配置串口1和串口2使用没问题,但是一旦配置两个之后串口1就使用不了了? 管脚映射正常,不清楚哪里出现问题,望高手出招。

alisa123 发表于 2015-9-30 16:03:02

我在stm32f030C8同时用过Usart1,并且通信ok;现在在stm32f030R8上同时用Usart1和Usart2,结果Usart2就是不能进入接收和发送中断,楼主你单独用usart2是ok的吗?

你好我好大家好! 发表于 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

你最好把你 设置的代码 加上来看看

龙龙龙龙龙龙龙龙 发表于 2015-10-4 10:20:13

void USART_INIT(void)
{
                GPIO_InitTypeDefGPIO_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);
}

chifen 发表于 2015-10-4 14:18:46



/*******************************************************************************
* Name: UART1_Configuration´®¿Ú³õʼ»¯
* Deion      : Configures the uart1
* Input                  : None
* Output               : None
* Return               : None
*******************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDefUSART_ClockInitStructure;

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


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

                                                                           //²¨ÌØÂÊ
USART_InitStructure.USART_BaudRate = 38400;
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 USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);


//------------------------RCC_APB1Periph_USART2---------------------
   //RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);//ʹÄÜ´®¿Ú2ʱÖÓ
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//ʹÄÜ´®¿Ú2ʱÖÓ
USART_InitStructure.USART_BaudRate = 38400;//9600;//²¨ÌØÂÊ
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);
/* Enable USART2 Receive interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);

//---------------------------------------------
//------------------------RCC_APB1Periph_USART3---------------------
   //RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, ENABLE);//ʹÄÜ´®¿Ú3ʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//ʹÄÜ´®¿Ú3ʱÖÓ
   USART_InitStructure.USART_BaudRate = 115200;//²¨ÌØÂÊ
// USART_InitStructure.USART_BaudRate = 230400;
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);
/* Enable USART2 Receive interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART3, ENABLE);
}


你对比一下有没有区别
页: [1] 2 3
查看完整版本: 关于STM32F030C8T6 串口1与串口2共同使用的问题