PICKIT2 loses bandgap values
Page 54 of the Datasheet:



In note 3 it specifies that the entire program memory including OSCCAL value is erased during programming. I must say that seems very odd behaviour, but Microchip will have their reasons I'm sure.

In note 1 it says that you must read the current value and save it somewhere prior to erasing the chip ready to reprogram it. By extension, although not specifically stated, you must then write the value back after erasing the chip.

These notes are also repeated on page 56, Section 9.2.5.1:



It also gives an example of how to calibrate the oscillator, though it is not entirely clear. As far as I can tell all the code is doing is loading some value that is stored in the program, and then saving it to the OSCCAL register. The C equivalent will be something along the lines of placing this as the first line in main():

OSCCAL = &lt;some value&gt;;


The value of course will be whatever calibration value makes the internal oscillator clock at the correct speed. However if you have erased this value you will need to either see if it is written anywhere, or try experimenting with different values until you find the right one.

If the clock frequency is not critical (i.e. you don't need accurate timing), then pick any value - say 0x80 as that is the middle value.

If you need accurate timing, you'll need to try different values and measure the clock frequency. The simplest way to get a ballpark figure is to have some code which waits for exactly 1 million instruction clock cycles, then toggles an LED. You can then time how long the LED takes to blink (it should be 1s on, 1s off), and then try different OSCCAL values systematically until it is blinking at the right speed.