0

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;


}
1
  • Do you need a delay after it RD0 is reset to 0 to ensure that the flag is picked up? Commented Jan 26, 2016 at 21:48

1 Answer 1

5

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){};

Sign up to request clarification or add additional context in comments.

1 Comment

simply while(1); is enough instead of return or while(1){};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.