PIC Microchip Keeps Resetting
As Eugene Sh. correctly pointed out. The most likely culprit is the watchdog timer. If you look at the specification sheet (http://ww1.microchip.com/downloads/en/DeviceDoc/40001729A.pdf) page 99, you can see that the default timeout for the watchdog timer reset is 2s. Also, on page 48, you can see that the default configuration bits enable the watchdog timer regardless of runtime software setting.

Your issue may be solved with the addition of the line:

#pragma config WDTE = OFF 


If that does not solve your issue, you can get the source of the last reset by checking the value of the PCON and STATUS registers ( see section 5.12 Determining the Cause of a Reset in the specification sheet at page 58 ).