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

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

[复制链接]
STMCU小助手 发布时间:2022-2-2 18:00
在STM32工程中调用printf函数,需要加入如下代码:
  1. <div align="left">#ifdef __GNUC__     " R! L, {# e2 X. }9 J
  2.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf% W0 U  X/ x' u8 Z0 R: g7 I8 L
  3.      set to 'Yes') calls __io_putchar() */' Y& M/ D; q5 _
  4.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
    0 g, K1 c& Q. J
  5. #else
    1 l2 {! G- K  H% |/ Y
  6.   #define PCHAR_PROTOTYPE int fputc(int ch, FILE *f)1 H& t& O- V' j$ f% N
  7. #endif /* __GNUC__ UT*/( Z6 n! j% k# U' k( p- ^
  8. </div><div align="left">void Printf_Init(void)
    : e5 u2 h, Q; _+ b- }! X
  9. {
    " A  }+ ~, t. q+ ^( K8 U. F
  10.   /*!< At this stage the microcontroller clock setting is already configured,
    6 \2 C( v6 G+ g+ i& J
  11.        this is done through SystemInit() function which is called from startup, R# A& Z1 d) [) ^7 l
  12.        file (startup_stm32f10x_xx.s) before to branch to application main.
    ) i5 L9 H" r' r# R
  13.        To reconfigure the default setting of SystemInit() function, refer to" m8 r5 Z( ~: Z# \2 }7 z6 k6 x
  14.        system_stm32f10x.c file
    5 x) W; Y, X' x2 m9 b
  15.      */     
    8 v$ I9 m2 W& S- L! z; m- Y
  16.       
    % q1 |0 T* i7 S  u$ O; n
  17.   /* USARTx configured as follow:* x3 I! u* E  s2 w8 z0 S
  18.         - BaudRate = 115200 baud  5 |0 k2 `6 C' S# D2 D0 b0 E
  19.         - Word Length = 8 Bits
    6 A$ P. E6 A3 J' u' G
  20.         - One Stop Bit
    9 I/ o* E8 G) F
  21.         - No parity4 S5 _( i- M. i4 F  v/ M" B
  22.         - Hardware flow control disabled (RTS and CTS signals)  a3 w  Y+ ^% s
  23.         - Receive and transmit enabled
    ' T8 \3 J# c( W; n
  24.   */. N0 ~8 \) i# ?" d5 P6 J
  25.   USART_InitStructure.USART_BaudRate = 115200;
    2 `! }" T& k4 @2 B8 v  v" W
  26.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;  h% p4 h5 e! {0 `
  27.   USART_InitStructure.USART_StopBits = USART_StopBits_1;6 z& W% I) Y/ V5 S
  28.   USART_InitStructure.USART_Parity = USART_Parity_No;* y, `1 Y, Q8 C( Y7 f
  29.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  m/ w5 |6 f- M
  30.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;5 x4 [; P/ b( z7 r! S) m" _: S

  31. 4 ~2 S3 k/ j7 S8 T9 v2 q% Q
  32.   STM_EVAL_COMInit(COM1, &USART_InitStructure);2 \7 v9 O3 x2 s) l- C* n

  33.   H! O- N# n7 B: s5 ]
  34.   /* Output a message on Hyperterminal using printf function */% l  O  ~0 O% P. z
  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>}
    . \$ c0 G2 N- e

  36. 1 n) U) V! I) |% q/ h
  37. /**+ l! s3 `5 V3 q, M! V) F4 H9 F
  38.   * @brief  Retargets the C library printf function to the USART.
    6 ^+ a" H# x6 Z5 u8 H
  39.   * @param  None7 j" R' F' I8 L5 `8 g5 c
  40.   * @retval None
    & {8 n: j  ^8 r0 U* o; n8 o3 ?$ G& I
  41.   */
    $ N7 ?8 n5 z2 ^
  42. PUTCHAR_PROTOTYPE
    6 z/ w1 s7 Z0 O6 h* c8 a" U
  43. {
    - M: {$ S- H' t3 V# v: x, X3 m
  44.   /* Place your implementation of fputc here */4 o, Z1 D, b3 W! _5 q4 R) ^
  45.   /* e.g. write a character to the USART */6 }& w% |. z- G" a2 H1 U; u% ~$ ^
  46.   USART_SendData(EVAL_COM1, (uint8_t) ch);! a( l0 U9 m' i
  47. 6 i! G+ ~5 ~& \. g+ z  C
  48.   /* Loop until the end of transmission */, ~1 f9 c% Q* ^5 V
  49.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)4 U6 o6 X  f! f- p. \
  50.   {}
    / D9 V% T# _. o/ K$ F* p3 T6 d: i# f

  51. ! {/ S3 M4 S  u$ }' s( X
  52.   return ch;
    ) w  k+ M" b" t  z
  53. }</div>8 K2 i& t5 x$ v9 u: c- M) ^3 N8 t% j
复制代码
5 I! V5 B! e" }1 l3 B& ]1 k8 G5 e8 r3 N
赞 收藏 评论0 发布时间:2022-2-2 18:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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