Practical PIC Projects



Saving the OSCCAL value
for PIC 12F629 & 12F675




Update:  The Microchip PICKit2 programmer with V2.61 application software will calculate and reprogram an erased OSCCAL in a single operation.  Therefore I would now recommend you get a PICkit2 programmer and then you'll never need to worry about the OSCCAL word getting erased ever again. 

If you have lost or erased the OSCCAL setting and you don't have a PICkit2 programmer available follow this link to the OSCCAL Recalibration project   This will allow you to calculate the correct value to program back into the PIC.

The PIC 12F629 and 12F675 have a factory set oscillator calibration value written to the last program memory location during manufacture.  When programming the PIC the programmer software should read this value out, merge it with the program data being written to the PIC, and then write it back.

However, since this calibration value is in the normal program memory area of the PIC it is quite possible to erase it.  Once you've done this, working out what the value should be is not easy and without it the internal 4Mhz oscillator will not run at the correct frequency.  Worse still, any application code that expects to find the calibration value will most likely crash. 

Therefore it is important to know the correct value for the internal oscillator calibration setting.  This tip shows you how to make sure you always know what the correct oscillator calibration value is so you can recover it and write it back should you accidentally erase it.

  1. Take your new PIC 12F629 or 12F675, put it into you programmer and READ the program data.

  2. Find the last program memory location at address 0x3FF

  3. Note down the hex value at this address. It should be in the form 34xx, like the red circled entry in the screen dump below.

  1. The 34 is a RETLW instruction, the bit we're interested in is the calibration value. In the example above it is 8C but it will vary from one device to another which is why it is important to know the actual value in each PIC.

  2. Make a note of the calibration value and now convert it to a binary number.
    In this example 8C is 10001100 in binary.

  3. Now conveniently for this, the PIC has 8 pins and the binary value we just converted has 8 bits.

  4. Get a permanent marker pen and then, working clockwise round the pins of the PIC, mark each pin on the PIC where a '1' occurs in the binary calibration value.

Now if you ever erase the calibration value, all you have to do is take the binary value marked on the pins, prefix it with 34 and write it back to address 0x3FF - Easy!