打印
[STM32F4]

stm32f407与PC之间USART通信不成功,求大虾们指教啊

[复制链接]
1987|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kevin2512|  楼主 | 2015-8-3 18:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 kevin2512 于 2015-8-3 18:29 编辑

stm32f407 discover开发板与PC之间USART通信不成功,按复位键才出个乱码,一个y字母上面两点的乱码。完全不对啊,复位键一直按住不放,就一直出乱码,松开啥都不出。求大虾们指教
下面是我写的代码,全在main.c文件中,没有用到中断什么的。
串口调试助手,波特率,COM口,等参数都设置正确。
不过,我在stm32f4xx.h和system_stm32f4xx.c中改了两个参数,一个是HSE我改成8M(板子上是8M),原来是25M。还有PLLM把25改成8,其他地方没动过。

#include "stm32f4xx.h"
#include "stdio.h"
#define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)

void USART6_Config(void);
int main(void)
{

   RCC_ClocksTypeDef   RCC_Clocks;
   RCC_GetClocksFreq(&RCC_Clocks);
   
  USART6_Config();  
  printf("\r\n this is a printf demo \r\n");
  printf("\r\n welcome to use discovery board: \r\n");
  
  while (1)
  {                        
  }
}
void USART6_Config(void)
{
    GPIO_InitTypeDef    GPIO_InitStruct ;
    USART_InitTypeDef   USART_InitStruct ;
    /*使能USART1外设时钟(挂在APB2上)和GPIOA外设时钟(挂在AHB1上)*/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
   
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6);  //Tx
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6);//Rx
        
   /*初始化GPIOA*/
      
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
    GPIO_Init(GPIOC,&GPIO_InitStruct);
   
    /*USART1初始化配置*/
    USART_InitStruct.USART_BaudRate=115200;/*波特率为115200bps*/
    USART_InitStruct.USART_WordLength=USART_WordLength_8b;/*发送字长为8位字长*/
    USART_InitStruct.USART_StopBits=USART_StopBits_1;/*停止位为1*/
    USART_InitStruct.USART_Parity=USART_Parity_No;/*无奇偶校验位*/
    USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;  /*不采用硬件流*/
    USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; /*串口双工模式*/               
    USART_Init(USART6,&USART_InitStruct);
    USART_Cmd(USART6,ENABLE); /*使能USART1*/
}

   /*实现重定向printf函数,而重写的fputc()这个C标准库函数*/
   int fputc(int ch,FILE *f)
   {
      /*将printf内容发往串口*/
      USART_SendData(USART6,(uint8_t)ch);
      while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET)/*检测串口数据发送完成标志位TC的值*/
      {}                                                     /*TC=SET(1),表示发送发成*/
      return ch;
    }
/*---------------------------------------下面是断言,不用管--------------------------------------------------------------------------------------*/
#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif



来自 2楼
kevin2512|  楼主 | 2015-8-5 13:01 | 只看该作者
错误找出来了,我晕死啊,那个APB2的时钟使能,把函数搞成复位了。,调了我好几天。总是在最简单的地方出错啊。还难以发现

使用特权

评论回复
板凳
mmuuss586| | 2015-8-3 20:22 | 只看该作者

一直发数据看看,看波特率对不对;

使用特权

评论回复
地板
kevin2512|  楼主 | 2015-8-3 20:36 | 只看该作者
mmuuss586 发表于 2015-8-3 20:22
一直发数据看看,看波特率对不对;

我想是不是时钟频率参数设置的不对呢, 因为库函数里给的外部晶振频率是25M,我的板子上焊接的是8M。
#define PLL_M      8   //在system_stm32f4xx.c中,这里把25改成8;

#if !defined  (HSE_VALUE)     
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */      在stm32f4xx.h中,这里把25000000改成的8000000;
不知道其他地方还要修改不?
串口助手里参数都没错的,波特率我用的是main.c中设置的115200,就是出不来那两行printf;

使用特权

评论回复
5
yklstudent| | 2015-8-5 17:49 | 只看该作者
#include "usart.h"

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configure Open_USART
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void USART_Configuration(void)
{                                                                                                
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  RCC_AHB1PeriphClockCmd(Open_USART_TX_GPIO_CLK,ENABLE);
  RCC_AHB1PeriphClockCmd(Open_USART_RX_GPIO_CLK,ENABLE);

  #ifdef        USART1_OPEN
          RCC_APB2PeriphClockCmd(Open_USART_CLK,ENABLE);
  #else
          RCC_APB1PeriphClockCmd(Open_USART_CLK,ENABLE);
  #endif       


  GPIO_PinAFConfig(Open_USART_TX_GPIO_PORT, Open_USART_TX_SOURCE, Open_USART_TX_AF);
  GPIO_PinAFConfig(Open_USART_RX_GPIO_PORT, Open_USART_RX_SOURCE, Open_USART_RX_AF);

  /*
  *  Open_USART_TX -> PA9 , Open_USART_RX -PA10
  */
  GPIO_InitStructure.GPIO_Pin = Open_USART_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(Open_USART_TX_GPIO_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = Open_USART_RX_PIN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(Open_USART_RX_GPIO_PORT, &GPIO_InitStructure);

/*
                    USARTx configured as follow:
         - BaudRate = 115200 baud  
                 - Word Length = 8 Bits
         - One Stop Bit
         - No parity
         - Hardware flow control disabled (RTS and CTS signals)
         - Receive and transmit   
*/

  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(Open_USART, &USART_InitStructure);
  /* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
     Open_USART transmit data register is empty */
  USART_ITConfig(Open_USART,USART_IT_RXNE,ENABLE);

  USART_Cmd(Open_USART, ENABLE);

}

void USART_NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = Open_USART_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/* Use no semihosting */
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{  
        int handle;
};
FILE __stdout;

_sys_exit(int x)
{
        x = x;
}
#endif

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(Open_USART, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(Open_USART, USART_FLAG_TC) == RESET)
  {}

  return ch;
}
给你一个范例,自己研究下吧

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