STM32F103ZET6串口2可以串口3收到的是乱码

2019-08-19 17:42发布

本帖最后由 zzq11986 于 2017-5-8 10:36 编辑

各位路过大侠请指点:
下面为串口代码:串口2正常接收
void USART2_Init(void)
{
           GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
          NVIC_InitTypeDef NVIC_InitStructure;
        
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
        
        

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       //推挽输出-TX
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入-RX
    GPIO_Init(GPIOA, &GPIO_InitStructure);

          /*串口2参数初始化*/

    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                USART_Init(USART2,&USART_InitStructure);
        
          //USART_DeInit(USART2);//复位串口2
        
        
          USART_Cmd(USART2,ENABLE);/*使能串口2*/
        
          USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//开启串口2接收中断,用到时开启
        
          NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
          NVIC_Init(&NVIC_InitStructure);//中断初始化
}

void USART2_IRQHandler(void) //中断请求处理函数
{
  u8 res;
        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
        {
           res= USART_ReceiveData(USART2); //接收串口2数据
     USART_SendData(USART2,res);     //将res发送给USART2
        }
}

int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//将中断优先级设置为组2
USART2_Init();
while(1);
}


下面为串口3代码、电路板连接图片和串口软件图片,串口发出来的数据,电脑接收为乱码
void USART3_Init(void)
{
           GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
          NVIC_InitTypeDef NVIC_InitStructure;
        
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟
        
        

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       //推挽输出-TX
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入-RX
    GPIO_Init(GPIOB, &GPIO_InitStructure);

          /*串口3参数初始化*/

    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                USART_Init(USART3,&USART_InitStructure);
        
          //USART_DeInit(USART2);//复位串口3
        
        
          USART_Cmd(USART3,ENABLE);/*使能串口3*/
        
          USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//开启串口3接收中断,用到时开启
        
          NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
          NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
          NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
          NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
          NVIC_Init(&NVIC_InitStructure);//中断初始化
}

void USART3_IRQHandler(void) //中断请求处理函数
{
  u8 res;
        if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
                {
                         res= USART_ReceiveData(USART3); //接收串口3数据
                         USART_SendData(USART3,res);     //将res发送给USART3
                }
        
}

int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//将中断优先级设置为组2
USART3_Init();
while(1);

4.png 5.png 电脑收到的乱码 电脑收到的乱码

请各位高手帮帮忙,小弟在这先谢谢了!!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
正点原子
1楼-- · 2019-08-19 23:33
是不是你代码波特率设置有问题啊?
正点原子
2楼-- · 2019-08-20 04:29
短接COM3_TX和COM3_RX,做一下自发自收实验,看看是否OK?
正点原子
3楼-- · 2019-08-20 10:10
看了你的图片,发现你的开关打错了!!!要打到RS232位置
flb666
4楼-- · 2019-08-20 12:35
 精彩回答 2  元偷偷看……
zzq11986
5楼-- · 2019-08-20 16:11
正点原子 发表于 2017-5-8 20:55
短接COM3_TX和COM3_RX,做一下自发自收实验,看看是否OK?

我把P11、p10接到COM2_TX和COM2_RX,通过COM2(DB9接口)连接到电脑上是可以正常接收的
zzq11986
6楼-- · 2019-08-20 20:18
正点原子 发表于 2017-5-8 20:55
是不是你代码波特率设置有问题啊?

115200及以下我全部试验了,都不行

一周热门 更多>