PIC16F877A latchup/glitching
The most likely culprit is the reset input is not pulled high (or low, cannot remember) with a resistor to keep noise from triggering it.  Look at the default circuits for PIC MCUs for the reset input.