(XC32) How do I instruct printf to output to UART1 or other UART channels instead of UART2?

By default, MPLAB® XC32's libraries use UART2 for STDOUT. This means that formatted output functions such as printf() use UART2.

The MPLAB XC32 compiler provides a __XC_UART variable that you may use to switch the default to UART 1. After including the xc.h header file, just add the following statement to your code in one of your functions: __XC_UART = 1. Currently, only UART1 and UART2 can be used by setting the __XC_UART to 1 or 2 respectively.


In some cases, you may want to use some other peripheral or mechanism for your output, other than UART1 and UART2. To do this, you can provide a custom _mon_putc() function. The _mon_putc() function will be called by printf to pass each character and _mon_putc() will output to the module as defined in the function.


This example writes to UART3. “Hello World” will be displayed in the output window via the simulator's SIM UART3 IO tab. Make sure the UART IO is configured under the Debugger Settings in MPLAB X.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.