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

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

[复制链接]
STMCU小助手 发布时间:2022-2-2 18:00
在STM32工程中调用printf函数,需要加入如下代码:
  1. <div align="left">#ifdef __GNUC__     
    2 M2 v2 y8 L, l8 {, G8 }0 m
  2.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf6 i3 t: ?/ m) r- j% Q% ^
  3.      set to 'Yes') calls __io_putchar() */  ~7 Y5 t% p0 K. z
  4.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)1 u8 Y( {" V/ @. G$ b2 X' Y0 C3 g# Q
  5. #else8 S3 Q: w/ H5 l
  6.   #define PCHAR_PROTOTYPE int fputc(int ch, FILE *f)
    / u9 B0 i; s+ i3 H$ D! O" R
  7. #endif /* __GNUC__ UT*// _9 s. M5 M" O
  8. </div><div align="left">void Printf_Init(void)
    , a% a* X6 \5 i9 r2 c
  9. {% [6 ?# Q2 u, q* ^
  10.   /*!< At this stage the microcontroller clock setting is already configured,
    - b$ m) H4 Y# g7 D( ^1 d
  11.        this is done through SystemInit() function which is called from startup
      i$ ~( c- H' e+ D6 e
  12.        file (startup_stm32f10x_xx.s) before to branch to application main.. n* s& A* Y9 E# t6 k3 ^6 S5 M
  13.        To reconfigure the default setting of SystemInit() function, refer to
    6 s" @1 x' K: F9 J! p- K7 n
  14.        system_stm32f10x.c file
    + s- }  t' p7 ~/ U/ H
  15.      */     ! N* g8 m( N: k! a" T* I1 i
  16.        / r/ t7 ]* o) A# |6 M( {
  17.   /* USARTx configured as follow:
    , X6 O# L' M" C( a; Y
  18.         - BaudRate = 115200 baud  4 z+ L1 l% [. U
  19.         - Word Length = 8 Bits5 ~$ d6 U3 y/ k# \  y  @
  20.         - One Stop Bit
    $ c; [  @. V& ~- G4 H. e; \
  21.         - No parity
    5 S' {6 l' v* `, V2 Z4 T* U
  22.         - Hardware flow control disabled (RTS and CTS signals)* P9 T! M) E' P9 W+ J
  23.         - Receive and transmit enabled* ~( c( \8 [1 B; l
  24.   */8 ?2 o9 Q, m, X- g
  25.   USART_InitStructure.USART_BaudRate = 115200;9 v6 v5 o! }7 f# \6 P" W
  26.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    / Q" s6 l: ^1 K9 ~* w  S
  27.   USART_InitStructure.USART_StopBits = USART_StopBits_1;  m4 @9 Z/ W7 e# h& v. U) \
  28.   USART_InitStructure.USART_Parity = USART_Parity_No;1 v* i6 L, s! ~; f+ X
  29.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    5 @; W8 ?: s7 {4 ^+ `: C
  30.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    ' E2 U* w, m3 {, T& D; E2 }

  31. 1 k5 Z, D+ E  C" S( d1 Q
  32.   STM_EVAL_COMInit(COM1, &USART_InitStructure);
    ( i' e$ p9 K( D9 u& s4 m6 m6 `8 ]

  33. : M2 y) w( [9 r8 I5 B% e6 r- r
  34.   /* Output a message on Hyperterminal using printf function */
    - R: W+ Z# Y: _, |: y! X& }
  35.   //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>}/ u$ ]; t6 j" D9 Z

  36. + B/ c5 i* @/ P
  37. /**# Q4 j" c1 P) n+ U5 D% f% U
  38.   * @brief  Retargets the C library printf function to the USART.
    7 e8 q6 A* x% B+ S% N7 \
  39.   * @param  None( z! M. j# p0 B' ?- h2 \( }; R$ x
  40.   * @retval None
    5 r8 d2 s+ }( L2 s5 a
  41.   */2 X" l7 M/ ]$ q( y+ a( _. R$ ^
  42. PUTCHAR_PROTOTYPE
    ! ]$ b) U3 e/ g
  43. {
    0 Z% H+ F6 x# l% O: q+ V5 m
  44.   /* Place your implementation of fputc here */
    $ R; D# L4 g* |) Y, O5 o
  45.   /* e.g. write a character to the USART */
    & y. J8 X8 i' N; c
  46.   USART_SendData(EVAL_COM1, (uint8_t) ch);7 J) L- n, q/ W4 ~5 {1 O  W
  47. * b3 ^! d; }$ y
  48.   /* Loop until the end of transmission *// {8 K. E8 k+ E  x7 z3 K8 M+ v
  49.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
    ; e* H8 Z8 m. b$ o$ ?
  50.   {}
    - L$ {$ [/ ?* R" p

  51. # ?( S& F8 j1 F) Z: @
  52.   return ch;
    & {! ]! o* [+ Z. d5 `+ [3 c- ^
  53. }</div>
    ; d/ E9 U! D5 X1 d6 W
复制代码
* A; L! [: S- \3 C
赞 收藏 评论0 发布时间:2022-2-2 18:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版