打印

STM32F207Z串口问题

[复制链接]
5006|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
lqab1983|  楼主 | 2012-9-28 13:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用STM32F207Z处理器,在keil4里建立工程,使用的是STM32F2xx_StdPeriph_Lib_V1.0.0固件函数库。编写的是最简单的串口程序。在keil4里编译没有问题,将.hex文件烧写进开发板,竟然没有现象?请问怎么回事?

usart.c:

#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__ */

  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_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_Cmd(USART1, ENABLE);  // void USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState)
}


main.c :

#include "stm32f2xx.h"
#include "usart.h"

int main(void)
{
  SystemInit();   /*配置系统时钟为120M*/
Usart_Config();  /*配置串口*/
  
   while (1)
    {
     printf("test gprs\n");
  }
}
沙发
lqab1983|  楼主 | 2012-9-28 13:38 | 只看该作者
注释写错了,程序是 串口1,一开始是串口2,后来改了。
请教问下 怎么回事?

能给个详细的串口流程吗

使用特权

评论回复
板凳
香水城| | 2012-9-28 15:45 | 只看该作者
首先,STM32F2最新的例程库版本是V1.1.0,你可以更新下


这里例程库里USART有专门PRINTF的例程,可以直接用的,你可以试试


我估计是你项目设置的问题

使用特权

评论回复
地板
airwill| | 2012-9-28 19:33 | 只看该作者
串口设置没有问题, 我怀疑楼主是不是没有给时钟. 特别是容易忘记给 GPIO 口时钟了.

使用特权

评论回复
5
figo20042005| | 2012-9-28 22:59 | 只看该作者
LZ可以试下3,4楼的

使用特权

评论回复
6
dream-yi| | 2012-9-29 11:40 | 只看该作者
时钟好像错了吧, RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    //配置GPIOA和USART2时钟 这个好像有点问题吧  你看看手册,好像是GPIOA好像是RCC_AHB2Periph的吧

使用特权

评论回复
7
lqab1983|  楼主 | 2012-10-22 09:30 | 只看该作者
谢谢大家回复,我一直没上论坛看,不好意思,但我一直再调这个小程序。我想说明几点:
1.STM32F2XX 的库 貌似与F1的库不一样,GPIOA的时钟好像就是AHB1Peripheral,因为我修改为AHB1会报错,而且我有别人的参考例程,也是AHB1的。
2.其实在 usart.c里还缺少
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;
} 这段代码,才可以用printf .    但我加上了依然不行。

3. 3楼说的项目设置问题,其实我觉得有可能,因为这个工程是我自己照着建立的,有些不是很清楚,但是我把别人的我验证过能用的207例程整个工程复制过来,在user里全部换成我的.c、.h文件,编译依然没有错,但是还是没现象。


真的好麻烦,大家 想想还有什么问题?谢谢!

使用特权

评论回复
8
lqab1983|  楼主 | 2012-10-25 09:40 | 只看该作者
帮帮忙呗

使用特权

评论回复
9
自然的天逸| | 2012-10-25 10:11 | 只看该作者
串口没有问题 我写过程序的/*
;*****************************************************************************************************
;* 函数名称 : showLedInit
;* 描    述 : 初始化RS232串口
;* 输          入 : baudrate: 波特率
;*        
;* 输          出 : TRUE 或 FALSE
;*****************************************************************************************************
;*/
static void IcIdInit(void)
{               
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd( EVAL_COM4_RX_GPIO_CLK, ENABLE);
  
  RCC_AHB1PeriphClockCmd( EVAL_COM3_RX_GPIO_CLK, ENABLE);

  RCC_APB1PeriphClockCmd(EVAL_COM3_CLK, ENABLE);

  RCC_APB1PeriphClockCmd(EVAL_COM4_CLK, ENABLE);

  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(EVAL_COM4_RX_GPIO_PORT, EVAL_COM4_RX_SOURCE, EVAL_COM4_RX_AF);

   /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(EVAL_COM3_RX_GPIO_PORT, EVAL_COM3_RX_SOURCE, EVAL_COM3_RX_AF);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = EVAL_COM4_RX_PIN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(EVAL_COM4_RX_GPIO_PORT, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = EVAL_COM3_RX_PIN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(EVAL_COM3_RX_GPIO_PORT, &GPIO_InitStructure);

  USART_InitStruct.USART_BaudRate = 9600;
  USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  USART_InitStruct.USART_StopBits = USART_StopBits_1;
  USART_InitStruct.USART_Parity = USART_Parity_No;
  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStruct.USART_Mode = USART_Mode_Rx;

  USART_ITConfig(EVAL_COM4, USART_IT_RXNE, ENABLE);

  USART_ITConfig(EVAL_COM3, USART_IT_RXNE, ENABLE);

  /* USART configuration */
  USART_Init(EVAL_COM4, &USART_InitStruct);

  USART_Init(EVAL_COM3, &USART_InitStruct);
   
  /* Enable USART */
  USART_Cmd(EVAL_COM4, ENABLE);

  USART_Cmd(EVAL_COM3, ENABLE);
}

使用特权

评论回复
10
自然的天逸| | 2012-10-25 10:12 | 只看该作者
本帖最后由 自然的天逸 于 2012-10-25 10:15 编辑

重复发了

使用特权

评论回复
11
lqab1983|  楼主 | 2012-11-16 09:22 | 只看该作者
断断续续在搞,终于知道为什么不行了  少粘了一段宏代码:
/* 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 USART1.
  * @param  None
  * @retval None
  */

这些宏定义真是不太明白   不过终于解决了

使用特权

评论回复
12
xiaoxintian| | 2015-8-17 11:59 | 只看该作者
lqab1983 发表于 2012-11-16 09:22
断断续续在搞,终于知道为什么不行了  少粘了一段宏代码:
/* Use no semihosting */
#if 1

这段宏是加在哪个文件中的?

使用特权

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

本版积分规则

17

主题

53

帖子

0

粉丝