Continuing on the subject of using printf() as the most basic debugging tool, if you are using the MPLAB SIM software simulator to test portions of your code, you might be aware that the PIC simulator offers the possibility to simulate the UART1 module.
Simply open the Debugger menu, select Settings to open the Simulator Settings dialog box and in the UART1 I/O pane check the Enable UART1 I/O checkbox. This will give you two options:
· Record anything transmitted by UART1 into a file
· Send the transmitted data directly to the MPLAB Output window but in a separate pane, next to the Build pane, the Find in Files pane and the Debugger Output panes.
Notice that the UART1 simulation timing is not accurate, which is actually an advantage here. Not only the output is displayed immediately, but this means also that you wont need to worry about the correct baud rate setting/matching (although you still need to remember to enable the module).
Here is the simplest code example I can provide.
Include the standard peripheral libraries (if you have not done so already) at the top of your source file:
Put the following lines in your main() function:
char s; // define a small (string) buffer
OpenUART1(UART_EN, UART_TX_ENABLE, 1); // enable UART1
Whenever needed you can send a simple text message:
putsUART1( I am here!);
Or you can use sprintf() to format a more complex output:
sprintf( s, ” data=%03d \n”, mydata); putsUART1( s);