找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2003|回复: 4
打印 上一主题 下一主题
收起左侧

STM32使用USART1没什么问题,使用USART3就是搞不出来 也不知程序哪里错了

[复制链接]
回帖奖励 10 黑币 回复本帖可获得 10 黑币奖励! 每人限 10 次
跳转到指定楼层
楼主
求一个stm32f103rct6使用USART3收发数据的历程,不胜感激!!!

void USART3_init(u32 bound)
{
    GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);        //??USART3,GPIOB??
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);        //??USART3,GPIOB??
    USART_DeInit(USART3);  //????3
         
    //USART3_TX   PB10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //??????
    GPIO_Init(GPIOB, &GPIO_InitStructure); //???Pb10
    //USART3_RX          PB11
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//????
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //???PB11

    //Usart1 NVIC ??
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//?????3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //????3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ????
    NVIC_Init(&NVIC_InitStructure);        //??????????VIC???

    //USART ?????
    USART_InitStructure.USART_BaudRate = bound;//?????115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???8?????
    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);//??USART3????
    USART_Cmd(USART3, ENABLE);                    //????
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:592807 发表于 2021-11-5 11:08 | 只看该作者
USART3_RX引脚没有复用

51hei截图20211105110814.png (96.99 KB, 下载次数: 74)

51hei截图20211105110814.png
回复

使用道具 举报

板凳
ID:313048 发表于 2021-11-5 14:06 | 只看该作者
去找原子或者野火的demo自己捣鼓他们的USART1的初始化就知道了,或者百度随便找个例程也很多,对比下就知道了。
回复

使用道具 举报

地板
ID:584195 发表于 2021-11-8 06:36 | 只看该作者
我在工程应用的初始化函数给你:2楼是正解,学习了:

void USART3_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* 使能 USART3 时钟*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能外设时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* USART3 使用IO端口配置 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //浮空输入
    GPIO_Init(GPIOB, &GPIO_InitStructure);   //初始化GPIOA

    /* USART3 工作模式配置 */
    USART_InitStructure.USART_BaudRate = 19200;        //波特率设置:19200 步进电机的通讯率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //数据位数设置:8位
    USART_InitStructure.USART_StopBits = USART_StopBits_1;         //停止位设置: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);  //初始化USART3

    /*使能串口3的发送和接收中断*/
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);


    USART_Cmd(USART3, ENABLE);// USART3使能
}
回复

使用道具 举报

5#
ID:897866 发表于 2021-11-27 18:49 | 只看该作者

这样改行不行

void USART3_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* 使能 USART3 时钟*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能外设时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* USART3 使用IO端口配置 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //浮空输入
    GPIO_Init(GPIOB, &GPIO_InitStructure);   //初始化GPIOA

    /* USART3 工作模式配置 */
    USART_InitStructure.USART_BaudRate = 19200;        //波特率设置:19200 步进电机的通讯率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //数据位数设置:8位
    USART_InitStructure.USART_StopBits = USART_StopBits_1;         //停止位设置: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);  //初始化USART3

    /*使能串口3的发送和接收中断*/
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);


    USART_Cmd(USART3, ENABLE);// USART3使能
}
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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