Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to enable debug mode in PIC?

Status
Not open for further replies.

electroRF

Member
Hi,
I got PIC18f4520, Pickit3, and MPLAB X IDE + XC8 Compiler installed.

I'm trying to debug the program but can't get the PIC into debug mode.

That is how I configured the PIC in my empty program:

#include <stdio.h>
#include <stdlib.h>
#include <p18f4520.h>
#include <xc.h>

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG4L
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)


/*
*
*/
int main(int argc, char** argv)
{


return (EXIT_SUCCESS);
}



But, it ends with:
Programming/Verify complete
The target device is not ready for debugging. Please check your configuration bit settings and program the device before proceeding.


Could you please share how to enable debug mode?

Thanks.
 
Last edited:
Hi,

You don't appear to have set CONFIG1H, i.e. told the pic what oscillator you are going to use. Try adding #pragma config CONFIG1H = 0x08;

Hint: Look under: Window / PIC Memory Views / Configuration Bits
 
Hi,
Thanks :) it seems to work!
Is the main function called periodically?
It seems like it when I debug the program.
 
int main(int argc, char** argv)
{
return (EXIT_SUCCESS);
}

You shouldn't return from main when programming for a microcontroller. When you are programming for a PC you exit when you are finished and control returns to the OS, but a uC doesn't have an OS. When main was called no return address was pushed onto the stack, so when you try to return from main it will cause a stack underflow. If you want your PIC to just stop and not do anything until it is reset, put in an infinite loop or disable interrupts and go to sleep.
 
Hi hedonms, thanks!

How do you explain then that when I was in debug mode, and reached into the end of the main function, I returned to the beginning of the main function?
 
Read your datasheet. Section 5.1.2

Essentially, you are crashing the PIC by telling it to do something that is impossible and it is resetting.
 
Thanks mate, got it.

btw, are you sure that uc has not OS?

I used to work on 8051 which had some sort of OS which moved in a cyclic way from one function to another.
 
Yes, quite sure.

There are OSs that you can load onto an 8051, but I'm fairly certain they don't have one from the factory.

Not all uCs will have a reset on underflow function, some might simply treat a return with an empty stack as a NOP and continue running what ever code is present after the main function, are you sure that isn't what you were seeing?
 
Status
Not open for further replies.

Latest threads

Back
Top