You are on page 1of 14

Setting Up An External Crystal Clock

Source (with Fuse Bits) for AVR


A!ega !icrocontrollers
"oste# onAugust 28, 2012$y 7 Comments
%i e&eryone'
Recently' ( ca)e across the nee# to utili*e an external clock source
for )y A!ega )icrocontroller+ ( was initially un#er the i)pression
that it woul# $e a relati&ely #aunting task after rea#ing a$out the
)oun#s of $ricke# AVRs which ha# fallen into the unforgi&ing grip of
incorrectly set fuse $ytes+ ( #i#n,t fin# any tutorials geare#
specifically towar#s a##ressing this su$-ect' suita$le for $eginners+
Fortunately )y experience wasn,t $a# at all' an# the )ain reason for
that (( suspect) is that ( #i# a lot of research an# aske# e&en )ore
.uestions to un#erstan# exactly what ( was #oing' an# what ( )ay $e
up against+ !y goal for this tutorial is to teach anyone who wants to
learn exactly what each an# e&ery aspect of setting up an external
clock source is' lea&ing no stones unturne#+
(n $rief' ( nee#e# to set up an external clock source $ecause of the
li)itations of the internal one/ the li)itations $eing spee# an# accuracy+
( nee#e# )ore spee# to support V-USB (expect a tutorial on that in
the future) which re.uires a faster clock than the internal one' an#
for those who #on,t know' the USB protocol itself is infa)ous for its
ti)ing accuracy re.uire)ents+ his tutorial will $e $ase# on the
A!ega0' $ut for the )ost part the infor)ation here will $e applica$le
to any other A!ega you ha&e+ (f you #on,t ha&e the A!ega0' then
(,ll teach you how to rea# the rele&ant parts of the #atasheet so
you can easily figure out what you ha&e to #o on your own+ (f you
know your stuff' feel free to skip sections you fin# irrele&ant in
which case ( also apologi*e for crow#ing this tutorial with $asic
infor)ation+
Basic (#eas
1et us first take a little while to un#erstan# the $asic concepts an#
i#eas associate# with setting up an external clock source+ ( highly
reco))en# opening up the #atasheet for the A!ega0 (or your
)icrocontroller) for the #uration of this tutorial' an# $riefly ski))ing
the chapter on Syste) Clock an# Clock 2ptions (page 34)+ Click here
for the A!ega05s #atasheet+
6hat is a clock7
A clock is si)ply a #e&ice that keeps track of ti)e' it kin# of gi&es
you a $eat to )o&e to+ he clock on your wall counts in incre)ents of
secon#s' for exa)ple+ A )etrono)e for your instru)ent )ight gi&e you
a $eat e&ery half or full secon#+ he a)ount of ti)es a clock
ticks8cycles per secon# is calle# its fre.uency' )easure# in %ert* (%*
or cycles8secon#)+ Si)ilarly' your A!ega has a clock insi#e too' an#
its spee# #irectly relates to how )any instructions it can carry out
per secon# (on e&ery tick8cycle of the clock)+ he #efault clock spee#
that co)es shippe# with )ost AVRs is 9 !%* (9 )illion cycles per
secon#) $ecause they ha&e internal clocks which keep ti)e+ he
internal A!ega0 clock can $e set up to a )axi)u) fre.uency of 0
!%*' an# $eyon# that you nee# an external source or else you woul#
$e o&er clocking your AVR which coul# lea# to unpre#icta$le
pro$le)s+
%ow can we set a clock spee#7
6e ha&e two options: use the internal one' or use an external source+
(f you are writing co#e that #oes $asic stuff' an# you #on,t re.uire
precision ti)ing' the internal clock shoul# suffice+ (n any other case'
particularly for co))unication (i+e+ using the UAR for exa)ple' or in
)y case' USB)' ti)ing is critical+ (n that case' we nee# an alternate
)etho#' so we use things like crystals' resonators' oscillators' an#
clocks+ hey are all suita$le to pro#uce the $eat we are looking for'
at the fre.uency we are looking for' $ut the )ost co))on a)ongst
ho$$yists are crystals an# resonator (you,ll see how they are pretty
)uch the sa)e)+ 6e will $e using a crystal for this tutorial' an# it
looks like this:
o use the crystal we will also re.uire two cera)ic 33
pF capacitors (so ha&e those han#y)+ A resonator' on
the other han#' has the capacitors an# crystal $uilt
into one package' thus )aking it a little )ore co)pact+
hat,s pretty )uch the only #ifference' $ut there )ay $e su$tle
#ifferences in setting fuse $its if you are using a resonator -ust
so)ething to look out for in the #atasheet+ 2scillators re.uire an
external power source to operate' an# usually ha&e four pins+ External
clocks' so)ething a lot people #on,t ha&e ($ut wish they #i#)' can
also $e use# $y pu)ping in a s.uare wa&e at the #esire# fre.uency+
Start;up ti)e
Clocks sources usually nee# a little $it of ti)e to war) up an# start
gi&ing us a relia$le signal when the )icrocontroller is turne# on+ his is
calle# the start;up ti)e+ o play it safe' we will $e using the )axi)u)
start;up ti)e to gi&e the clock as )uch ti)e as it nee#s to get up to
spee# (no pun inten#e#)+ Actually' the )ax start;up ti)e is only a few
)illisecon#s anyway<
6hat are fuse $ytes7
his is the one concept which ( notice# catches a lot of $eginners off
guar# ()yself inclu#e#)' an# is the source of a lot of confusion an#
)ishaps+ ypically' there are only two fuse $ytes: a high one' an# a
low one+ As you shoul# hopefully know' one $yte contains 0 $its+ So
we ha&e 9= $its to set to on or off+ Each of those $its' #epen#ing on
whether they are on or off' i)pacts the critical operations of the
)icrocontroller+ he )istake )ost people )ake is )essing aroun# with
$its they #i# not inten# to change' or gi&ing $its they inten#e# to
change the wrong &alue+ his is particularly easy $ecause a $it set to
> )eans it is progra))e#' an# a $it set to 9 )eans it is
unprogra))e# kin# of counter intuiti&e' especially for those who
wear $inary wrist watches ?whistles innocently?+ But )ore on that
later+
@oing $ack to fuse $its' $asically we )o#ify a few $its in the high
$yte an# the low $yte to tell the )icrocontroller that the next ti)e
you start up' expect an external crystal gi&ing you a fre.uency of x
!%*' an# you ha&e to gi&e it y a)ount of ti)e to start up $efore you
start running the co#e insi#e of you+ hat,s pretty )uch it/ look o&er
the section calle# !e)ory "rogra))ing on page 394 -ust to get a
general i#ea of what co)prises a fuse $yte+ As you )ay notice' each
$it in each of the two $ytes has a specific na)e (i+e+ RSA(SB1'
CBSE1' SU9' etc+)+ his )akes it easier to refer to the)' an# har#er
to )ake )istakes $ecause the na)e of the $it alone gi&es you a goo#
i#ea a$out what its purpose likely is+ For exa)ple' RSA(SB1 stan#s
for reset #isa$le (you #o not want to progra) this guy' unless you
pretty )uch ne&er want to reprogra) your precious )icrocontroller
again)' CBSE1 for clock select' SU for start;up ti)e' etc+ Finally'
it,s i)portant to note that the #efault &alues for each fuse $yte are
also liste# in the #atasheet+
1et,s Ao (t<
So what #o we know so far7 6ell' we now know that we ha&e to set
so)e $its to in#icate we are using an external crystal' one greater
than a certain fre.uency' an# re.uiring a certain start;up ti)e+
1et,s go through the datasheet' an# $ring our i#eas together+ Scroll
o&er to the chapter title# Syste) Clock an# Clock 2ptions on page 34
(or whiche&er page it is for your )icrocontroller,s #atasheet)+ So we
ha&e C $its to change in or#er to get e&erything working the way we
want: CB2"' CBSE1D++> (that )eans CBSE1D' CBSE13' CBSE19'
CBSE1>)' an# SU9++>+ 6e will a##ress each $it $elow' with the
associate# ta$les to refer to fro) the #atasheet+ For con&enience (
ha&e also a##e# the ta$les an# figures to the post' $ut they are all
copie# #irectly fro) the #atasheet+
But first' let,s take a .uick peek at Figure 9>:

