Hello [Name]

When a console program needs to obtain information from the user, such as the user's name, it uses gets or perhaps scanf to obtain the desired input. The debug routines included in the PIC32 Starter Kit provide this capability. When the PIC32 board requests input, through the JTAG/USB interface, the host computer brings up a dialog window where the user can enter the requested information.


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

hello2.c


01: /* hello2.c
02: *
03: *    Try db_gets
04: */
05: 
06: #include <p32xxxx.h>
07: #include "db_utils.h"
08: 
09: int main()
10: {
11:         char name[25];
12:         DBPRINTF("Basic Starter Kit Lab (" __DATE__ ", " __TIME__ ")\n");
13:         DBPRINTF("What is your name?\n");
14:         DBGETS(name,24);
15:         DBPRINTF("Hello, %s!\n",name);
16:         DBPUTS("Program terminated. Click HALT and then RESET to stop the microcontroller. \n");
17:         return 0;
18: }


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

Exercises

  1. Try out this example. What happens when you enter names with fewer than four characters? What happens when you enter a name with more than 24 characters?

  2. In this example, the input is stored in a character array on the stack (automatic memory variables). We can change the program so that the input is stored in static memory:
    char name[25];
    
    int main()
    {
       ...
    

    Does the program behave the same way with shorter and longer names?

  3. Compare the memory usage for the two programs and fill out the following table. How much data memory is reserved for the static array?

    codeprogramdata
    original43231544
    modified


Maintained by John Loomis, updated Mon Jul 28 18:40:04 2008