OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4401|回复: 2

请教关于STM32F103C8串口3无数据输出问题

[复制链接]

1

主题

7

帖子

0

精华

初级会员

Rank: 2

积分
56
金钱
56
注册时间
2014-1-5
在线时间
7 小时
发表于 2021-3-4 14:19:09 | 显示全部楼层 |阅读模式
1金钱
      我有个制作,需要用到3个串口,串口初始化后,串口1和2都能正常输出,唯独串口3一直没有输出,也查阅了很多资料,也找不出问题所在,请假一下各位大侠,能帮我指点一下吗?小弟先在这里谢过了。

这里我将串口1、2、3的时钟集中在一个函数里配置
void Usart_RCC_Configuration(void)
{
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE)
}


void COM1_Configuration(void)
{       
        GPIO_InitTypeDef GPIO_InitStructure;                                                                                                                                                                                                //重新定义GPIO结构体名称
        USART_InitTypeDef USART_InitStructure;                                                                                                                                                                                        //重新定义USART结构体名称
       
//                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
            
        USART_InitStructure.USART_BaudRate = 115200;                                                                                                  //设置波特率
        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_Mode = USART_Mode_Tx|USART_Mode_Rx;                                                                                          //收发使能
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;                        //硬件流控制失能
        USART_Init(USART1,&USART_InitStructure);                                                                                                                                                   //初始化COM1

        //配置PA9位复用推挽输出TX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                                                                                                                                                 //选择GPIO9
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                                                                                                                    //IO口速率50MHZ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                                                                                                                                            //设置为复用推挽输出
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                                                                                                                                                  //初始化TX引脚

        //配置PA10位浮空输入RX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                                                                                                                                                 //选择GPIO10
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                                                                                                                    //配置为浮空输入       
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                                                                                                                                                  //初始化RX引脚
                 
        USART_Cmd(USART1,ENABLE);                                                                                                                                                                 //使能COM1
}


void COM2_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;                                                                                                                                                                                                //重新定义GPIO结构体名称
        USART_InitTypeDef USART_InitStructure;                                                                                                                                                                                        //重新定义USART结构体名称
       
        USART_InitStructure.USART_BaudRate = 57600;                                                                                                  //设置波特率
        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_Mode = USART_Mode_Tx|USART_Mode_Rx;                                                                                          //收发使能
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;                        //硬件流控制失能
        USART_Init(USART2,&USART_InitStructure);                                                                                                                                                   //初始化COM2

        //配置PA2位复用推挽输出TX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                                                                                                                                                 //选择GPIO2
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                                                                                                                                    //IO口速率50MHZ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                                                                                                                                            //设置为复用推挽输出
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                                                                                                                                                  //初始化TX引脚

        //配置PA3位浮空输入RX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;                                                                                                                                                 //选择GPIO3
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                                                                                                                    //配置为浮空输入       
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                                                                                                                                                  //初始化RX引脚
                 
        USART_Cmd(USART2,ENABLE);                                                                                                                                                                 //使能COM2
}


void COM3_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;                                                                                                                                                                                                //重新定义GPIO结构体名称
        USART_InitTypeDef USART_InitStructure;                                                                                                                                                                                        //重新定义USART结构体名称
       
        USART_InitStructure.USART_BaudRate = 9600;                                                                                                  //设置波特率
        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_Mode = USART_Mode_Tx|USART_Mode_Rx;                                                                                          //收发使能
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;                        //硬件流控制失能
        USART_Init(USART3,&USART_InitStructure);                                                                                                                                                   //初始化COM3
       
        //配置PB10位复用推挽输出TX
        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);
        //配置PB11位浮空输入RX
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                 
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
}


串口发1个字节函数,
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
                USART_SendData(pUSARTx,ch);                                      //  发送一个字节数据到USART      
                while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);   //   等待发送数据寄存器为空
}


main函数里就是初始化3个串口
Usart_SendByte( USART1, ‘A’);
Usart_SendByte( USART2, ‘B’);
Usart_SendByte( USART3, ‘C’);


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

32

主题

236

帖子

0

精华

高级会员

Rank: 4

积分
993
金钱
993
注册时间
2017-8-11
在线时间
137 小时
发表于 2021-3-5 09:05:40 | 显示全部楼层
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE)

给你个建议,像这种多串口一起用的,都分开3个.c去写,一个串口一个篇幅去写,去初始化
这样有的话,以后调用的话,很方便。而且混着来写极容易导致端口初始化冲突
楼主分开写,单独初始化串口3,一个一个排查,问题很快就出来了
回复

使用道具 举报

6

主题

890

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1481
金钱
1481
注册时间
2020-8-19
在线时间
336 小时
发表于 2021-3-5 12:28:13 | 显示全部楼层
C8T6的串口配置可以参照这个里面的.c和.h文件

usart.zip

1.87 KB, 下载次数: 19

回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-10 10:23

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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