As you can see' the clock )ultiplexer is gi&en an input fro) one of
the )any clock sources+ 6e will $e using the Crystal 2scillator+ hat
input goes #irectly to all the critical parts of the )icrocontroller' fro)
the C"U an# EE"R2!' to RA! an# your @"(2 pins+ he clock really is
at the heart of e&erything' gi&ing the entire syste) a $eat to
operate at+ "retty cool huh7
Choosing the &alues for the fuse $its
Eust to )ake sure we are on the sa)e page' CBSE1D++> )eans all the
CBSE1 $its fro) CBSE1> to CBSE1D+ CBSE1 stan#s for clock select'
an# tells our )icrocontroller what kin# of a clock we are using' an#
what its fre.uency is+ 1et,s look at a$le 3:

Fro) this' we see that CBSE1D++> has to $e so)ewhere $etween 9999


an# 9>9>+ So the four $its (CBSE1>' CBSE19' CBSE13' CBSE1D) are
so)ewhere in that range+ %ow #o we know what they are exactly7
hat #epen#s on our crystal,s fre.uency' a$le 3 is -ust telling us
that if we use an external crystal' the four $its will $e so)ewhere in
that range+ Right' )o&ing forwar#+

a$le F gi&es us so)e )ore #etail a$out CBSE1D++9 (note' not CBSE1>
-ust yet)' an# tells us what our options for CB2" are+ 6e will
progra) CB2" (set it to *ero) $ecause a progra))e# CB2" is
nee#e# when we are #ealing with higher fre.uency ranges' nee# a full
swing signal' or we are #ri&ing a secon# $uffer with the sa)e output
fro) the clock+ 6e )eet the first criteria (9= !%* is G 9 !%*)' so
we will set CB2" to >+ As a result' CBSE1D++9 can $e any of 9>9'
99>' or 999' it #oesn,t )atter+ 6e,ll go with setting the three
CBSE1 $its to 999 -ust for kicks+
@oo#' let,s )o&e on to the final three $its we ha&e to work on:
CBSE1>' SU9' an# SU>+

