Menu

12F675, 12F629 - using of the oscillator calibration value

Frank
2016-08-09
2016-10-10
  • Frank

    Frank - 2016-08-09

    Hi,

    I didn't found this in the GCB documentation:

    12F675, 12F629 (and similar chips) are using a calibration value for correct internal oscillator timing.
    To use this (factory saved) value you must simply add these lines to the beginning of your GCB code:

    BSF STATUS, RP0 
    call 0x3ff 
    MOVWF OSCCAL
    BCF STATUS, RP0
    

    This snippet loads the calibration value into the OSCCAL register and your PIC runs with the correct frequency! Without this snippet runs the oscillator out of tune! I checked the working of the snipping with my scope!

    Frank

     
    • William Roth

      William Roth - 2016-09-29

      This should not be necesssary when using Pickit2 or Pickit3 programmers as the firmware saves the factory calibration settings and automatically writes it to the last memory location.

      However ....all bets are off when using other programmers as some are known to erase or corruot the factory calibration value in the OSCCAL register.

      On baseline and some midrange chips (eg 12F675) if FOSC is incorrect, the Pickit2 GUI allows either manual or automaitc calibration of the OSCAL register via the tools menu. The Pickit3 supports manual only.

      With Pickit2, select: TOOLS > OSCCAL >AUTO REGENERATE to recalibrate and re-write the OSCCAL Register. I have had a few 12F510 chips where the factory calibration is off and this corrected.

      It may be a good idea (if you only have a Pickit3) when you first connect the chip, to read the chip memory, then write down tthe factory OSCCAL calibration value. Then put it on a sticker on that chip. This way you can use SET MANUALY to get back to the factory calibration value if it somehow gets corrupted or erased.

      When lookimg at the PICKIT2/3 or IPE memory pane for these chips the OSCCAL value is in the last memory location.

       

      Last edit: William Roth 2016-09-29
  • Keith

    Keith - 2016-08-25

    Hello again frank, I have used your snippet above on a number of occassions and it sure works a treat.

    I could do with a similar routine to do the same with 16F610 and 12F616 devices. Any ideas?

    Thanks
    Keith

     
  • Chris Roper

    Chris Roper - 2016-08-26

    The 16f610 works slightly differently, in place of the OSCCAL register it has an OSCTUNE Register.
    Basically they have reversed things and the 16F610 will start with the factory calibration value and you can detune it by setting the OSCTUNE register.

    I am not aware of a 12F616 but if you meant the 12F617 it is identical to the 16F610 in that it uses OSCTUNE.

    Cheers
    Chris

     
  • William Roth

    William Roth - 2016-09-30

    I was wrong about how Pickit2/3 handle the OSCCAL register on the baseline chips. I got a 12F510 and tested. The Pickit simply does not erase the MOVLW and Calibration data located at the last flash memory location.

    Regardless of the OSCCAL setting programmed and reported by the PICKIT2/3 GUI apps, the frequency is only accurate immediately after programming and with no Power ON Reset. On these chips, after a reset, the calibration is lost and the frequency becomes inaccurate.

    The OSCCAL register in these chips is volatile and therefore at POR is the OSCCON register is reset to the POR default value which is NOT a factory calibraton value. Thus the inaccuracy.

    The only way to have an accurate clock using INTOSC is to write the OSCCAL register with the calibration value at the start of code where it will load if the chip is reset

    Some compilers handle this automatically but GCB does not, The post below shows how system.h can be used to allow the compiler to automatically load the calibration data.

    William

     

    Last edit: William Roth 2016-10-01
  • William Roth

    William Roth - 2016-09-30

    Ideally we could add a few lines to system.h (initsys) that will automatically load the saved calibration value into the OSCCAL register as the first instruction.

    Since the first instruction (movlw k) is executed beginning at the last memory location (0x3FF for 12F675) there is no need to read the calibration data as it is already placed in the W register via movlw k, where k is the 7-bit calibration value.

    For example, if you read the program memory using Pickit2 and the value at location 03FF is 0C10 ... 0C represents the movlw instruction and 10 represents a calibration value of 0x10 or decimal 16.

    Place the following lines at the very top of sub initsys in the system.h file. This makes sure that the movlw k is ist the first line executed when the program is started or reset.

    #IFDEF PIC
        #IFDEF VAR(OSCCAL)
             MOVWF OSCCAL
        #ENDIF
    #ENDIF
    

    This should work for any PIC chip that has an OSCCAL register and eliminates the need to add code to the source file.

    William

    Edit: Changed code above to also work with chips that have banked registers (12F675). GCB will automatically add 'banksel' to compiled code if needed. Corrected Register name (OSCCAL)

     

    Last edit: William Roth 2016-10-10
  • William Roth

    William Roth - 2016-10-10

    Update: The system.h file has been updated to automatically load the saved calibration data to the OSCCAL register on all PIC chips that require it.

    Thanks to Frank for bringing this up.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.