30.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 (RF1) to set PTGSTRT to execute a single command. 
//An LED (RC8) 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) {
    _ANSELF1 = 0;
    _TRISF1 = 1; 				//RF1 input connected to push button switch
    _Bool sw_latch = 0; 		     //Latch button input so one press makes one step

    _TRISC8 = 0; 				//RC8 output connected to external indicator
    _LATC8 = 0; 				 //RC8 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 (!_RF1) { 			 //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__)) _PTGSTEPInterrupt(void) {
    _LATC8 = !_LATC8; 			//Toggle RC8 to show step has taken place
    _PTGSTEPIF = 0;
}