野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15644|回复: 2

STM32F103RB 串口2 能发送 接收不了,大家帮忙看下

[复制链接]
发表于 2017-7-15 19:56:57 | 显示全部楼层 |阅读模式
10火花
void USART2_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        USART_TypeDef *uart;

        /* 第1步:打开GPIO和USART部件的时钟 */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

        /* 第2步:将USART Tx的GPIO配置为推挽复用模式 */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* 第3步:将USART Rx的GPIO配置为浮空输入模式
                由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的
                但是,我还是建议加上便于阅读,并且防止其它地方修改了这个口线的设置参数
        */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        /*  第3步已经做了,因此这步可以不做
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        */
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        /* 第4步:设置串口硬件参数 */
        uart = USART2;       
        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(uart, &USART_InitStructure);
        USART_ITConfig(uart, USART_IT_RXNE, ENABLE);        /* 使能接收中断 */
        /*
                USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
                注意: 不要在此处打开发送中断
                发送中断使能在SendUart()函数打开
        */
        USART_Cmd(uart, ENABLE);                /* 使能串口 */

        /* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
                如下语句解决第1个字节无法正确发送出去的问题 */
        USART_ClearFlag(USART2, USART_FLAG_TC);     /* 清发送外城标志,Transmission Complete flag */
}

/// 配置USART2接收中断
void NVIC2_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        /* Enable the USARTy Interrupt */
        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);
}



void USART2_IRQHandler(void)
{
        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
        {        
            Rxflag2=1;               
                ucTemp2 = USART_ReceiveData(USART2);
        }
         
}

回复

使用道具 举报

 楼主| 发表于 2017-7-15 19:59:32 | 显示全部楼层
大家帮忙看下,我用串口1可以收发都可以,但是增加串口2后,就不行,只能发送不能接收数据;接收中断配置是:
/// 配置USART2接收中断
void NVIC2_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        /* Enable the USARTy Interrupt */
        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);
}
回复

使用道具 举报

 楼主| 发表于 2017-7-15 20:03:01 | 显示全部楼层
还有,是当串口1和0串口2 在配置 NVIC时 ,如果将两个串口接收中断合并一起 :NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn | USART1_IRQn;  后串口1接收中断也失效;
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 11:39 , Processed in 0.028367 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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