LL-395390 发表于 2012-10-17 16:49:43

STM32F207串口无现象

选用的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("\r\ntest gprs\n");
  }
}
  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)
 }
 
 
 

LL-395390 发表于 2012-10-17 16:52:28

RE:STM32F207串口无现象

注释写的是串口2,但程序是串口1的

发表于 2012-10-18 16:22:10

RE:STM32F207串口无现象

建议自己写一个串口发送的试试。

LL-395390 发表于 2012-10-24 08:24:35

RE:STM32F207串口无现象

难道这个不是发送的?写个串口接收的试试?

kevin022 发表于 2012-10-30 11:13:46

RE:STM32F207串口无现象

恩,最好是自己写一个串口收发程序试一下。
顺便问一下,你用什么工具开发的?用J-LINK吗?

LL-395390 发表于 2012-10-30 13:27:16

RE:STM32F207串口无现象

那您觉得我的有问题吗?我是直接往板子里烧写的

LL-395390 发表于 2012-11-16 09:31:36

RE:STM32F207串口无现象

终于解决这个问题了,就是少抄了段代码
/* Use no semihosting */
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
        int handle;
};
FILE __stdout;
_sys_exit(int x)
{
        x = x;
}
#endif
/**
* @briefRetargets the C library printf function to the USART1.
* @paramNone
* @retval None
*/
真是不太懂这段代码!不过问题解决了
页: [1]
查看完整版本: STM32F207串口无现象