29.4.10.3 PTG Step Interrupt

After each step command is executed in Single-Step mode, the PTG step interrupt will occur, if enabled. This notifies the application of the completion of the command.

Single-Step Demonstration Program

//This code shows a basic example of using PTG Single-Step Mode.
//Program must be run in Debug Mode.
//Once the PTGSSEN bit is set, press the push button (RE9) to set PTGSTRT to execute a single command. 
//An LED (RE0) will toggle on the completion of each command.

#include <xc.h>

void PTG_populate_queue() {
	//Set up commands in the PTG Step Queue
	PTGQUE0bits.STEP0 = 0;            //NOP
	PTGQUE0bits.STEP1 = 0b10100000;   //PTGJMP(0)
}

int main (void) {
	_TRISE9 = 1;                      //RE9 input connected to push button switch
	_Bool sw_latch = 0;               //Latch button input so one press makes one step
	
	_TRISE0 = 0;                      //RE0 output connected to external indicator
	_LATE0 = 0;                       //RE0 output low initially
	
	PTG_populate_queue();             //Set up step commands in PTG queue
	PTGCONbits.ON = 1;             //Place breakpoint on this line. When execution halts, set PTGSSEN = 1 using the debugger, then continue.
	
	_PTGSTEPIE = 1;                   //Enable PTG Step Interrupt

	while(1) {
		if (!_RE9) {                  //Check if button is pressed (active low)
			if (!sw_latch) {       
				_PTGSTRT = 1;	//Execute a single Step command
				sw_latch = 1;
			}
		}
		else {
			sw_latch = 0;            //Reset latch for next button press
		}
	}
	
	return 0;
}

void __attribute__ ((__interrupt__, no_auto_psv)) _PTGSTEPInterrupt(void) {
	_LATE0 = !_LATE0;               //Toggle RE0 to show step has taken place
	_PTGSTEPIF = 0;
}