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);
If you define and configure a baud rate for UART1 in your code, the printf and UART output are run with delay, as it should to properly simulate. The printf command will take the proper cycles to operate. This can mess with stimulus inputs to the UART. I had a printf statement in a receive ISR when I encountered this. I scratched my head for some time until I figured it out by placing breakpoints and watching it work fine without the printf. It wasn’t exiting the ISR before the next input byte arrived, and it dropped the rest of the data.
I am glad I stopped by. I just started a new project: porting some code I wrote a few years ago for an AVR Atmega processor to a new PIC32. Having printf available should make my job much easier.