简体   繁体   中英

“printf” in microcontroller, what is it for?

我在微控制器的c语言示例代码中看到了“printf”指令,特别是在8051中。由于微控制器没有固定的输出显示,“printf”指令的用途是什么?

更可能的是,您可以通过RS232端口连接调试控制台,或者作为在线仿真器的虚拟输出。

printf is defined to output to stdout not an "output display", stdout may be any stream device. Typically on a system without a display it will output to a serial interface (UART), so that a terminal or terminal emulator (HyperTerminal or TeraTerm for example) may be used as a display device.

Some development environments implement "semi-hosting" where stdio, stdin, and stderr, and even in some cases a filesystem are provided by the development host through the debugger interface (JTAG, ICE, SWD etc).

Generally your compiler's library will provide you with hooks or stubs so that you can implement drivers for alternative stream I/O devices, so for example you could implement one so that printf will output to an LCD display if your device has one. This is called "retargetting".

You can interface the microcontroller to the serial port of the PC and monitor the data you

printf

using hyperterminal. Also you can use it for diagnostic purposes

Some development tools allows you to use printf given an implementation of putchar or putch . In such tools, since you have this function sending characters to some device, printf will show messages on that device.

You just have to do the correct device initialization, implement putchar, putch or such (check your compiler/lib docs) and voilá! Your printf will behave the way you're expecting.

PS: Some compilers/libraries do not implement all printf format specifiers. Again, check your docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM