You can redirect thestdout stream when using MPLAB® XC8 C compiler so thatprintf() output is displayed in the simulator's Universal Asynchronous Receiver Transmitter (UART) console in MPLAB X IDE. To do this, you need to perform the following steps:
- Implement theputch() function
- Initialize the UART
- Enable the UART console in the IDE
The putch() Function
Theprintf() function formats the string it has been asked to print by expanding placeholders and modifiers. It then calls the functionputch() to send each character of the formatted text tostdout. By customising theputch() function, you can define the destination ofstdout and haveprintf() "print" to any peripheral on your target device.
To use the UART console feature in the IDE, you must ensure that yourputch() function sends its argument to the transmit register in the UART. The following code will work with most devices and you can copy this into your project. It will be called automatically byprintf().
Initialising the UART
Just like the UART on a real device, the simulated UART needs to be initialized. If you already have code in your project to initialize this peripheral, that same code should also work in the simulator. To use the UART console feature, the simulated UART does not need the same degree of configuration as the actual peripheral, so your code can be simplified if you like. The following code is sufficient for most devices:
Enable the UART console
So that the IDE knows it should be redirecting UART transmissions to a console window, you need to enable this feature. To do this, open theProject Properties dialog for your project. Select theSimulator category, then, in the right-hand pane, select theUart1 IO Options options category. Finally, enable the checkbox markedEnable Uart1 IO.
Let's Print
To have theprintf() output appear in the UART console, all you need to do is call your routine to initialize the UART, then callprintf(). The following test code does just that:
The UART 1 Output window will be automatically opened by the IDE once you run your program in the simulator.



