Electronic – OSCALL byte, what is this

microcontrollerpic

I recently started playing around with micro-controllers, and with a pc interface and it's software, when reading a program from a micro controller i got a field that said OSCALL byte and seems to give me a different value everytime, my question is, what is this byte? What does it stand for?

From the name i would guess something like OSScillator CALLibration, although both the S and L are doubled, this is what makes more sense to me…

Best Regards

Best Answer

Your related question says that you are using a 12F629, so I will refer to this datasheet throughout.

Many modern microcontrollers have inbuilt oscillators. These are fairly accurate but the exact frequency will vary due to the manufacturing process. Because of this most of these microcontrollers have some way of "tuning" or calibrating the oscillator whilst it is running.

With the PIC 12F (and other similar PIC families) there is a register called OSCCAL. You can read about it in section 9.2.5.1 "Calibrating the Internal Oscillator" on page 56 of the datasheet I linked above.

The datasheet says:

A calibration instruction is programmed into the last location of program memory. This instruction is a RETLW XX, where the literal is the calibration value. The literal is placed in the OSCCAL register to set the calibration of the internal oscillator.

This value can be calculated and set by Microchip's PICKit2 and PICKit3 programmers and probably by some of the better compatible tools.

Example 9-1 on page 56 shows how this is loaded into the OSCCAL register, which is volatile and therefore needs to be set at each powerup.

BSF STATUS, RP0 ; Bank 1
CALL 3FFh       ; Get the cal value
MOVWF OSCCAL    ; Calibrate
BCF STATUS, RP0 ; Bank 0

This is a simple piece of code that selects the correct bank, calls the last memory location (0x3FF) and then moves the result into the OSCCAL register. This explains why the last byte in a hex file disassembles to RETLW XX, it simply returns the calibration value.

If you want to use this value which is stored in program flash you need to include a small stub like the above code that runs each time your device undergoes a power-on reset. Some PIC C compilers will automatically include this for you on relevant devices.

You can read some more about OSCCAL at the following links: