8-bitMicrocontrollersApplicationNoteRev.2563B-AVR-02/06AVR54:Run-timecalibrationoftheinternalRCoscillatorFeatures•CalibrationofinternalRCoscillatorviaUART•Synchronization/calibrationtowithin+/-2%oftargetfrequencywithasinglesynch-byte.•Alternaterun-timesynchronization/calibrationtowithin+/-1%oftargetfrequencywithdoublesynch-byte.•SupportforallAVRswithtunableRCoscillator•50%dutycyclerequiredforcorrectsynchronization/calibration.•EnablesrobustUARTcommunicationwithlowcostclocksourcesinvaryingoperatingconditions.1IntroductionThisapplicationnotedescribeshowtocalibratetheinternalRCoscillatorviatheUART.Inthemethodusedaslavenodeissynchronizedtoamasternodeatthebeginningofeverymessageframe.Thisallowsaslavenodetocommunicatewithothernodesatbaudrateswithinspecifiedlimits,evenwhenrunningonalow-costclocksource,suchastheinternalRCoscillator.ThemajorityofthepresentAVRmicrocontrollersofferthepossibilitytorunfromaninternalRCoscillator.TheinternalRCoscillatorfrequencycaninmostAVRsbecalibratedatruntimetowithin+/-1%ofthefrequencyspecifiedinthedatasheetforthedevice.Thisfeatureisidealforsynchronizationpurposes,andofferssignificantcostsavingscomparedtousinganexternaloscillator.NotethatthisimplementationusesthesynchronizationsignaltoalterthefrequencyoftheinternalRCoscillator,whichagainaltersthebaudrateoftheUARTmodule.Theterms“synchronization”and“calibration”inthiscaseessentiallymeansthesame,andwillbeusedinterchangeably.Thechoiceofexpressionismerelyrelatedtotheobjective.2AVR0542563B-AVR-02/062Theoryofoperation–theinternalRCoscillatorInproductiontheinternalRCiscalibratedateither5Vor3.3V.Refertothedatasheetoftheindividualdevicesforinformationabouttheoperatingvoltageusedduringcalibration.Theaccuracyofthefactorycalibrationiswithin+/-3or+/-10%(refertothedatasheet).Ifadesign’sneedforaccuracyisbeyondwhatcanbeofferedbythestandardcalibrationinfactorybyAtmel,itispossibletoperformasecondarycalibrationoftheRCoscillator.Bydoingthisitispossibletoobtainafrequencyaccuracywithin+/-1(+/-2%forthosewithan10%accuracyfromfactorycalibration).Asecondarycalibrationcanthusbeperformedtoimproveortailortheaccuracyorfrequencyoftheoscillator.2.1ClockselectionTheAVRfusesettingscontrolthesystemclocksourcebeingused.TousetheinternalRCoscillator,thecorrespondingfusesettingmustbeselected.Anoverviewofthefusesisavailableinthedatasheets.2.2Base-frequencyThefollowingsectionsprovideanoverviewoftheinternalRCoscillatorsavailableintheAVRmicrocontrollers.SomeAVRshaveoneRCoscillator,whileothershaveupto4differentRCoscillatorstochoosefrom.Thefrequencyrangesfrom1MHzto9.6MHz.TomaketheinternalRCoscillatorsufficientlyaccurateanOscillatorCalibrationregister,OSCCAL,ispresentintheAVRIOfile.TheOSCCALregisterisonebytewide.Thepurposeofthisregisteristobeabletotunetheoscillatorfrequency.ThistuningisutilizedwhencalibratingtheRCoscillator.WhenAtmelcalibratesadevice,thecalibrationbyteisstoredintheSignatureRowofthedevice.Thecalibrationbytecanvaryfromonedevicetotheother,astheRCoscillatorfrequencyisprocessdependent.IfadevicehasmorethanoneoscillatoracalibrationbyteforeachoftheRCoscillatorsisstoredintheSignatureRow.ThedefaultRCoscillatorcalibrationbyteisinmostdevicesautomaticallyloadedfromtheSignatureRowandcopiedintotheOSCCALregisteratstart-up.Forexample,thedefaultATmega8clocksettingistheinternal1MHzRCoscillator;forthisdevicethecalibrationbytecorrespondingtothe1MHzRCoscillatorisautomaticallyloadedatstart-up.Ifthefusesarealteredsothatthe4MHzoscillatorisusedinsteadofthedefaultsetting,thecalibrationbytemustbeloadedintotheOSCCALregistermanually.Aprogrammingtoolcanbeusedtoreadthe4MHzcalibrationbytefromtheSignatureRowandstoreitinaFlashorEEPROMlocation.ThemainprogramreadsthislocationandcopiesitintoOSCCALatrun-time.2.3RCOscillatoroverviewThebasefrequencyofanoscillatorisdefinedastheunscaledoscillatorfrequency.DifferentRCoscillatorshavebeenutilizedintheAVRmicrocontrollersthroughoutthehistory.AnoverviewoftheRCoscillatorsandexampledevicesisseeninTable1-1.Thedevicelistissortedbyoscillatortype,whichisalsomoreorlessequivalenttosortingthembyreleasedate.Onlydeviceswithtunableoscillatorsarelistedinthetable.Foracompletelistofsupporteddevices,refertothe“device_specific.h”headerfileofthesourcecode.AVR05432563B-AVR-02/06Table1-1.OscillatorfrequenciesandfeaturesofdifferentinternalRCoscillator(s),withexampledevices.OscillatorversionDeviceRCoscillatorfrequency[MHz]CKDIVPRSCK1.1ATtiny121.2--1.2ATtiny151.6--2.0ATmega1631.0--3.0ATmega81.0,2.0,4.0,and8.0--3.1ATmega641.0,2.0,4.0,and8.0-XDIV(1)4.0ATmega169(2)8.0YesYes4.1ATtiny134.8and9.6YesYes4.2ATtiny23134.0and8.0YesYes5.0ATmega169P8.0YesYesNotes:1.TheprescalerregisterisinthesedevicesarenamedXDIV2.ATmega169revisionA-E,ATmega169Puseoscillatorversion5.02.3.1Version1.xoscillatorsThisversionistheearliestinternalRCforAVRthatcanbecalibrated.Itisofferedwithfrequenciesrangingfrom1.2MHzto1.6MHz.ThecalibrationbyteisstoredintheSignatureRow,butisn’tautomaticallyloadedatstart-up.TheloadingoftheOSCCALregistermustbehandledatrun-timebythefirmware.Theoscillatorfrequencyishighlydependentonoperatingv