Can you confirm that the PIC does indeed work at 40MHz? Maybe you are having a hardware problem with the oscillator (I had something similar once using a breadboard).

Can you add a simple test, maybe blink an LED at a fixed interval?