Search code examples
cinfinite-looppicmplab

PIC endless loop


I am trying to program PIC16F887 and having this interesting problem. I expect from LED to blink one-time and stop forever, however it starts back and never stops although watchdog is disabled. Here is the code. Thanks in advance.

I wrote this in MPLAB v8.84 and programmed using PICkit2 and Hi-Tech C compiler.

#include <htc.h>
#include <pic.h>
#define _XTAL_FREQ 800000
//__CONFIG(0x3FF5);

//functions
void INITgeneral(void);
void ledshow (void);

void main(void) {
INITgeneral();
ledshow();
return;
}

void INITgeneral(void)
{
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISD = 0;
TRISE = 0;
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;

}

void ledshow(void)
{

__delay_ms(400);
RD0 = 1;
__delay_ms(400);
RD0 = 0;


}

Solution

  • The built-in simulator is very helpful in finding issues such as this one, well worth learning about.

    Under the ‘View’ tab select ‘Disassembly Listing’. Notice that the next instruction after returning from the call to ledshow() is the instruction GOTO 0 which loads the program counter with zero, the reset vector. This is why you are endlessly executing the program.

    To stop this behavior replace return in main() with an endless loop while(1){};