# Pickit 2 debugger stuck in delay loop when used to debug PIC16F877A

#### goutham1995

Joined Feb 18, 2018
94
My pic microcontroller seems to be stuck in a delay loop when i am debugging using a pickit 2 clone which i bought from a local store. When i use the pickit 2 to program the microcontroller, it works fine but when i use it to debug, it enters the debug session and when it enters the delay loop, it stays stuck there. What can be wrong?

#### be80be

Joined Jul 5, 2008
2,049
_PWRTE_ON is incompatible with debugging as the debugger and the debug executive may fail to synchronise (chip and clock speed dependent). Also you mustn't set _DEBUG_ON as MPLAB must manage this bit itself. If you turn it on, under some circumstances MPLAB has been known to toggle it back off when attempting to program the PIC for debugging. You may also have a noise pickup problem. How long are the connections between the PICs and the PICkit 2? Do both PICs have adequate decoupling on all Vss and Vdd pin pairs?

#### spinnaker

Joined Oct 29, 2009
7,835
My pic microcontroller seems to be stuck in a delay loop when i am debugging using a pickit 2 clone which i bought from a local store. When i use the pickit 2 to program the microcontroller, it works fine but when i use it to debug, it enters the debug session and when it enters the delay loop, it stays stuck there. What can be wrong?

My car doesn't start. Can you tell me what is wrong?

You are going to have to leave WAY more details than that. What have you done to resolve this issue? Does it run in "production" mode? How do you know it is "stuck in a loop"? Where is your code? What happens when you comment out the code? Are you stepping into the delay loop? Because that might be the problem. What happens if you just run?

#### goutham1995

Joined Feb 18, 2018
94
My car doesn't start. Can you tell me what is wrong?

You are going to have to leave WAY more details than that. What have you done to resolve this issue? Does it run in "production" mode? How do you know it is "stuck in a loop"? Where is your code? What happens when you comment out the code? Are you stepping into the delay loop? Because that might be the problem. What happens if you just run?
if i just program it, it runs properly. Its a simple program which just toggles led pins. I am able to see the LED blink. but if i want to be able to debug the program, its gets stuck in the delay loop and the led does not toggle. To resolve it, i tried to see if the issue is because of my ide..i used a software debugger but that seems to be working fine..if i use pickit 2 to debug, there lies the issue.

My code is here -

#define _XTAL_FREQ 32000000 //Device Crystal Frequency
__CONFIG(0x1F7A); //Configuration Bits refer Datasheet

void delay(int count)
{
for(int i=0;i<=count;count++)
{
}
}

void main()
{
ADCON1=0x06; //Changes PORT A to Digital
CMCON =0x07; //Disable Analog Comparator
TRISA=0x00; //Configure PORT A as Output
TRISB=0x00; //Configure PORT B as Output
TRISC=0x00; //Configure PORT C as Output
TRISD=0x00; //Configure PORT D as Output
TRISE=0x00; //Configure PORT E as Output

PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;

while(1)
{
PORTA=~PORTA;
PORTB=~PORTB;
PORTC=~PORTC; //Toggle the All PORTS
PORTD=~PORTD;
PORTE=~PORTE;
// __delay_ms(500); //Delay 500 milliseconds
delay(100);
}
}

#### goutham1995

Joined Feb 18, 2018
94
My car doesn't start. Can you tell me what is wrong?

You are going to have to leave WAY more details than that. What have you done to resolve this issue? Does it run in "production" mode? How do you know it is "stuck in a loop"? Where is your code? What happens when you comment out the code? Are you stepping into the delay loop? Because that might be the problem. What happens if you just run?
if i just program it, it runs properly. Its a simple program which just toggles led pins. I am able to see the LED blink. but if i want to be able to debug the program, its gets stuck in the delay loop and the led does not toggle. To resolve it, i tried to see if the issue is because of my ide..i used a software debugger but that seems to be working fine..if i use pickit 2 to debug, there lies the issue.

My code is here -

C:
#include<htc.h>                                                             //Header File

#define _XTAL_FREQ 32000000                                 //Device Crystal Frequency
__CONFIG(0x1F7A);                                                   //Configuration Bits refer Datasheet

void delay(int count)
{
for(int i=0;i<=count;count++)
{
}
}

void main()
{
ADCON1=0x06;    //Changes PORT A to Digital
CMCON =0x07;    //Disable Analog Comparator
TRISA=0x00;        //Configure PORT A as Output
TRISB=0x00;        //Configure PORT B as Output
TRISC=0x00;        //Configure PORT C as Output
TRISD=0x00;        //Configure PORT D as Output
TRISE=0x00;        //Configure PORT E as Output

PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;

while(1)
{
PORTA=~PORTA;
PORTB=~PORTB;
PORTC=~PORTC;                                               //Toggle the All PORTS
PORTD=~PORTD;
PORTE=~PORTE;
//    __delay_ms(500);                                              //Delay 500 milliseconds
delay(100);
}
}
Mod edit. Code tags

Last edited by a moderator:

#### spinnaker

Joined Oct 29, 2009
7,835
but if i want to be able to debug the program, its gets stuck in the delay loop and the led does not toggle. To resolve it, i tried to see if the issue is
This is normal behavior. Just comment out the delays when debugger or set breakpoints past the delay and run through them.

#### JohnInTX

Joined Jun 26, 2012
4,339
FWIW in the delay routine you should increment i, not count.

#### goutham1995

Joined Feb 18, 2018
94
This is normal behavior. Just comment out the delays when debugger or set breakpoints past the delay and run through them.
When I comment out the delays, I'm able to debug but the LEDs are not toggling. But the values are changing as i looked at them using the watch window. If i set the breakpoint past the delay without commenting them, then the same problem of getting stuck in the delay loop persists.

#### goutham1995

Joined Feb 18, 2018
94
FWIW in the delay routine you should increment i, not count.
Sorry, my bad. I've changed it now. But it still doesn't work

#### goutham1995

Joined Feb 18, 2018
94
Hi, thanks to all of you. I've got it working. It actually takes a lot of time to get out of the delay loop. I just waited a little longer this time. Obviously , making it count++ instead of i++ was the problem.

#### spinnaker

Joined Oct 29, 2009
7,835
Sorry didn't notice it was your own delay.

Why aren't you using HTC's delay?

Why are you still using HTC?

#### goutham1995

Joined Feb 18, 2018
94
I was thinking the problem was with the HTC's delay. So i wrote my own delay...and i just copied the code from somewhere..that's why I was using HTC

#### be80be

Joined Jul 5, 2008
2,049
The problem is the delay period the sim goes there and stays it's how it works do you think you could see the sim values if it ran and full speed
they would fly by so fast you would never see them.

the sim hits a loop that counting for even a second that could take hours to happen maybe even longer . days

I still remember 2008 I was siming some LCD code I couldn't see why it didn't work.
I left the sim on came back a day later and the value had changed.

So I built it on a 18F1220 I load the code nothing happens I left and came back maybe 4 hours later the output shows on the LCD the OSC was running at it's slowest speed.
Because a bug in compiler the oscon setting had to be a include file that was added at the start because the chip took 2 hours to get to that part if you added it in the init code.

Last edited: