(XC16) How do I instruct the compiler to divert the output of printf to UART2?

In the XC16 compiler, the __C30_UART compiler-defined variable is available. It decides the UART channel to use for printing the output of printf(). This is defined in the file c30_uart.c, available under the compiler installation (<Compiler-DIR>\src\libpic30\pic30). The __C30_UART variable decides whether the output is directed to UART1 or UART2. By default, __C30_UART is 1. So, the output of printf() will be directed via UART1. To divert the output from UART1 to UART2, you need to change the __C30_UART value to 2 before invoking the printf() routine.

Example

This program will transmit the “Hello world” string through UART2.

© 2025 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.