STM32工程中调用printf函数

在STM32工程中调用printf函数,需要加入如下代码:
  1. #ifdef __GNUC__      
  2.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  3.      set to 'Yes') calls __io_putchar() */
  4.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  5. #else
  6.   #define PCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  7. #endif /* __GNUC__ UT*/
  8. void Printf_Init(void)
  9. {
  10.   /*!< At this stage the microcontroller clock setting is already configured, 
  11.        this is done through SystemInit() function which is called from startup
  12.        file (startup_stm32f10x_xx.s) before to branch to application main.
  13.        To reconfigure the default setting of SystemInit() function, refer to
  14.        system_stm32f10x.c file
  15.      */     
  16.        
  17.   /* USARTx configured as follow:
  18.         - BaudRate = 115200 baud  
  19.         - Word Length = 8 Bits
  20.         - One Stop Bit
  21.         - No parity
  22.         - Hardware flow control disabled (RTS and CTS signals)
  23.         - Receive and transmit enabled
  24.   */
  25.   USART_InitStructure.USART_BaudRate = 115200;
  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.   STM_EVAL_COMInit(COM1, &USART_InitStructure);
  32.   /* Output a message on Hyperterminal using printf function */
  33.   //printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
  34. //  printf("\r\n\n\n WWW.ARMJISHU.COM  %s configured....", EVAL_COM1_STR);
  35. //  printf("\n\r ############ WWW.ARMJISHU.COM! ############ ("__DATE__ " - " __TIME__ ")\n\r");
  36. }
  37. /**
  38.   * @brief  Retargets the C library printf function to the USART.
  39.   * @param  None
  40.   * @retval None
  41.   */
  42. PUTCHAR_PROTOTYPE
  43. {
  44.   /* Place your implementation of fputc here */
  45.   /* e.g. write a character to the USART */
  46.   USART_SendData(EVAL_COM1, (uint8_t) ch);
  47.   /* Loop until the end of transmission */
  48.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  49.   {}
  50.   return ch;
  51. }
一见钟情 发表于10-25 09:26 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

一见钟情
一见钟情(大校)
金币:1529个|学分:3659个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号