PIC 18F4550 capture mode
The xor() must be inside the infinite loop. In particular inside the while (!PIR2bits.CCP2IF) loops.
In all cases if you are not using interrupts it is useless to use the capture compare for an internal function.