STM32F207串口问题

2019-03-23 19:29发布

选用的CPU是 STM32F207ZG,自己就写了个简单的串口1程序,想在串口调试助手上看到printf中的内容。在keil4里建立工程,并编写程序,编译没有问题,但是烧写到开发板里没有现象。不知道错在哪里,请大家帮忙看看!我用的是STM32F2xx_StdPeriph_Lib_V1.0.0库函数。谢谢了!
main.c:
#include "stm32f2xx.h"
#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__ */
int main(void)
{
  SystemInit();   /*配置系统时钟为120M*/
Usart_Config();  /*配置串口*/

   while (1)
    {
      printf(" test gprs ");
  }
}
  PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}
usart.c:
#include "usart.h"
  void Usart_Config(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   /*   typedef struct
    {
    u32 USART_BaudRate;   波特率
    u16 USART_WordLength;  一个帧中传输或者接收到的数据位数
    u16 USART_StopBits;   停止位数目
    u16 USART_Parity;   奇偶模式
    u16 USART_HardwareFlowControl;   硬件流控制模式
    u16 USART_Mode;     使能或者失能发送和接收模式
    u16 USART_Clock;     USART时钟使能或者失能
    u16 USART_CPOL;   指定SLCK引脚上时钟输出的极性
    u16 USART_CPHA;   指定了SLCK引脚上时钟输出的相位
    u16 USART_LastBit;  控制是否在同步模式下,在SCLK引脚上输出的最后发送的那个数据字(MSB)对应的时钟脉冲                        
    } USART_InitTypeDef;
   */
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    //配置GPIOA和USART2时钟
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);  

   /*配置串口2的TX*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最高输出速率50MHz
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;    //AF复用
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //PP推挽式
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);    // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)

   /*配置串口2的RX*/
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;    //OD Open-Drain 开漏
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOA, &GPIO_InitStructure);    // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)

   /*配置串口2的模式*/
   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(USART1, &USART_InitStructure);   // void USART_Init(USART_TypeDef *USARTx,)
   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
   USART_Cmd(USART1, ENABLE);  // void USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState)
} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。