Spanish Italian
17454 Users    

How to Debug with MPLAB

  Download PDF version of the Article

Debugging with MPLAB

Usually complex programs, (sometimes even simple programs), do not work properly or, at least, they do not work as we wish. Sometimes the errors do not occur immediately, they arise when particular situations happen. Therefore the programmer must review the code and its operations to determine what must be modified to get the wanted results. So the "bugs" in the programs must be removed if we want they work properly. Once the code has been built and checked from the syntax point of view, it needs to be tested.
In a development environment the execution code must be checked by a debugger: it can be a hardware device to analyse the program during its execution or it can be a software that simulates the micro-controller operations. MPLAB IDE owns its simulator, so the programs can be tested without any hardware device. Two types of hardware can be used with MPLAB IDE: programmers and hardware debuggers. By a programmer the code can be modified, reprogrammed in the micro-controller in order to be retested, but it takes time especially if we have at hand a complex code. As a consequence it will be very hard to understand what is going wrong in the device functioning. The universal remedy? An hardware debugger. Hardware debuggers, like MPLAB REAL ICE, can be in-circuit emulators, which use specialized hardware in place of the actual target micro-controller, or they can be, like MPLAB ICD2, in-circuit debuggers, which use micro-controllers that have special built in debugging features. Therefore the debugger is a part of MPLAB IDE, and whether we are using MPLAB SIM, MPLAB ICE or MPLAB ICD 2, most operations are exactly the same. This allows to develop code using the simulator, then when the hardware is ready, we can use a hardware debugger to further test the code in practice without having to learn how to use a new tool.
To summarize, we have three types of supported debuggers:
- software solution - simulator

software solution - simulator

- combined software/hardware solution - in-circuit debuggers

- advanced software/hardware solution - in-circuit emulators

Let's spend some words about the MPLAB SIM that has some unique debugging features that are not available in the hardware debuggers. MPLAB SIM has features to simulate hardware interaction with other signals and devices, and since it is running as software on the PC, it has complete information about the internal state of the simulated chip at each instruction. This is a little difference from the hardware debuggers because, while they are running code at full speed, they typically cannot monitor all registers and all memory in real time. So the simulator simulates the operation of:
- the core CPU and it’s internal registers
- memory
- many of its peripherals.
Simulation includes pin inputs and outputs as well as many of the other peripherals. The peripherals communicate to the application through special function registers. In order to test the application on the simulator, stimulus signals can be applied to pins and registers. In the picture below how to select MPLAB SIM from the Debugger menu is shown.

MPLAB SIM  from the Debugger menu

MPLAB SIM can accurately measure code timing using the Stopwatch or the time stamp feature of the Trace Buffer.
The stopwatch
The Stopwatch dialog can time sections of code as they run on the simulator. The Stopwatch calculations are based upon the instructions executed and the setting entered for the processor frequency. From the number of instruction cycles executed the total time is calculated.

Stopwatch

The trace buffer
The trace buffer allows to capture events and time them. The trace buffer has the advantage that it can capture large amounts of data selectively and each instruction has a time stamp. It is possible to capture an interrupt routine, for instance, and then easily calculate the time between interrupts and the total time each interrupt took to execute.
A complex Stimulus Generator simulates signals that can be applied to the device under simulation. The stimulus generator can send signals to pins or to registers in the simulator. These signals can be triggered manually, can be set up as lists of events, can be repetitive waveforms, and can be activated by complex conditions. There are three types of Stimulus sources for MPLAB SIM:
- Manual triggers are changes in digital signal levels caused by clicking on a button with a mouse. These allow to simulate the action of closing a switch, or pulsing a pin.
- A Cyclic stimulus generates a repeating waveform, either a for a predetermined length of time, or continuously.
- Sequential data is data that can be applied to pins, registers, or bits in registers from a list.
Here is the Stimulus controller dialog box for MPLAB SIM.

Stimulus controller

Let's make an example using the Stimulus controller. In the dialog box we can select "actions" to apply to a "pin" and then, when the program is running, we can press the associated “Fire” button to the left of the pin name to activate that signal. Suppose to set up a pulse to occur on PORTA pin RA3. When we press the Fire button, the simulated signal on RA3 will pulse high for some microseconds (8 mµ in this example).

Stimulus Controller

More actions can be added.
- one to force the INT0 pin low when we press the fire button, and
- one to toggle the INPUT pin for the Timer/Counter.
A “Toggle” Action will alternate between setting the pin high and low each time the Fire button is pressed.

