STMCU小助手
发布时间:2021-12-18 17:43
|
printf重定向介绍 在学C语言的时候,会经常用标准库<stdio.h>中printf(), scanf()这两个函数, 来实现数据的输入与输出. 格式化输入输出的功能还是挺强大的, 那已经有了现成的轮子, 就要寻思一下如何进一步在单片机上使用它. 下面是ARM Compiler6文档中对printf的介绍: ! f: N& b: B+ l- T# Q* ~ The printf family consists of _printf(), printf(), _fprintf(), fprintf(), vprintf(), and vfprintf().5 W6 u: t# D ]: a2 ^* C1 E" b All these functions use __FILE opaquely and depend only on the functions fputc() and ferror(). The functions _printf() and _fprintf() are identical to printf() and fprintf() except that they cannot format floating-point values." j* u' I7 A- d+ ? The standard output functions of the form _printf(…) are equivalent to:3 t/ e, U. L2 K4 \+ o fprintf(& __stdout, …)where __stdout has type __FILE. p' T- a0 E) C6 {9 N 大致是说printf家族有这么些函数<_printf(), printf(), _fprintf(), fprintf(), vprintf(), and vfprintf()>, 这些函数都隐式的使用__FILE<文件流>, 并且只依靠fputc(), ferror()这两个函数. 5 z9 j/ \2 N; r! g3 y If you define your own version of __FILE, your own fputc() and ferror() functions, and the __stdout object, you can use all of the printf() family, fwrite(), fputs(), puts() and the C++ object std::cout unchanged from the library. 重新定义__FILE, fputc(), ferror(), __stdout, 就可以使用printf()家族的各种函数了.. k. D% o1 O/ G/ {' Y; t STM32中printf重定向0 U& l0 C( A4 P! ? 下面是官方给出的重映射模板: <遇到不会的先看看文档往往能得到最直接的答案>$ B: E: _# j* f! k6 l% l0 d
要将printf()重定向到STM32中的串口, 只需要重新实现 fputc() 函数即可, 在fputc()中通过串口发送一个char. 在上一次的工程上进行修改, 将printf()重定向到串口3.
写个简单的程序试一试: 4 {0 e$ Q) Y* c% U9 W* j& r
输出结果:, a. s* l6 Y# N2 v
可以开始愉快的使用printf()了.! l( z- u* { ? & l2 t& r/ F4 `# U 6 h5 i: I: b. ?# | |
【STM32H735G-DK测评】lvgl移植
【STM32H735G-DK 测评】STM32H735G-DK External Loder 编译和测试
【福利三:逢7发帖赢大礼】使用MPU6050模块、img_dsc、img_btn机制实现一些简单有趣的动画效果
【福利三:逢7发帖赢大礼】使用arc弧形拉条控件控制PWM舵机及使用SPI3接口操作NRF24L01模块进行无线通信
【福利三:逢7发帖赢大礼】调试nucleo-WBA65RI工程测试样板的一些心得
【福利三:逢7发帖赢大礼】lv_timer和BME680实现温度、湿度、气压数据读取
【福利三:逢7发帖赢大礼】成功利用STM32H743VI片内SRAM12和DTCM空间(完全胜利)
【福利三:逢7发帖赢大礼】想要充分利用STM32H743VI片内的RAM空间(但并不顺利)
【福利三:逢7发帖赢大礼】使用惠勤致远STM32H743VI开发板+ST7796并口彩屏移植LVGL9.5并分享一些额外内容
【 逢7发帖赢大礼】7、TouchGFX中实现云彩流动效果
微信公众号
手机版