你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F103 UART4,UART5的配置问题

[复制链接]
yzfy123456 提问时间:2018-4-19 16:49 /
大家好!
    本人使用STM32f103RC的UART1、UART2、UART3都很正常,且很好用。
    但是UART4、UART5,始终只能实现发送,却实现不了接收,连中断都进不去
    哪位大侠可以指点一下,非常感谢!

    注:我已添加了 startup_stm32f10x_hd.s 文档,里面是包含有UART4、UART5的
收藏 评论8 发布时间:2018-4-19 16:49

举报

8个回答
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;
        }
}
大陶 回答时间: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的时钟

查看一下这些的

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

七哥 回答时间:2018-4-19 22:29:51
本帖最后由 toofree 于 2018-4-19 22:38 编辑

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

傲游截图20180419222726.jpg


傲游截图20180419222855.jpg

用STM32CubeMX建了个工程,UART4 IO的初始化代码为下图
傲游截图20180419223826.jpg

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

废鱼 回答时间:2018-4-20 08:41:32
我用的103 uart4如下配置:

  1.     // 第1步:打开GPIO和USART部件的时钟
  2.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
  3.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  4.    
  5.     // Configure UART4 Tx (PC.10) as alternate function push-pull
  6.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  7.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  9.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  10.    
  11.     // Configure UART4 Rx (PC.11) as input floating
  12.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  13.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  14.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  15.    
  16.     // Enable the UART4 Interrupt
  17.     NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
  18.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  19.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  20.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  21.     NVIC_Init(&NVIC_InitStructure);
  22.    
  23.    
  24.     // 串口相关配置
  25.     USART_StructInit(&USART_InitStructure);
  26.     USART_InitStructure.USART_BaudRate = 9600;
  27.     USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位宽
  28.     USART_InitStructure.USART_StopBits = USART_StopBits_1;      //停止位
  29.     USART_InitStructure.USART_Parity = USART_Parity_No;         //校验位
  30.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件控制流
  31.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口模式
  32.     USART_Init(UART4, &USART_InitStructure);  // Configure USART3 配置 串口发送方式寄存器
  33.    
  34.     USART_ClockInitS.USART_Clock = USART_Clock_Disable;          //SCLK时钟使能(同步模式下)
  35.     USART_ClockInitS.USART_CPOL = USART_CPOL_Low;                //时钟极性(同步模式下)
  36.     USART_ClockInitS.USART_CPHA = USART_CPHA_2Edge;              //时钟相位(同步模式下)
  37.     USART_ClockInitS.USART_LastBit = USART_LastBit_Disable;      //最后一位时钟脉冲(同步模式下)
  38.     USART_ClockInit(UART4, &USART_ClockInitS);
  39.    
  40.     USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  41.     USART_Cmd(UART4, ENABLE);   
复制代码


评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

hhhhhhhaa 回答时间:2018-11-9 09:13:17
安 发表于 2018-4-20 08:41
我用的103 uart4如下配置:

大神,请教一个问题啊,我用UART4转换成232芯片通讯没问题,用485通讯就不行,配置没问题,也能进中断,我中断配置的是这样的
  1. void UART4_IRQHandler(void)
  2. {
  3.         u8 Res;
  4.          if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
  5.                 {
  6.                 Res =USART_ReceiveData(UART4);       
  7.     Res++;
  8.                 USART_SendData(UART4, Res);
  9.                        
  10.      }
  11. }
复制代码
哪里有问题啊
西点钟灵毓秀 回答时间:2018-11-9 09:59:59
485的收发使能管脚没有配置吧?
bbxyzlyz 回答时间: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, 是不存在上面这些用于同步的设置的
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版