Definition in file calib_32kHz.c.
#include "calib_values.h"
#include "device_specific.h"
#include <inavr.h>
#include <ioavr.h>
Include dependency graph for calib_32kHz.c:

Go to the source code of this file.
Functions | |
| void | BinarySearch (unsigned int ct) |
| The binary search method. | |
| void | CalibrateInternalRc (void) |
| Calibration function. | |
| void | CalibrationInit (void) |
| Initializes the calibration. | |
| unsigned int | Counter (void) |
| The Counter function. | |
| void | main (void) |
| Program entry point. | |
| void | NeighborSearch (void) |
| The neighbor search method. | |
Variables | |
| unsigned char | bestCountDiff = 0xFF |
| The lowest difference between desired and measured counter value. | |
| unsigned char | bestCountDiff_first |
| Stores the lowest difference between desired and measured counter value for the first search. | |
| unsigned char | bestOSCCAL |
| The OSCCAL value corresponding to the bestCountDiff. | |
| unsigned char | bestOSCCAL_first |
| Stores the OSCCAL value corresponding to the bestCountDiff for the first search. | |
| unsigned int | calibration |
| Calibration status. | |
| unsigned char | calStep |
| The binary search step size. | |
| unsigned int | countVal |
| The desired counter value. | |
| unsigned char | neighborsSearched |
| Holds the number of neighbors searched. | |
| signed char | sign |
| Stores the direction of the binary step (-1 or 1). | |
|
|
The binary search method. This function uses the binary search method to find the correct OSSCAL value. Definition at line 200 of file calib_32kHz.c. References calibration, calStep, countVal, FINISHED, NOP, and sign. Referenced by CalibrateInternalRc(). 00200 { 00201 00202 if (ct > countVal) // Check if count is larger than desired value 00203 { 00204 sign = -1; // Saves the direction 00205 OSCCAL -= calStep; // Decrease OSCCAL if count is too high 00206 NOP(); 00207 } 00208 else if (ct < countVal) // Opposite procedure for lower value 00209 { 00210 sign = 1; 00211 OSCCAL += calStep; 00212 NOP(); 00213 } 00214 else // Perfect match, OSCCAL stays unchanged 00215 { 00216 calibration = FINISHED; 00217 } 00218 calStep >>= 1; 00219 }
|
|
|
Calibration function. Performs the calibration according to calibration method chosen. Compares different calibration results in order to achieve optimal results. Definition at line 116 of file calib_32kHz.c. References ABS, bestCountDiff, bestOSCCAL, BinarySearch(), calibration, calStep, Counter(), countVal, FINISHED, NeighborSearch(), neighborsSearched, NOP, and RUNNING. Referenced by main(). 00116 { 00117 unsigned int count; 00118 00119 #ifdef CALIBRATION_METHOD_SIMPLE // Simple search method 00120 unsigned char cycles = 0x80; 00121 00122 do{ 00123 count = Counter(); 00124 if (count > countVal) 00125 OSCCAL--; // If count is more than count value corresponding to the given frequency: 00126 NOP(); // - decrease speed 00127 if (count < countVal) 00128 OSCCAL++; 00129 NOP(); // If count is less: - increase speed 00130 if (count == countVal) 00131 cycles=1; 00132 } while(--cycles); // Calibrate using 128(0x80) calibration cycles 00133 00134 #else // Binary search with or without neighbor search 00135 unsigned char countDiff; 00136 unsigned char neighborSearchStatus = FINISHED; 00137 00138 while(calibration == RUNNING){ 00139 count = Counter(); // Counter returns the count value after external ticks on XTAL 00140 if (calStep != 0) 00141 { 00142 BinarySearch(count); // Do binary search until stepsize is zero 00143 } 00144 else 00145 { 00146 if(neighborSearchStatus == RUNNING) 00147 { 00148 countDiff = ABS((signed int)count-(signed int)countVal); 00149 if (countDiff < bestCountDiff) // Store OSCCAL if higher accuracy is achieved 00150 { 00151 bestCountDiff = countDiff; 00152 bestOSCCAL = OSCCAL; 00153 } 00154 NeighborSearch(); // Do neighbor search 00155 } 00156 else // Prepare and start neighbor search 00157 { 00158 #ifdef CALIBRATION_METHOD_BINARY_WITHOUT_NEIGHBOR // No neighbor search if deselected 00159 calibration = FINISHED; 00160 countDiff = ABS((signed int)count-(signed int)countVal); 00161 bestCountDiff = countDiff; 00162 bestOSCCAL = OSCCAL; 00163 #else 00164 neighborSearchStatus = RUNNING; // Do neighbor search by default 00165 neighborsSearched = 0; 00166 countDiff = ABS((signed int)count-(signed int)countVal); 00167 bestCountDiff = countDiff; 00168 bestOSCCAL = OSCCAL; 00169 #endif 00170 } 00171 } 00172 } 00173 #endif 00174 }
Here is the call graph for this function: ![]() |
|
|
Initializes the calibration. Computes the count value needed to compare the desired internal oscillator speed with the external watch crystal, and sets up the asynchronous timer. Definition at line 101 of file calib_32kHz.c. References COMPUTE_COUNT_VALUE, DEFAULT_OSCCAL, NOP, and SETUP_ASYNC_TIMER. Referenced by main(). 00101 { 00102 00103 COMPUTE_COUNT_VALUE(); // Computes countVal for use in the calibration 00104 OSCCAL = DEFAULT_OSCCAL; 00105 NOP(); 00106 00107 SETUP_ASYNC_TIMER(); // Asynchronous timer setup 00108 }
|
|
|
The Counter function. This function increments a counter for a given ammount of ticks on on the external watch crystal. Definition at line 182 of file calib_32kHz.c. References EXTERNAL_TICKS. Referenced by CalibrateInternalRc(). 00182 { 00183 unsigned int cnt; 00184 00185 cnt = 0; // Reset counter 00186 TIMER = 0x00; // Reset async timer/counter 00187 while (ASSR & ((1<<OUTPUT_COMPARE_UPDATE_BUSY)|(1<<TIMER_UPDATE_BUSY)|(1<<ASYNC_TIMER_CONTROL_UPDATE_BUSY))); // Wait until async timer is updated (Async Status reg. busy flags). 00188 do{ // cnt++: Increment counter - the add immediate to word (ADIW) takes 2 cycles of code. 00189 cnt++; // Devices with async TCNT in I/0 space use 1 cycle reading, 2 for devices with async TCNT in extended I/O space 00190 } while (TIMER < EXTERNAL_TICKS); // CPI takes 1 cycle, BRCS takes 2 cycles, resulting in: 2+1(or 2)+1+2=6(or 7) CPU cycles 00191 return cnt; // NB! Different compilers may give different CPU cycles! 00192 } // Until 32.7KHz (XTAL FREQUENCY) * EXTERNAL TICKS
|
|
|
Program entry point. Main initializes all subsystems, prepares and starts calibration according to the calibration method chosen, and the device specific oscillator characteristics. Ends in an eternal loop. Definition at line 64 of file calib_32kHz.c. References bestCountDiff, bestCountDiff_first, bestOSCCAL, bestOSCCAL_first, CalibrateInternalRc(), CalibrationInit(), DEFAULT_OSCCAL_HIGH, NOP, and PREPARE_CALIBRATION. 00064 { 00065 CalibrationInit(); // Initiates calibration 00066 PREPARE_CALIBRATION(); // Sets initial stepsize and sets calibration state to "running" 00067 CalibrateInternalRc(); // Calibrates to selected frequency 00068 00069 #ifndef CALIBRATION_METHOD_SIMPLE // If simple search method is chosen, there is no need to do two calibrations. 00070 #ifdef TWO_RANGES // For devices with splitted OSCCAL register. 00071 if (bestCountDiff != 0x00) // Do not do a second search if perfect match 00072 { 00073 OSCCAL = DEFAULT_OSCCAL_HIGH; // Sets search range to upper part of OSCCAL 00074 NOP(); 00075 bestOSCCAL_first = bestOSCCAL; // Save OSCCAL value and count difference achieved in first calibration 00076 bestCountDiff_first = bestCountDiff; 00077 PREPARE_CALIBRATION(); // Search performed in lower OSCCAL range, perform search in upper OSCCAl range 00078 CalibrateInternalRc(); // Perform a second search in upper part of OSCCAL 00079 00080 if (bestCountDiff > bestCountDiff_first) // Check which search gave the best calibration 00081 { 00082 OSCCAL = bestOSCCAL_first; // First calibration is more accurate and OSCCAL is written accordingly 00083 NOP(); 00084 } 00085 } 00086 #endif 00087 #endif 00088 00089 for(;;) 00090 { 00091 } 00092 }
Here is the call graph for this function: ![]() |
|
|
The neighbor search method. This function uses the neighbo search method to improve binary search result. Will always be called with a binary search prior to it. Definition at line 228 of file calib_32kHz.c. References bestOSCCAL, calibration, FINISHED, neighborsSearched, NOP, and sign. Referenced by CalibrateInternalRc(). 00228 { 00229 00230 neighborsSearched++; 00231 if (neighborsSearched == 4) // Finish if 3 neighbors searched 00232 { 00233 OSCCAL = bestOSCCAL; 00234 calibration = FINISHED; 00235 } 00236 else 00237 { 00238 OSCCAL+=sign; 00239 NOP(); 00240 } 00241 }
|
|
|
The lowest difference between desired and measured counter value.
Definition at line 35 of file calib_32kHz.c. Referenced by CalibrateInternalRc(), and main(). |
|
|
Stores the lowest difference between desired and measured counter value for the first search.
Definition at line 37 of file calib_32kHz.c. Referenced by main(). |
|
|
The OSCCAL value corresponding to the bestCountDiff.
Definition at line 39 of file calib_32kHz.c. Referenced by CalibrateInternalRc(), main(), and NeighborSearch(). |
|
|
Stores the OSCCAL value corresponding to the bestCountDiff for the first search.
Definition at line 41 of file calib_32kHz.c. Referenced by main(). |
|
|
Calibration status.
Definition at line 45 of file calib_32kHz.c. Referenced by BinarySearch(), CalibrateInternalRc(), and NeighborSearch(). |
|
|
The binary search step size.
Definition at line 33 of file calib_32kHz.c. Referenced by BinarySearch(), and CalibrateInternalRc(). |
|
|
The desired counter value.
Definition at line 43 of file calib_32kHz.c. Referenced by BinarySearch(), and CalibrateInternalRc(). |
|
|
Holds the number of neighbors searched.
Definition at line 31 of file calib_32kHz.c. Referenced by CalibrateInternalRc(), and NeighborSearch(). |
|
|
Stores the direction of the binary step (-1 or 1).
Definition at line 47 of file calib_32kHz.c. Referenced by BinarySearch(), and NeighborSearch(). |
1.4.5