你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 printf 函数原型

[复制链接]
STMCU小助手 发布时间:2022-2-2 18:00
在STM32工程中调用printf函数,需要加入如下代码:
  1. <div align="left">#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. </div><div align="left">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");<div align="left">//  printf("\r\n\n\n <a href="http://WWW.ARMJISHU.COM" target="_blank">WWW.ARMJISHU.COM</a>  %s configured....", EVAL_COM1_STR);</div><div align="left">//  printf("\n\r ############ <a href="http://WWW.ARMJISHU.COM" target="_blank">WWW.ARMJISHU.COM</a>! ############ ("__DATE__ " - " __TIME__ ")\n\r");</div>}

  34. /**
  35.   * @brief  Retargets the C library printf function to the USART.
  36.   * @param  None
  37.   * @retval None
  38.   */
  39. PUTCHAR_PROTOTYPE
  40. {
  41.   /* Place your implementation of fputc here */
  42.   /* e.g. write a character to the USART */
  43.   USART_SendData(EVAL_COM1, (uint8_t) ch);

  44.   /* Loop until the end of transmission */
  45.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  46.   {}

  47.   return ch;
  48. }</div>
复制代码

收藏 评论0 发布时间:2022-2-2 18:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版