Hello from PIC32


Embedded computers seldom have an attached console. The development configuration for the PIC32 Starter Kit consists of your computer connected to the PIC32 board via a USB cable. The starter kit includes a small debug library that provides console I/O (puts and gets) through the USB cable to the MPLAB IDE.

The mechanics of using the MPLAB IDE are described in a separate tutorial. This document is devoted to the code itself. Our first example is shown below. The necessary code files and MPLAB project and workspace files are packaged in hello1.zip.

hello1.c


01: /* hello1.c
02: * The db_utils run only on the starter kit (not in SIM)
03: * Note also that DBINIT() does not do anything.
04: * You must define the symbol PIC32_STARTER_KIT to use the debug print utility.
05: */
06: 
07: #include <p32xxxx.h>
08: #include "db_utils.h"
09: 
10: 
11: int main()
12: {
13:         DBINIT(); //Initialize the IO channel
14:         DBPRINTF("Basic Starter Kit Lab (" __DATE__ ", " __TIME__ ")\n");
15:         DBPRINTF("Hello from PIC32 \n");
16:     /* Apparently there must be at least one DBPUTS to load the DB library */
17:         DBPUTS("Program terminated. Click HALT and then RESET to stop the microcontroller. \n");
18:         return 0;
19: }


The first statement (line 7)

#include <p32xxxx.h>

imports a file that in turn contains more #include statements designed to import the file appropriate to the device currently selected. In our case this would be p32mx360f512.h. We could have referenced this file directly, but we choose to use the generic reference to make the project more device independent and hopefully easier to port to other devices.

An ordinary C program (see Hello, World!) would also import stdio.h. Here we import debug utility routines provided in the PIC32 Starter Kit and defined through db_utils.h. We have made the following observations about this debug package:

Memory Usage

Select View > Memory Usage Gauge from the top menu of the MPLAB IDE to display the following diagram:

For the PIC32 processor, the program memory is given in 32-bit words (instructions) and the data memory in bytes.

Results

A screen capture of the MPLAB IDE after running this program is shown below. Console output appears in the Output window.

Exercise

Systemically remove (or comment out) each line in the program from line 13 through line 18. Rebuild the program and record the program memory and data memory usage at each step. The last version consists of an empty main program:

int main()
{

}

Complete the following table:

removeprogramdata
--43121544
13

14

15

17

18

Note that after lines 14-15 are removed, there are no more references to printf. After line 17 is removed there are no more references to the debug utility. After line 18 removed the only code remaining is device start-up program code and data.


Maintained by John Loomis, updated 13 July 2011