Question

I see "printf" instruction in sample codes of c language for microcontroller particularly in 8051. Since microcontrollers has no fixed output display what is the use of the "printf" instruction?

Was it helpful?

Solution

More likely than not, so you can attach a debugging console, either through an RS232 port, or as virtual output from an in-circuit emulator.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top