MPLAB Simulator - Ece
Personal tools

MPLAB Simulator

From Ece

Jump to: navigation, search

Group Meeting Miniutes

Robotics research group

MPLAB SIMULATOR

In order to test the code, software or hardware is needed that will execute the PIC micro-instructions. A debug execution tool is a hardware or software tool that is used to inspect code as it executes a program. Hardware tools such as MPLAB ICE or MPLAB ICD 2 can execute code in real devices. If hardware is not available, the MPLAB simulator can be used to test the code.

MPLAB SIM The simulator is a software program that runs on the PC to simulate the instructions of the PIC micro MCU. It does not run in “real time.” The simulator program is dependent upon the speed of the PC, the complexity of the code, overhead from the operating system and how many other tasks are running. However, the simulator accurately measures the time it would take to execute the code if it were operating in real time in an application. Simulator is an apparatus that generates test conditions approximating actual or operational conditions. A training device that provides specific, controlled conditions so that skills can be practiced under circumstances closely resembling those experienced in actual performance.

MPLAB SIM is a discrete-event simulator for:

  • PIC microcontroller (MCU) families
  • dsPIC Digital Signal Controller (DSC) families

MPLAB SIM allows you to:

  • Modify object code and immediately re-execute it
  • Inject external stimuli to the simulated processor
  • Set pin and register values at pre-specified intervals
  • Trace the execution of the object code

The speed at which the simulator runs depends on the speed of the computer and how many other tasks are running in the background. The software simulator must update all of the simulated registers and RAM, as well as monitor I/O, set and clear flags, check for break and trace points in software and simulate the instruction with instructions being executed on your computer's CPU. Turning off simulator trace will increase simulation speed by up to 50%.

The simulator executes on instruction cycle boundaries, and resolutions shorter than one instruction cycle (Tcy) cannot be simulated. The simulator is a discrete-event simulator where all stimuli are evaluated, and all responses are generated, at instruction boundaries, or Tcy = 4*Tosc, where Tosc is the input clock period. Therefore, some physical events can not be accurately simulated. These fall into the following categories:

  • Purely asynchronous events.
  • Events that have periods shorter than one instruction cycle.

The net result of instruction boundary simulation is that all events get synchronized at instruction boundaries, and events smaller than one instruction cycle are not recognized.

After selecting the Debugger--> Select Tool --> MPLAB SIM the following changes should be as shown in the following fig.

  • The Status bar on the bottom of the MPLAB IDE window should change to MPLAB SIM.
  • Additional menu items should appear in the Debugger menu.
  • Additional toolbar icons should appear in the Debug tool bar.
Image:screenshot2.jpg

For Single Step through the application program, select, Debugger --> Step Into. This will execute the currently indicated line of code and move the arrow to the next line of code to be executed.

In order to see if the code is operating as intended, sending the present values to PORT C, Watch the values being sent to the Port C. For this select View --> Watch to bring up an empty watch window. The pull downs can be used to add the SFR(Special Function Registers). Select an output pin and then Add SFR Image:screenshot3.jpg Image:screenshot4a.jpg

Stop Watch tells the execution time of the program until the program execution has come to an end or until the processor reaches the breakpoint. To bring up this dialog select Debugger --> Stop Watch. The execution time, which depends on the processor frequency changes if either the program location is changes or if a delay loop is introduced. The following figures shows the values in SFR when the processor has reached the breakpoint (shown by a B encircled in a red in the above fig.) and the values in SFR when the processor execution reaches the breakpoint for the 2nd time.

Image:screenshot5.jpg Image:screenshot6.jpg

Simulator Stimulus is a powerful tool that allows you to control inputs to the simulator and exercise your application code.

Tracing allows you to record the step-by-step execution of your code and examine this recording. The following figure shows the trace window where the step-by-step execution can be seen.

Image:screenshot9.jpg

The Simulator Logic Analyzer allows you to graphically view digital pin signals over a defined time period. The logic analyzer uses the same data as the simulator trace, from the trace buffer and therefore must be enabled. The logic analyzer works even without enabling the trace this is because the trace buffer is enabled by default. We can check this by going to Debugger-->Settings and under the Osc/trace tab, Trace Options where the Trace All box is checked.

The MPLAB SIM logic analyzer has an advantage of importing the values of the waveform/graph so obtained and store it in .txt format. These values can further be used either for verification or for comparing the results. A right click on the logic analyzer window shows the Import Table option. The following Fig. shows the Logic Analyzer window

Image:screenshot10.jpg

Code coverage feature provides visibility as to what portions of the code are being executed. Code coverage differs from traced code in the following way:

  • Trace displays code that has been executed and tells when it was executed.
  • Code coverage marks code that has been executed, but does not display when it was executed.

Advantages of MPLAB SIMULATOR

  • If in case the code you have written isn't giving the desired results and you know not where did something went wrong. You must first know whether it is a hardware error resulted from wrong connections or in the code (the software part) resulted from syntax errors etc. Using Simulator you can easily find the errors in the code and debug them and check for a graphical output.
  • You can also open multiple projects within the same workspace.
  • Have a internal path representation. For this Right click on the project name and add files and at the bottom of the opened window we have Remember Settings. Choose the 2nd option User: Files were created especially for this project, use relative path.

Note:

  • We cannot use Port A and B as these are the ports that are used for the A/D converter.
  • The simulator executes the code for an optimization i.e. it doesn’t execute those instructions which it finds are not used frequently. In order to remove the optimization go to Debugger-->Build Options or Right click on the project window and then Build options and then choose the MPLAB C18 tab. Choose Categories and in the drop down menu choose Optimization and under the General Command window choose Debug. This helps in running the program without missing execution of any instruction.Photographic view of the above said is as shown below

Image:screenshot11.jpg

Image:screenshot12.jpg

  • Also choose the MPLINK linker tab and choose the target address as 0x00. This helps in easy execution for the PC and in choosing the starting of the program. Changing the target address results in change in time taken to execute the program. this can be seen by having a look at the stopwatch before and after changing the target address.
  • If you have trouble with I/O pins on the processor that have A/D make certain that the ADCON registers are configuring those pins for digital I/O rather than for analog input. For most processors, these default to digital inputs and the associated pins cannot be used for I/O until the ADCON (ADCON1) register is set properly.
  • The execution time in terms of instruction cycles change can be noticed when the memory location is changed from 00h to 200 as shown below

Image:screenshot13a.jpg Image:screenshot13b.jpg

The overall screenshot of the code using MPLAB SIM is as shown:

Image:screenshot1.jpg