野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 655|回复: 2

STM32F407 USART3串口不能接收

[复制链接]
发表于 2025-2-11 16:41:43 | 显示全部楼层 |阅读模式
我最近做STM32F407 USART3,但是串口不能接收请问是怎么回事

代码如下:

void UART3_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
         /* USART2 -> GPS */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

  /* 配置中断源 */
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);   /* PA2 AF7 USART2_TX */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
                       
   USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate            = 115200;
    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(USART3, &USART_InitStructure);
       
        /*配置串口接收中断*/
        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
       
  USART_Cmd(USART3, ENABLE);
}


中断接收

void USART3_IRQHandler(void)
{
    if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {
        USART3_RxBuffer[USART3_RxLength] = USART_ReceiveData(USART3);
        USART3_RxLength++;
                       
                                if(USART3_RxLength > (USART3_MAXRxLength-1)){
                                        USART3_RxLength = 0;
                                        memset(USART3_RxBuffer, 0, sizeof(USART3_RxBuffer));
                                }
                          if((USART3_RxBuffer[USART3_RxLength-2]==0x0D)&&(USART3_RxBuffer[USART3_RxLength-1]==0x0A)){
                                        C2_flag = 1;
                                        USART3_RxLength = 0;
                                }
    }
}       


现在是进入不了接收中断,请问是哪里的问题
回复

使用道具 举报

发表于 2025-2-12 11:34:54 | 显示全部楼层
先排除硬件问题
回复 支持 反对

举报

发表于 2025-2-24 10:56:26 | 显示全部楼层
有没有可能是你设置的IO有问题,正常情况下,串口的IO不是一个输入,一个输出的么。
回复 支持 反对

举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2025-4-3 06:30 , Processed in 0.184862 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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