Binary Search of OSCCAL

The search is based on a binary search method, a divide-and-conquer method:

  1. 1.The OSCCAL register is loaded with the initial value, which is half the maximum value of OSCCAL. The initial value of OSCCAL is defined as the initial step size.
  2. 2.The frequency of the system clock is then compared to an external reference, the calibration clock.
    1. 2.1.If the frequency is within 1% accuracy limit, go to step 5.
    2. 2.2.If the system clock is found to be too fast the OSCCAL value is reduced, and if the clock is too slow OSCCAL is increased, go to step 3.
  3. 3.The step size is assigned the value of half the previous step size.
    1. 3.1.If the step size is zero, the binary search has been unsuccessful. Go to step 4.
    2. 3.2.If the step size is not zero, the step size is added to or subtracted from the current value in the OSCCAL register to increase or decrease the oscillator frequency, then go to step 2.
  4. 4.Test the four nearest neighbor-values of OSCCAL. This is done to compensate for the lack of a strictly monotonic relationship between OSCCAL and oscillator frequency.
  5. 5.If a tested OSCCAL value is within the accuracy limits, continue to store value in EEPROM, signal success and stop calibration.
  6. 6.If none of the tested OSCCAL values are within the limits (not expected), signal on MISO that the calibration has failed by driving the line low and go to stop calibration.