I finally got into the debug mode after changing the fuse setting for the quartz and the main resetting circuit.
I'm using a 12M, I changed the Fosc1/0 setting from 01(XT) to 10(HS).
I have not yet done some isolation test to demonstrate whether the change of resetting circuit alone makes difference in this concern.
---------- Post added at 01:43 ---------- Previous post was at 01:35 ----------
For other guys kind information, here lists some tips for tackling this error:
1. If the target is a Baseline or Mid-Range device (PIC10F, PIC12F, PIC16F) it may require an ICD header to debug with. See Section 4.4.3 Using an ICD Header.
2. The device oscillator is not working. Check the Configuration bits and ensure the device has a working oscillator.
3. Make sure to select Debugger>Program in MPLAB IDE before trying to run or step your code.
4. If you have a PICkit 2 with a black colored push button, you may need pull-down resistors to debug reliably. See Section 4.3 Installing the Hardware and Software.
5. The target board is not powered or is not powered properly. Check the power supply.
6. The PICkit 2 VDD output pin has not been connected to the target board. Even if the target board has its own power supply, this pin must always be connected to the target VDD supply.
7. The PICkit 2 unit has become disconnected from the target board or has an inter-mittent connection on one or more pins. Check the PICkit 2 connections to the target board.
8. The incorrect Linker Script was used. The ì-i? ICD linker script must be used when debugging. (For example, 16F887i.lkr must be used instead of 16F887.lkr).
9. The target application is attempting to use resources reserved for the debug executive, causing the debug executive to become corrupted. For example, the target application is writing to a reserved File Register. See Section 4.4.2 Reserved Resources.
10. For PIC24 and dsPIC? DSC devices, the incorrect PGXn/EMUXn port is selected in the ìComm Channel Select? Configuration bits. Check that the port the PICkit 2 Debug Express is connected to is selected.
11. A configuration setting may be preventing the target from operating properly and executing code. The target will not enter Debug mode if it is unable to execute code.
12. The device is code-protected. Check Configuration bits for code protect settings.
13. An operation may be attempting in an invalid context. For example, PICkit 2 Debug Express cannot set a breakpoint if the target application is currently running.
According to my own experience, the following should be given special attention:
1. the correct circuiting of main reset, refer to page 150 of datasheet of PIC16F877A (R2 could be replaced with a diode with its (-) end connected to !MCLR);
2. the oscillator circuit and correct fuse setting (take PIC16F877A for example - for crystals of 4M or less, XT mode should be used, for 4M or above, HS should be used. Both ok for 4M.)