Difference between revisions of "NU32v2: Using the Simulator"

From Mech
Jump to navigationJump to search
Line 27: Line 27:


<ol>
<ol>
<li> Download and unzip [[Media:MPLAB_Invest_Example.zip]] </li>
<li> Download and unzip [[Media:MPLAB_Invest_Example.zip|MPLAB_Invest_Example.zip]] </li>
<li> Launch the invest MPLAB project, invest.mcp </li>
<li> Launch the invest MPLAB project, invest.mcp </li>
<ol style="list-style-type:lower-roman">
<ol style="list-style-type:lower-roman">

Revision as of 02:20, 5 January 2011

NDM 1/4/2010 Page Under construction


Introduction to the MPLAB Simulator

The MPLAB Simulator is a debugging tool that allows you to test code without programming it onto a PIC32. Simple debugging, such as watching the values of variables or registers, can be performed, as well as advanced features, such as simulating inputs from a UART.


Starting a Simulator Project in MPLAB v8.63

  • Open MPLAB v8.63.
    • If a project is already open, close it by going to Project -> Close -> (the project name)
  • Start a New Project by going to Project -> Project Wizard.
  • Follow the steps in the Project Wizard.
    • Select the PIC32MX795F512L as your device.
Project Wizard Device.
    • Select the Microchip PIC32 C-Compiler Toolsuite as your Active Toolsuite. Select the MPLAB C32 C Compiler (pic32-gcc.exe) v1.1(b) from the Toolsuite Contents.
Project Wizard Compiler.
    • Place your New Project File in a new folder.
    • Add existing files to your project folder if you have any.
    • Click Finish to create the New Project.

Sample Code

To learn how to use the Simulator, add the following sample code to your project: ...

Setting up MPLAB SIM

To run the sample code in the simulator,

  1. Download and unzip MPLAB_Invest_Example.zip
  2. Launch the invest MPLAB project, invest.mcp
    1. open invest.c
  3. Go to Debugger->Select Tool->MPLAB SIM
  4. Go to Debugger->Settings... and click on the "Uart1 IO" tab
  5. In the "Uart1 IO" tab
    1. click on "Enable Uart1 IO" checkbox
    2. in the "Input File" field, click the "Browse" button and add input.txt
    3. click on the "Window" radio button
    4. hit "OK" to change the settings and close the window
  6. Go to Project->Build Options...->Project, select the "MPLAB PIC32 Linker" tab and
    1. enter a heap size, 4096 bytes works for this example
    2. hit "OK" to change the settings and close the window
  7. Compile the program (you can press F10 or go to Project->Make)
  8. Run the debugger (press F9 or go to Debugger->Run)
  9. If you see text similar to what you saw in the first day of class scrolling by in the Output window, then everything is working correctly. Next, we'll briefly go over a few basic features of the debugger, but let's reset the debugger first.
  10. Halt the debugger by pressing F5 or Debugger->Halt
  11. Reset the simulation/microcontroller by pressing F6 or Debugger->Reset->Processor Reset

Working with the Debugger

The MPLAB debugger has several useful tools and commands. In this section, we will cover the following:

  • setting breakpoints
  • basic debugging commands: Halt, Reset, Step Into, Step Over, and Run
  • understanding the Locals window.

The Basic Debug Commands

The debug commands do the following:

Halt
stops the program at the current instruction
Reset
resets the PIC to its default state and starts the program from the very beginning
Step Into
executes the next valid C expression in your code and then halts the program
Step Over
If the next expression is a function call, then it executes that function and then halts the program. Otherwise, for non-function calls, this is equivalent to Step Into. IMPORTANT, if you don't have the source code for a function you should STEP OVER it. Not doing so can lead to MPLAB not responding to your commands, because its trapped inside the function.
Run
executes the program from the last executed expression until a breakpoint is encountered or Halt is selected

There are several ways to execute the debugging commands (see Table 1)

Table 1: How to Access the Basic Debugging Commands
Halt Reset Step Into Step Over Run
Keyboard F5 F6 F7 F8 F9
Menubar Debugger->Halt Debugger->Reset->Processor Reset Debugger->Step Into Debugger->Step Over Debugger->Run
Debugger Toolbar pic1 pic2 pic3 pic4 pic5


The walkthrough will use the keyboard shortcuts, but the other methods can be substituted for the shortcuts. Before we begin, make sure that invest.c is opened, line numbers are visible, and the debugger is not running and has been reset.

Debugging invest.c

  1. Set a breakpoint at line 104 in invest.c
    • A breakpoint allows you to stop a program mid-execution. You can set breakpoints at any time, even while the program is running. To set a breakpoint, double-click the gray side-pane on the number 104 or right-click the line of code at line 104 and select "Set Breakpoint". You should now see a red circle with the letter 'B' in it.
  2. Press F9 to start the debugger
    • You'll see a green arrow on top of the breakpoint. The arrow indicates the next instruction that will execute when the program resumes. The next instruction to execute will be the function call to initUart1().
    • If you hover over any local variable, like num, you can see their current value (by default the numbers are shown in hexadecimal).
  3. Press F8 to Step Over the initUart1() function.
    • Notice how the debugger skipped over this function. Alternatively, because we have access to the source code, we could have chosen to Step Into the function to see it in action. Instead, we step over it, because it contains code that we won't cover until later in the course.
    • The debugger has advanced the program to the next instruction in our main function (as denoted by the green arrow). The next instruction is a function call to getUserInput(...).
  4. Press F7 to Step Into getUserInput(...).
    • Instead of hovering over local variables to see their value, we can display them in a window
  5. Go to View->Locals, to bring up the Locals window.
  6. Press F8 once to advance the program
    • You'll see the Locals window populated with values of all of the current local variables, such as valid and input.
    • Keep in mind that you can only see the individual values of a statically allocated array.
  7. Change the display of valid from hexadecimal to decimal by
    • right-clicking the row in Locals that valid is in and click on "Properties"
    • in the "Format" drop-down menu select "decimal" and accept changes by pressing "OK"
  8. Keep pressing F8 until you hit line 145
    • the Locals window now displays a new value for valid, it should equal 1, due to the assignment in line 142
    • a nice feature about a debugger is that you can change the value of any variable or array in real-time. The Locals window is one window where this is possible.
  9. Under the column "value", double-click the field corresponding to the value of valid
    • You should now be able to enter text
  10. Change the value for valid from 1 to 782 followed by your enter key.
    • this won't affect the normal execution of the program because the value is still true
  11. Set another breakpoint in calculateGrowth(...) by double clicking line 175
  12. Press F9 to Run
    • now you've skipped over a bunch of lines and stopped at the next breakpoint in the program.

At this point you've covered the basics of working with a debugger. Feel free to practice what you've learned on the for loop. For example, change the value of i while the loop is running, set extra breakpoints, etc.

Further information regarding the debugger can be found in the "MPLAB IDE Features/Debug Features" section of the "MPLAB IDE" .chm help file under Help->Topics....

Troubleshooting MPLAB SIM

MPLAB SIM isn't responding to any of my commands
MPLAB SIM isn't perfect and if it doesn't respond to a command after a period of time, it may be caught in an infinite loop. You may have to restart the simulator by selecting Debugger->Select Tool->None and then Debugger->Select Tool->MPLAB SIM. If that doesn't work exit MPLAB and start again.