How to start debugging with MPLAB

Discussion in 'Embedded Systems and Microcontrollers' started by Fanfire174, Apr 17, 2019.

  1. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    I have a pickit 3 and I work with PIC 8-bit MCU's like PIC16F877A

    IDE : MPLABX
    Compiler: XC8
    Language : C

    I follow the following steps to program PIC Microcontroller

    Project -> Double click on project Name
    Build -> Run

    Does anybody know the steps to debug code with MPLAB?
     
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,121
    2,011
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    18,818
    5,869
    Have you read the Guide?
    Guide
    Max.
     
    atferrari and nsaspook like this.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716

    In MPLABX, I would need to go out on a limb and take a wild guess that you should click on Debug

    upload_2019-4-17_21-53-33.png
     
    Last edited: Apr 17, 2019
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716

    You need to use the guide to know how to start the debugger? :confused:
     
  6. MaxHeadRoom

    Expert

    Jul 18, 2013
    18,818
    5,869
    Question asked
     
  7. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    there is debug option in MPLAB IDE

    upload_2019-4-18_16-27-47.png

    I can build and Run the code but I don't understand how to debug following example code ?

    Code (C):
    1. // 'C' source line config statements
    2.  
    3. // CONFIG
    4. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    5. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    6. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    7. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    8. #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    9. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    10. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    11. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    12.  
    13.  
    14. #include <xc.h>
    15.  
    16. #define _XTAL_FREQ 20000000 //Specify the XTAL crystall FREQ
    17.  
    18. #define PIR RC0
    19. #define Buzzer RB2
    20.  
    21. void main() //The main function
    22. {
    23.    TRISB=0X00; //Instruct the MCU that the PORTB pins are used as Output.
    24.    TRISC=0Xff; //Instruct the MCU that the PORTB pins are used as Input.
    25.    PORTB=0X00; //Make all output of RB3 LOW
    26.  
    27.    while(1) //Get into the Infinie While loop
    28.   {
    29.     if(PIR ==1)
    30.     {
    31.         Buzzer=1;
    32.         __delay_ms(1000);   //Wait
    33.     }
    34.     else
    35.     {
    36.         Buzzer=0;
    37.     }
    38. }
    39.  
    40. }
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
    Debug main Project is disabled for some reason. Never seen that. Do you have a Pickit 2 or 3 connected? Is it a real tool or a clone?
     
  9. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    I have Pickit 3 connected When I click on debug button

    I get debugger windows

    upload_2019-4-20_15-50-26.png

    How do you debug code ?
     
  10. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,121
    2,011
    There is no short answer to that question. There are so many things you can do.
    However, if you click on the left margin of a line of your code a red blob will appear. This sets a breakpoint.
    Now when you run your code in debug mode it will be stopped at your breakpoint and you can view and change RAM and SFRs.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
    In other words:

    RTFM
     
    atferrari likes this.
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716

    Hint the debugger is a misnomer. It is not going to debug the code for you. ;)
     
  13. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    I was trying to set breakpoints can you tell me how to do it?

    upload_2019-4-20_17-21-51.png
     
  14. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,121
    2,011
    Your image shows a breakpoint set at line 21.
    If you now run the code it will stop at that breakpoint.
    Click the button immediately to the left of the blue MCC shield.
     
  15. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    But we set two breakpoints to find a problem in the code.

    Can you attach a screenshot of debugging process?
     
  16. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
    Why don't you read the manual? And there are bound to be tons of generic tutorials on the internet that show how to use a debugger. They all have the same concepts.
     
  17. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,716
    Learning how to operate the debugger is very easy. All you need to do is follow the instructions in the manual of how to operate the various tools like breakpoints and watches.

    You can find definitions of breakpoints, watches etc on the internet. They are common tools used by most every debugger.

    Learning how to use those tools to help you troubleshoot your problem is an entirely different matter. I am not sure that can even be taught. It is just going to take experience.
     
  18. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,121
    2,011
    Start at section 4.17 of the MPLAB(X) user guide and follow that through.
     
    Fanfire174 likes this.
  19. Fanfire174

    Thread Starter Member

    Mar 13, 2018
    204
    0
    AlbertHall

    I followed user guide but I am using letest IDE and compiler There may be some problem

    I followed this instruction

    File -> New Project
    Microchip Embedded -> Standalone Project
    Next
    Family -> Device -> PIC16F877A
    Next
    Hardware Tool -> Pickit3
    Next
    compiler tool chain -> XC8
    Next
    Project Name -> PIR
    Finish

    Click on project PIR
    source file -> new-> main.c-> PIR.c
    finish

    save code

    click on project -> Build
    click on project -> Run
    click on project -> debug

    set break pont ctrl + F8

    But variable value doesn't change during debug process
     
  20. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    645
    178
    The debug facilities on the pic16f877a are extremely minimal... There is no debug header available so you are limited with ICD ( in circuit debugging ) to 1 level, which is rubbish.. If I were you, I would debug on the simulator as much as possible... When I used pickit3 for debugging I only get two breakpoints and that was on a mid range... I since use Proteus.. Real time debugging in a simulator.. Or as near as damn it..

    If you are going to use ICD, you will be better off with ICD2.. There is a reason pickit's are cheap...
     
    Fanfire174 likes this.
Loading...