Since you have a WAIT state in the DEBUGIN statement that part of the code will take as many millesconds or years it takes for that "*" to arrive - simple as that.

You have lot of nice peripherals available to you, why not make use of them?
Use the UART for communication and a timer to count the pulses for you.

Also, each time you update the dutycycle using the HPWM command you'll get a small "glitch" in the waveform (due to the fact that HPWM completely reconfigures the CCP module each time). You're better off just writing to the dutycycle register(s) directly.

/Henrik.