As you )ay notice' a$le 4 is the last piece of the pu**le+ (t tells us
exactly what CBSE1> an# SU9++> ha&e to $e< So let,s see' we
#eci#e# that we want the )axi)u) start;up ti)e earlier+ (n this ta$le'
that woul# correspon# to a Start;up i)e of 9=B CB an# an
A##itional Aelay of =4)s+ hus' CBSE1>' SU>' an# SU9 woul# all $e
unprogra))e# (set to 9)+
6e,re #one< o su))ari*e our $it settings' we ha&e:
CBSE1> H 9
CBSE19 H 9
CBSE13 H 9
CBSE1D H 9
SU9 H 9
SU3 H 9
CB2" H >
Setting the fuse $ytes
6e know exactly which $its we nee# to change in the fuse $ytes' so
let,s look at how the fuse $ytes are organi*e#+ %ea# o&er to the
chapter calle# !e)ory "rogra))ing on page 394+ As ( sai#' there are
two fuse $ytes' the high fuse $yte' an# the low+ he $its we nee# to
set are scattere# insi#e these two $ytes' so we nee# to fin# which
one is where' an# change it without changing any of the other ones+
ake note of the #efault &alues in the fourth colu)ns of the following
two ta$les+

a$le 0C shows the $its insi#e the high fuse $yte+ he only one we
want to change is CB2" ($it F) to >+ So the high fuse $yte shoul#
$e 99>>9>>9 in $inary' or CI in hexa#eci)al+

Si)ilarly' a$le 00 is the ta$le for the low fuse $yte+ 6e ha&e = $its
to change in here' an# in the en#' our low fuse $yte shoul# $e
99999999 in $inary' or FF in hexa#eci)al+
6e,re al)ost at the final step<
Setting Up the %ar#ware
Finally' you are at the point at which you are rea#y to $urn the fuse
$its into your )icrocontroller+ But you want to )ake sure you ha&e
the crystal connecte# to your AVR too' so follow the sche)atic
$elow:

C9 an# C3 are 33 pF cera)ic capacitors+ %a&e this setup as close


together as possi$le' #on,t ha&e wires running aroun# the $rea#$oar#
to reach the crystal an# groun#+ Jou coul# use the following setup to
try out the i)pact of the crystal along with the following co#e (skip
this little $it if you #on,t want to try out a si)ple exa)ple):

