The code execution fail at instruction while ( ptr1 != ptr2) {…}, after updating pointers ptr1 and ptr2 on interrupts and declaring the following:
extern volatile unsigned char *ptr1;
extern volatile unsigned char *ptr2;
Why does this happen?
The problem can be addressed by changing the declaration to:
extern unsigned char * volatile ptr1;
extern unsigned char * volatile ptr2;
Using this declaration, the pointer is qualified as volatile instead of the object.
For more information, please read the chapter "8.7.1 Combining Type Qualifiers and Pointers" of the "MPLAB® XC16 C Compiler User’s Guide".
Any variable used in an ISR and the main code must be qualified as volatile because the compiler would not know when the variable is modified in the ISR.