野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13314|回复: 3

求助!STM32F4 UART4无法正确发送数据

[复制链接]
发表于 2016-9-7 17:27:48 | 显示全部楼层 |阅读模式
请大神指教一下,自己根据官方提供的例程实现了UART4发送数据的功能,刚开始使用串口调试助手可以正确显示UART4发送的数据; 但不知什么原因,现在使用串口调试助手无法正确显示需要UART4发送的数据,请大神帮忙分析一下会是哪里出现的问题?


  1. /UART4的配置函数/
  2. void Usart_Config(void)
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.         USART_InitTypeDef USART_InitStructure;
  6.        
  7.         RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK|DEBUG_USART_TX_GPIO_CLK,ENABLE);
  8.        

  9.   RCC_APB1PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
  10.        

  11.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  12.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  13.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.        

  15.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  16.   GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN  ;  
  17.   GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);


  18.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  19.   GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN;
  20.   GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
  21.        

  22.   GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT,DEBUG_USART_RX_SOURCE,DEBUG_USART_RX_AF);


  23.   GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_SOURCE,DEBUG_USART_TX_AF);
  24.        

  25.   USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;

  26.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  27.   USART_InitStructure.USART_StopBits = USART_StopBits_1;

  28.   USART_InitStructure.USART_Parity = USART_Parity_No;

  29.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  30.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  31.   USART_Init(DEBUG_USART, &USART_InitStructure);
  32.        

  33.         NVIC_Configuration();
  34.   

  35.         USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);
  36.        

  37.   USART_Cmd(DEBUG_USART, ENABLE);
  38. }

复制代码
  1. /主函数/
  2. int main(void)
  3. {
  4.         LED_GPIO_Config();
  5.         Key_GPIO_Config();
  6.         Usart_Config();
  7.        
  8.         LED1(OFF);
  9.         LED2(OFF);
  10.         LED4(OFF);

  11.         while(1)
  12.         {
  13.                 if( Key_Scan(KEY1_GPIO_PORT,KEY1_PIN) == KEY_ON  )
  14.                 {

  15.                  LED1_TOGGLE;

  16.            Usart_SendString(DEBUG_USART,"1234"); //输出字符串1234

复制代码
当调用函数Usart_SendString(DEBUG_USART,"1234");输出字符串1234时,串口调试助手上显示能够接收到数据,但显示的数据并不是“1234”,
请帮忙分析一下,问题可能出现在哪里?会不会是软件环境哪里没有配置正确
168OBZ1JQ6PB8B5HN8ZB2MK.png
回复

使用道具 举报

发表于 2016-9-7 17:30:13 | 显示全部楼层
使用printf函数测试一下,另外确认一下程序的波特率
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-7 21:23:52 | 显示全部楼层
随风 发表于 2016-9-7 17:30
使用printf函数测试一下,另外确认一下程序的波特率

改用printf函数也不正确,波特率设置的是115200,应该没有问题
  1. //宏定义
  2. /*******************************************************/
  3. #define DEBUG_USART                             UART4
  4. #define DEBUG_USART_CLK                         RCC_APB1Periph_UART4
  5. #define DEBUG_USART_BAUDRATE                    115200  //′®¿ú2¨ìØÂê

  6. #define DEBUG_USART_RX_GPIO_PORT                GPIOA
  7. #define DEBUG_USART_RX_GPIO_CLK                 RCC_AHB1Periph_GPIOA
  8. #define DEBUG_USART_RX_PIN                      GPIO_Pin_1
  9. #define DEBUG_USART_RX_AF                       GPIO_AF_UART4
  10. #define DEBUG_USART_RX_SOURCE                   GPIO_PinSource1

  11. #define DEBUG_USART_TX_GPIO_PORT                GPIOA
  12. #define DEBUG_USART_TX_GPIO_CLK                 RCC_AHB1Periph_GPIOA
  13. #define DEBUG_USART_TX_PIN                      GPIO_Pin_0
  14. #define DEBUG_USART_TX_AF                       GPIO_AF_UART4
  15. #define DEBUG_USART_TX_SOURCE                   GPIO_PinSource0

  16. #define DEBUG_USART_IRQHandler                  UART4_IRQHandler
  17. #define DEBUG_USART_IRQ                                                 UART4_IRQn
  18. /************************************************************/
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-7 21:25:18 | 显示全部楼层
随风 发表于 2016-9-7 17:30
使用printf函数测试一下,另外确认一下程序的波特率

个人觉得不像是代码部分的问题,会不会是配置环境上的问题,什么地方可能导致这样的错误呢
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 01:49 , Processed in 0.048089 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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