So if you want'
gi&e the a$o&e
)ini)alistic
setup an# co#e
a go' an# you
shoul# see the
1EA $link e&ery 3>> )illisecon#s (note that you shoul# not connect the
crystal -ust yet if you are going through the exa)ple' so si)ply #on,t
connect the green an# orange wires for now)+ ( ignore# the pull;up on
the reset pin' #ecoupling capacitors' etc+ -ust to keep it si)ple' $ut
you shoul# ha&e that stuff (if you #on,t' this setup shoul# still work
if you #i# e&erything else right)+ Also' )ake sure you ha&e the correct
pins for your AVR )o#el+ 2ne )istake in the #iagra) is the resistor
&alue/ sorry' ( forgot to change it+ 1et )e know if you nee# help
choosing the correct resistor' $ut ( a) sure you can han#le that
Kow we will run the co))an#s in a&r#u#e to $urn the fuse $its+ 2f
course' if you #on,t use a&r#u#e' use whate&er tool you are
co)forta$le with $ecause the $ytes will $e the sa)e+
a&r#u#e ;c us$asp ;p )0 ;U lfuse:w:>$99999999:) ;U
hfuse:w:>$99>>9>>9:)
Kotice that we are using the actual $inary &alues $ecause the $its
$eing change# are &ery clear+ %owe&er' it is reco))en#e# to use hex
&alues' like this:
a&r#u#e ;c us$asp ;p )0 ;U lfuse:w:>xff:) ;U hfuse:w:>xCI:)
Eust to )ake it clear' you woul#n,t $e using ;c us$asp an#8or ;p )0
if you are not using the USBasp progra))er an#8or the A!ega0+
(f you followe# the a$o&e exa)ple' go ahea# an# connect the wires to
the LA19 an# LA13 pins on your AVR (the green an# orange ones
in the #iagra))+ (f you #i# e&erything right an# use# the exact co#e as
the one paste# a$o&e' you will now notice the 1EA is not flashing+
6ell' that,s $ecause the FMC"U &alue in your co#e is wrong+ 6ell' it
isn,t wrong' $ut your FMC"U &alue of 9>>>>>> now )eans 989=th of
a real secon# is actually one secon# to the AVR+ So your #elay is no
longer 3>> )illisecon#s' $ut 989=th of 3>> )illisecon#s+ he #elay
results in a Nfra)e rateO of 0> flashes per secon#< hat,s too fast
for the hu)an eye to co)forta$ly process+
So to fix this' we ha&e to tell the AVR that $ecause we change# the
clock spee# to 9= !%*' one secon# actually is now )a#e up 9= )illion
cycles' not 9 )illion+ All we ha&e to #o is change FMC"U to
9=>>>>>>U1' an# we,re #one< Jour 1EA shoul# $link e&er 3>>
)illisecon#s' -ust like it was $efore+
Conclusion
hat was fun< ( hope you learne# so)ething to#ay' an# ( woul# like to
sincerely apologi*e to anyone who foun# this tutorial fille# with too
)uch $asic infor)ation+ 2ne last note: if you woul# like to return your
fuse $it settings to the way they were fro) the factory' -ust run the
sa)e process again $ut use the #efault $it &alues foun# in a$le 0C
an# 00 instea# of the ones we ca)e up with+
( woul# lo&e to rea# your co))ents an# answer your
.uestions as well' so #rop )e a line $elow+ Beep it cool e&eryone
References
( learne# e&erything ( wrote in this tutorial fro) the #atasheet' an#
)y frien#s on the Electrcal Engneerng Stac!E"change' AV# $rea!s' an#
%ad&ada+ A $ig thank you to e&eryone who helpe# )e<
Configuring a new microcontroller
A virgin
microcontroller
controller is
configured at
1MHz internal
RC oscillator with longest start-up and the JTAG pre-enaled! "o the fuse
#tes are as follows$

To set the microcontroller for high e%ternal fre&uenc# with longest start-up and
JTAG disaled' the fuse settings are changed as following!


GG 1akukan write #isetting setting fuse
Refer to '(A) dsa*lng n AV# to learn the read(write operation of fuse #tes
using A)R*+*,!

You might also like