打印
[AT32F403/403A]

相同方法初始化串口,USART1接受到乱码,USART2就正常

[复制链接]
2478|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
z119988w|  楼主 | 2021-8-11 17:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
相同方法初始化串口,USART1接受到乱码,USART2就正常,请问有谁知道是为什么?下面是初始化方法

void USART_Config(USART_Type* USARTx){
        GPIO_InitType GPIO_InitStructure;
        USART_InitType USART_InitStructure;
       
        NVIC_InitType NVIC_InitStructure;
  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        if(USARTx == USART1){
                RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_USART1| RCC_APB2PERIPH_GPIOA , ENABLE);
               
               
                GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
               
                GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9;
                GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
               
                /*中断*/
                NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
                NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
        }else if(USARTx == USART2){
                RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART2 , ENABLE);
                RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA,ENABLE);
//                GPIO_PinsRemapConfig(GPIO_Remap_USART2, ENABLE);  
               
                GPIO_InitStructure.GPIO_Pins = GPIO_Pins_3;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
               
                GPIO_InitStructure.GPIO_Pins = GPIO_Pins_2;
                GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                GPIO_Init(GPIOA, &GPIO_InitStructure);
               
                /*中断*/
                NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
                NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
        }
       
        USART_StructInit(&USART_InitStructure);
  USART_InitStructure.USART_BaudRate = 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(USARTx, &USART_InitStructure);
       
        USART_ClrBuf(USARTx);
        /* Enable USART Receive and Transmit interrupts */
        USART_INTConfig(USARTx, USART_INT_RDNE, ENABLE);
       
       
        USART_Cmd(USARTx, ENABLE);
}


使用特权

评论回复
沙发
嵌入式小白985| | 2022-4-12 10:16 | 只看该作者
这个问题我也遇到了,串口2正常收发,串口1只能发,接收中断不行,问了原厂那边也没给解释说明

使用特权

评论回复
板凳
muyichuan2012| | 2022-4-12 10:48 | 只看该作者
用的at start吧,上面的串口1接到了at link ez,看看电路就明白了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