ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

Using the oscillator calibration factor on devices with internal RC oscillators

When Microchip ships devices with an internal oscillator, they store an oscillator calibration factor in the last location of code space.  You can use this data to calibrate the internal oscillator to exactly 4MHz.

To calibrate manually, you first have to read the data from the new device.  This must be done before you erase the device, because erasure will destroy the stored data.

To read the calibration factor, you'll need some method of reading the code space in the device.  A device programmer like our EPIC™ Programmer is the best thing to use.  The rest of this document assumes that you have an EPIC programmer. 

Insert the new device into the socket on the programmer and select the device part number from the drop down list on the toolbar.  Click the read button on the toolbar or select Read from the program menu.

To view the code space as read from the device, select Code from the view menu.  Scroll down to the very last location in the code space window to find the calibration factor.  The value will be shown as a 4-digit hexadecimal number.  You only need the 2 rightmost digits.

In your source code, you will need to write the calibration factor value to the OSCCAL register.  A typical PICBASIC PRO statement is:

OSCCAL = $C0

Using DEFINEs to automate

If you are programming new parts that have not been erased, you can automate this process with a PICBASIC PRO define.  The compiler will insert code at the beginning of your program that reads the calibration factor and writes it to the OSCCAL register.  This method will only work with new, never-erased devices.

For devices with 1K of code space:

DEFINE OSCCAL_1K    1

For devices with 2K of code space:

DEFINE OSCCAL_2K    1

Add one of these 2 DEFINEs near the beginning of the PICBASIC PRO program to perform the setting of OSCCAL.

PICBASIC PRO will automatically load the OSCCAL value for the PIC12C5xx and 12CE51x devices , if it is available. It is unnecessary to use the above DEFINEs with these devices.

If a UV erasable device has been erased, the value cannot be read from memory. If one of these DEFINEs is used on an erased part, it will cause the program to loop endlessly. 

Calibration is only necessary when you need precise timing from you PICmicro®, as for serial communication.