yzfy123456 发表于 2018-4-19 16:49:46

STM32F103 UART4,UART5的配置问题

大家好!
    本人使用STM32f103RC的UART1、UART2、UART3都很正常,且很好用。
    但是UART4、UART5,始终只能实现发送,却实现不了接收,连中断都进不去
    哪位大侠可以指点一下,非常感谢!

    注:我已添加了 startup_stm32f10x_hd.s 文档,里面是包含有UART4、UART5的

yzfy123456 发表于 2018-4-19 17:48:47

下面的我的初始化程序,

void UART4_Initial(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDef USART_ClockInitStructure;
       
        // 使能串口4时钟
        // 注意 UART4, UART5 是挂载在APB1总线上的,用RCC_APB1PeriphClockCmd()函数初始化!
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);       
       
        // PC10 做 UART4_TX
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       GPIO_Init(GPIOC, &GPIO_InitStructure);

      // PC11 做 UART4_RX
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_Init(GPIOC, &GPIO_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(UART4, &USART_InitStructure);

        USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
        USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
        USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
        USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

       USART_ClockInit(UART4, &USART_ClockInitStructure);

        USART_Cmd(UART4, ENABLE);
        USART_ClearFlag(UART4,USART_FLAG_TC);

        // 使能UART4接收中断
        USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
}

// UART4接收中断入口函数
void UART4_IRQHandler(void)
{
        unsigned char value;

        if(UART4->SR & (1<<5))
        {
                value = UART4->DR;
                       
                UART4_Rx_OK = TRUE;
        }
}

dataozi 发表于 2018-4-19 17:53:59

void UART4_IRQHandler(void) 这个是UART4的中断服务函数

USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
USART_Cmd(UART4, ENABLE); 在查看一下有没有这两句话

RCC_APB1Periph_UART4, ENABLE 串口4的时钟

查看一下这些的

toofree 发表于 2018-4-19 22:29:51

本帖最后由 toofree 于 2018-4-19 22:38 编辑

PC11在做串口接收功能时,是否应该设置为输入状态呢?输出的话,与外面的信号会打架。






用STM32CubeMX建了个工程,UART4 IO的初始化代码为下图

发表于 2018-4-20 08:41:32

我用的103 uart4如下配置:

    // 第1步:打开GPIO和USART部件的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
   
    // Configure UART4 Tx (PC.10) as alternate function push-pull
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
   
    // Configure UART4 Rx (PC.11) as input floating
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
   
    // Enable the UART4 Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    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(UART4, &USART_InitStructure);// Configure USART3 配置 串口发送方式寄存器
   
    USART_ClockInitS.USART_Clock = USART_Clock_Disable;          //SCLK时钟使能(同步模式下)
    USART_ClockInitS.USART_CPOL = USART_CPOL_Low;                //时钟极性(同步模式下)
    USART_ClockInitS.USART_CPHA = USART_CPHA_2Edge;            //时钟相位(同步模式下)
    USART_ClockInitS.USART_LastBit = USART_LastBit_Disable;      //最后一位时钟脉冲(同步模式下)
    USART_ClockInit(UART4, &USART_ClockInitS);
   
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
    USART_Cmd(UART4, ENABLE);   

hhhhhhhaa 发表于 2018-11-9 09:13:17

安 发表于 2018-4-20 08:41
我用的103 uart4如下配置:

大神,请教一个问题啊,我用UART4转换成232芯片通讯没问题,用485通讯就不行,配置没问题,也能进中断,我中断配置的是这样的void UART4_IRQHandler(void)
{
        u8 Res;
       if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
                {
                Res =USART_ReceiveData(UART4);       
    Res++;
                USART_SendData(UART4, Res);
                       
   }
}哪里有问题啊

zts329547875 发表于 2018-11-9 09:59:59

485的收发使能管脚没有配置吧?

欧阳敏琪zZ 发表于 2019-5-23 16:22:26

你好   请问一下有没有配置usart3同步模式CPOL+CPHA=00/01/10情况下,通信是否正常。

edmundlee 发表于 2019-5-23 16:44:18

USART1, USART2, USART3,
到了4,就不是USARTA4,而是UART32, 少了个S, 就是非同步(异步)的意思

      USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
      USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
      USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
      USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
       USART_ClockInit(UART4, &USART_ClockInitStructure);
UART4, 是不存在上面这些用于同步的设置的
页: [1]
查看完整版本: STM32F103 UART4,UART5的配置问题