z119988w 发表于 2021-8-11 17:39

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

相同方法初始化串口,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]
查看完整版本: 相同方法初始化串口,USART1接受到乱码,USART2就正常