📄 osccal.h
字号:
/* Name: osccal.h * Author: Christian Starkjohann * Creation Date: 2008-04-10 * Tabsize: 4 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) * This Revision: $Id: osccal.h 692 2008-11-07 15:07:40Z cs $ *//*General Description:This module contains a function which calibrates the AVR's internal RCoscillator so that the CPU runs at F_CPU (F_CPU is a macro which must bedefined when the module is compiled, best passed in the compiler commandline). The time reference is the USB frame clock of 1 kHz availableimmediately after a USB RESET condition. Timing is done by counting CPUcycles, so all interrupts must be disabled while the calibration runs. Forlow level timing measurements, usbMeasureFrameLength() is called. Thisfunction must be enabled in usbconfig.h by definingUSB_CFG_HAVE_MEASURE_FRAME_LENGTH to 1. It is also recommended to callcalibrateOscillator() from the reset hook in usbconfig.h:#ifndef __ASSEMBLER__#include <avr/interrupt.h> /* for sei() */extern void calibrateOscillator(void);#endif#define USB_RESET_HOOK(resetStarts) if(!resetStarts){cli(); calibrateOscillator(); sei();}This routine is an alternative to the continuous synchronization describedin osctune.h.Algorithm used:calibrateOscillator() first does a binary search in the OSCCAL register forthe best matching oscillator frequency. Then it does a next neighbor searchto find the value with the lowest clock rate deviation. It is guaranteed tofind the best match among neighboring values, but for version 5 oscillators(which have a discontinuous relationship between OSCCAL and frequency) abetter match might be available in another OSCCAL region.Limitations:This calibration algorithm may try OSCCAL values of up to 192 even if theoptimum value is far below 192. It may therefore exceed the allowed clockfrequency of the CPU in low voltage designs!Precision depends on the OSCCAL vs. frequency dependency of the oscillator.Typical precision for an ATMega168 (derived from the OSCCAL vs. F_RC diagramin the data sheet) should be in the range of 0.4%. Only the 12.8 MHz and16.5 MHz versions of AVR-USB (with built-in receiver PLL) can tolerate thisdeviation! All other frequency modules require at least 0.2% precision.*/#ifndef __OSCCAL_H_INCLUDED__#define __OSCCAL_H_INCLUDED__void calibrateOscillator(void);/* This function calibrates the RC oscillator so that the CPU runs at F_CPU. * It MUST be called immediately after the end of a USB RESET condition! * Disable all interrupts during the call! * It is recommended that you store the resulting value in EEPROM so that a * good guess value is available after the next reset. */#endif /* __OSCCAL_H_INCLUDED__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -