Example code for 32-bit PIC MCUs

If you want to run example code in the MPLAB X IDE simulator, the IDE's UART IO feature, available for PIC32 devices, allows you to view output from the stdout stream. Once properly configured, the output of printf() and other functions writing to stdout can then be viewed from the IDE when the program is run in the simulator. This IDE feature is not available for SAM MCUs.

When available, this feature is enabled using the Enable Uartx IO checkbox in the Project properties > Simulator > Uartx IO Options dialog (shown below). You might have a choice of UARTs. Choose the UART that your code will write to. Output can be displayed in a window in the IDE or sent to a file on your host machine, based on the selections you make in the dialog.

Figure 1. Enabling the UART IO feature in the MPLAB X IDE

When using 32-bit PIC devices, the IO helper functions provided by the MPLAB XC32 compiler will work with the simulator's UART IO feature without any modification; however, remember that you might need to modify these helper functions to suit applications running on your own hardware. All that is required in your project is code to select which UART output should be sent to.

The following code shows UART2 being selected for output (using the __XC_UART variable) before text is printed to the IDE's UART IO feature.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <xc.h>

int main(void) {
    
    __XC_UART = 2; // set UART 2 for the simulator
    printf("The sine of 0.5 is %f\n", sin(0.5));
    
    return (EXIT_SUCCESS);
}