In the MPLAB IDE, all the debuggers (hardware and software) allow watch windows, breakpoints and single stepping to go through the code and test it. According the chosen device other debugger options can be available.
Watch windows
To see what the application is doing, the debug windows allow to see the program contents, data or other types of memory devices. In particular this window allows to select only the variables or registers that we want to observe. If we want to see, for example, the COUNT variable value (of course we must have a COUNT variable in our program!) we must click on the pull down on the right and click on "Add Symbol".In the same way we can see the special function register value (PORT C), clicking on the pull down on the left and therefore clicking on "Add SFR". Below you can see the sequence:

 

Breakpoints
They are useful to stop the program execution in fixed points: so memory, register or variable values, after a run time execution, can be observed. By the watch window we can see the values of the registers and variables at the block point. Below a breakpoint is shown. (B in red colour).

Single stepping
It is possible to go through the code step by step, monitoring affected variables, registers and flags. The option " Step Into" will execute the currently indicated line of code and move the arrow to the next line of code to be executed. Below
a Step Into is shown (green arrow).

The above mentioned debugging tools are common to all the PIC families:
8 bit PIC Micro-controllers: PIC10/PIC12/PIC14/PIC16/PIC18
16 bit PIC Micro-controllers: PIC24H/PIC24F/dsPIC33F
32 bit PIC Micro-controllers: PIC32
But the PIC32 micro-controller has a peculiar, powerful debug tool that allows it to interact with MPLAB in interactive manner. In a coming article this debug tool will be discussed.

MPLAB Code view capabilities

It is possible to visualize C code and asm code in the same window :
Select View->Disassembly Listing.
Remember to enable listing as well in Project->Build Options

Finally I get what I wanted

Finally I get what I wanted to! Thank you very much for presenting all these information to us. I am sure that a lot of people were looking for it and never did find it. Thanks and I can't wait to see what you will surprise us with in the future!

MPLAB SIM

Hi everyone!

I'm using MPLAB SIM for debug my program. In main function, i called some function which in MPLAB library (header file in C:\Program Files\Microchip\MPLAB C30\support\peripheral_30F_24H_33F, source code in ...\MPLAB C30\src\peripheral_30F_24H_33F\src\pmc)

I don't know the way how it's linked in MPLAB. While i debug, i can't "step into" any function in library. I think this issue relate MPLAB SIM configuration but i can't do. Anybody help me ?

Thanks and best regards

RE: MPLAB SIM

Hi, Unfortunately, I cannot say I am able to replicate the behavior you describe. What I have noticed, however, is that while stepping inside a built in function, the registers in the SFR window do not always get updated. I am not sure, however, if this is a bug of MPLAB or if it is just a missunderstanding from me.

Regards,
Cristian

Hello to all, In MPLAB SIM,

Hello to all,

In MPLAB SIM, I want to feed INT0 pin with the pulses comming from RA0 and get the interrupt. Is it possible to simulate an interrupt this way?

thanks

RE: Hello to all, In MPLAB SIM,

Hi ramjisitanal,
It is possible to do what you want. In order to do this, you can navigate at Debugger->Stimulus->New workbook, and in the window that opens click on the Clock Stimulus tab. There you can make the necessary settings to generate a pulse train on any pin of the micro. If you only want a trigger on INT0 on demand, then check the default Asynch tab.

Regards,
Cristian

Hello Cristian, Thanks for

Hello Cristian,

Thanks for your valuable feed. I will follow the process that you have mentioned.

Cristian, I tried 'Clock

Cristian,

I tried 'Clock Stimulus' with following values:

Label : new_pulse
Pin : RC1
Initial : High
Low Cyc : 10
Hi Cyc : 10
Begin : Start
End : Never
Comments : none

All works well, But when I trace Pin RC1 with inbuilt simulated Logic Alalyser of MPLAB, I am unable to see the Hi, Low pulses at this pin(RC1).

Where I am wrong?

Thanks.

RE: Cristian, I tried 'Clock

Hi ramjisitanal,
Sorry, but using the same settings like you I can see a train pulse with a period of 20us in the Logic Analyzer. I want to point out however, that when I select the channel to be plotted in the analyzer, I choose channel "INT0", not channel "RC1". Please try that, ramjisitanal.

Regards,
Cristian

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
15 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Who's new

  • fernand
  • Ligrock
  • paolo_0665
  • chanuei
  • JM
  • samsilva77
  • araghube
  • stoll
  • mt
  • orionkw

Who's online

There are currently 0 users and 52 guests online.