Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 141
5.3.9.1
OSCILLATOR CALIBRATION CONSTANTS
Some Baseline and Mid-range devices come with an oscillator calibration constant 
which is pre-programmed into the device’s program memory. This constant can be read 
from program memory and written to the OSCCAL register to calibrate the internal RC 
oscillator.
On some Baseline PIC devices, the calibration constant is stored as a MOVLW instruc-
tion at the top of program memory, e.g. the PIC10F509 device. On Reset, the program 
counter is made to point to this instruction and it is executed first before the program 
counter wraps around to 0x0000, which is the effective Reset vector for the device. The 
default runtime startup routine (see Section 5.10.1 “Runtime Startup Code”) will 
automatically include code to load the OSCCAL register with the value contained in 
WREG after Reset on such devices. No other code is required.
For other chips, such as the 12F629 device, the oscillator constant is also stored at the 
top of program memory, but as a RETLW instruction. The compiler’s startup code will 
automatically generate code to retrieve this value and do the configuration.
For other chips, such as PIC12F629 device, the oscillator constant is also stored at the 
top of program memory, but as a RETLW instruction. The compiler’s startup code will 
automatically generate code to retrieve this value and perform the configuration.
At runtime, the calibration value stored as a RETLW instruction may be read using the 
‘function’ __osccal_val(),as a label is assigned the RETLW instruction address. A 
prototype for the function is provided in <xc.h>. For example:
calVal = __osccal_val();
Loading of the calibration value can be turned off via the osccal suboption to the 
--RUNTIME
At runtime, this calibration value may be read using the macro 
_READ_OSCCAL_DATA()
. To be able to use this macro, make sure that <xc.h> is 
included into the relevant modules of your program. This macro returns the calibration 
constant which can then be stored into the OSCCAL register, as follows:
OSCCAL = _READ_OSCCAL_DATA();
Note:
The location which stores the calibration constant is never code protected 
and will be lost if you reprogram the device. Thus, if you are using a win-
dowed or Flash device, the calibration constant must be saved from the last 
ROM location before it is erased. The constant must then be reprogrammed 
at the same location along with the new program and data.
If you are using an in-circuit emulator (ICE), the location used by the cali-
bration RETLW instruction may not be programmed. Calling the 
_READ_OSCCAL_DATA()
 macro will not work and will almost certainly not 
return correctly. If you wish to test code that includes this macro on an ICE, 
you will have to program a RETLW instruction at the appropriate location in 
program memory. Remember to remove this instruction when programming 
the actual part so you do not destroy the calibration value.