Getting Started With The AVR
Getting Started With The AVR
&egal 'ote(
AVR" A)AVR*S# and S)+500 are regi,tered trade!ark, o- At!el .or$oration/ All other 0o!$an1 and $rodu0t na!e, are the $o,,e,,ion o- their re,$e0ti2e owner,/ All $rodu0t, and !aterial, di,0u,,ed in thi, tutorial are u,ed ,olel1 -or edu0ational $ur$o,e,/ Should an1 $rodu0t e !i,re$re,ented or thi, do0u!ent in an1 wa1 e in 2iolation o- trade!ark law" the 0our,e ,ta-- will e !ore than ha$$1 to !ake the a$$ro$riate 0orre0tion, and 0an e 0onta0ted at ee5453e0e/0!u/edu/ )hi, do0u!ent !a1 e -reel1 $roli-erated/
*n thi, do0u!ent all -un0tion 0all," -un0tion na!e," $ath, and te4t 0o!!and, are $re,ented in courier/ All other te4t i, in )i!e, 'ew Ro!an/
.ontent,///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////2 What i, the AVR6////////////////////////////////////////////////////////////////////////////////////////////////////////////////7 Wh1 8o * .are6///////////////////////////////////////////////////////////////////////////////////////////////////////////////////7 *nter-a0ing With the AVR////////////////////////////////////////////////////////////////////////////////////////////////////5 9a,i0 G#*: u,e////////////////////////////////////////////////////////////////////////////////////////////////////////////////5 ;AR) Setu$/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////5 )he <an1 Ste$, o- .o!$ilation//////////////////////////////////////////////////////////////////////////////////////////= )utorial 1( Si!$le )er!inal 8e!o//////////////////////////////////////////////////////////////////////////////////////> )utorial 2( SRA< 8e!o///////////////////////////////////////////////////////////////////////////////////////////////////10 :ther .a$a ilitie, o- the at!ega8//////////////////////////////////////////////////////////////////////////////////////12 Re-eren0e,//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////17
Wh1 8o * .are6
)he AVR allow, 1ou to ra$idl1 de2elo$ 0ode -or a 2er1 low-$ower" low-0o,t !i0ro$ro0e,,or with the ad2antage, o- G.. 0o!$iler ,u$$ort/ )he at!ega8 ha, a de0ent a!ount o- 0o!$utational $ower and ha, e40ellent -le4i ilit1 in the u,e o- it, *: $in,/ )he AVR -a!il1 o- $ro0e,,or, ha, a 2er1 large -ollowing on the *nternet allowing -or ra$id ad2i0e on de2elo$!ent i,,ue,/ *- that i,nBt enough" 1ou are re@uired to u,e thi, $ro0e,,or -or the $ur$o,e, o- thi, 0la,,/ #rogra!!ing the AVR )he AVR $ro0e,,or -a!il1" and in $arti0ular the at!ega8 $ro0e,,or in it, 8*# -or!at" are 2er1 ea,1 to $rogra! through it, *n S1,te! #rogra!!ing (*S#) inter-a0e/ All $rogra!!ing i, done 2ia the A)AVR*S# whi0h 0onne0t, ea,il1 to a .:< $ort on the #. and to a 10 $in header on AVR/ A, a re,ult" $rogra!!ing on the at!ega8 $ro0e,,or i, -a,t and ea,1/ 9e-ore an1 $rogra!!ing 0an take $la0e" we !u,t $ower the AVR and 0onne0t the ;AR) line, on the at!ega8 to the 0o!$uter/ )hi, i, not a, ,i!$le a, it ,ee!," howe2er/ RS272 ,ignal, go etween C12V and D12V and are in2erted (-12V i, a logi0al 1)/ A$$l1ing thi, kind o- 2oltage to the AVR i, 2er1 dangerou,/ 8ealing with thi, $ro le! re@uire, the u,e o- a 2er1 0o!!on 0hi$( the <AE272/ )he <AE272 u,e, 0a$a0itor $u!$, to $er-or! the ne0e,,ar1 le2el ,hi-ting and $ro2ide two re0ei2e and two tran,!it 0hannel, $er 0hi$/ )he AVR i, $rogra!!ed 2ia an inter-a0e etween a 0o!$uterB, ,erial $ort that goe, to $in, on the AVR/ *n thi, ,e0tion" we will ,et u$ thi, inter-a0e/ )he A)AVR*S#B, (the $rogra!!ing inter-a0e) ,erial 0onne0tor !u,t e 0onne0ted to the #. and we !u,t ,etu$ an inter-a0e etween the 10 $in 0onne0tor on the A)AVR*S# and the at!ega8/
)o ea,e thi, ,etu$" we will e,ta li,h a 0on2ention in whi0h the otto! row o- the 10 $in 0onne0tor i, de-ined a, the row 0lo,e,t to the long $art o- the ri on 0a le/ 1
10
2 G Ri
7 8
4 >
5 =
on .a le
;,e the -ollowing ta le to !ake 0onne0tion, etween the AVR $ro0e,,or and the 10 $in header/ AVR Processor Pin 1> (<:S*) G'8 (an1 ,our0e will do) 1 (RFSF)) 1G (S.+) 18 (<*S:) Programmer 10 Pin Header 1 2 7 4 5
:ne thing to note i, that 1ou !u,t atta0h a 10+H re,i,tor ( rown D la0k D orange) to a 5V $ower ,our0e and $in 1 on the AVR $ro0e,,or/ )hi, $ull-u$ re,i,tor i, ne0e,,ar1 on RFSF) ,in0e the $rogra!!er i, not ,trong enough to $ull down an1thing ee-ier and the AVR doe, not ha2e a $ull u$ re,i,tor on thi, $in internall1/ Al,o" $in 10 on the 10 $in 0onne0tor !u,t e 0onne0ted to a 5V $ower ,our0e/ 9e ,ure to do thi, ,te$ a, it i, VFRI *<#:R)A')/ )o egin $rogra!!ing" the $ro0e,,or !u,t ha2e RFSF) and S.+ $in, ,et to 0/ )hen" ,$e0i-i0 #rogra!!ing Fna le ,erial in,tru0tion, !u,t e ,ent to the <:S* $in (!ore detail, in the at!ega8 data ,heet)/ ?owe2er" thi, doe, not ha2e to e done 1 the u,er a, the A)AVR*S# take, 0are o- all o- thi, -un0tionalit1/ 'ow that weB2e ,etu$ the $rogra!!ing inter-a0e" it i, relati2el1 ea,1 to 0o!$ile and download a $rogra!/ 5ind the )er!inal8e!o $rogra! lo0ated at C:\545\demo\code\avr\ and t1$e make/ )hi, will 0reate a -ew -ile, (who,e 0reation and !eaning will e di,0u,,ed later in thi, do0u!ent)/ :- !o,t intere,t to u, i, the TerminalDemo.hex -ile/ )hi, -ile 0ontain, all the in-or!ation ne0e,,ar1 to run the $rogra!/ )o download thi, $rogra! to the AVR" t1$e in STK500.EXE -dATmega8 -e -ifTerminalDemo.hex -pf -vf
Sin0e we are Ju,t te,ting to en,ure that the $rogra!!er i, $ro$erl1 ,etu$" no a0tual out$ut will e ,een/ ?owe2er" i- 1ou re0ei2e an error !e,,age" en,ure that the green light on the A)AVR*S# i, on and ,ta le/ *- thi, i, ok" 0he0k the orientation o- 1our 0a le/
UART Setup
)hi, ,e0tion de,0ri e, how to ,et u$ the <AE272 0hi$ and 0onne0t it to the AVR to ena le ;AR) tran,!i,,ion, etween the AVR and the #./ &etB, ,etu$ the $ower line, to the 0hi$ -ir,t/ A-ter $la0ing it onto the read oard" $la0e a 5V wire to $in 1= and $la0e G'8 to $in 15 o- the <AE272/ 'e4t" o tain 4 1K5 0a$a0itor,/ .onne0t the negati2e end o- one o- the 0a$a0itor (the one with the ,tri$e) to $in 7 and the $o,iti2e end to $in 1/ With another 0a$a0itor" 0onne0t the negati2e end to $in 5 and $o,iti2e end to $in 4/ .onne0t the 7rd 0a$a0itor, negati2e end to a 5V 0olu!n and the $o,iti2e end to $in 2/ .onne0t the la,t 0a$a0itor, negati2e end to $in = and the $o,iti2e end to a ground 0olu!n/ )he,e 0onne0tion, are ,u!!ariAed in the ta le elow/ Capacitor 1K5 1K5 1K5 1K5 Negative End #in 7 D <AE272 #in 5 D <AE272 5V #in = D <AE272 Positive End #in 1 D <AE272 #in 4 D <AE272 #in 2 D <AE272 G'8
'ow itB, ti!e to 0onne0t the <AE272 to the AVR ;AR) $in header,/ #la0e a 542 $in header onto the oard/ 9e 0are-ul not to $la0e it in 0olu!n, $owered 1 another de2i0e/ <ake the -ollowing 0onne0tion, (u,e the $i0ture o- the $in header -ro! 2.1.2 to guide 1ou)/ 10 pin Header Connection #in 1 #in 7 #in 4 MAX232 Connection G'8 #in 17 (R1*') #in 14 ()1:;))
&a,tl1" we !u,t 0onne0t the <AE272 to the AVR/ )hi, in2ol2e, two ,i!$le 0onne0tion,/ MAX232 Connection #in 12 (R1:;)) #in 11 ()1*') AVR Connection #in 2 (RE8) #in 7 ()E8)
G..B, de-ault eha2ior !a,k, !an1 detail, o- the 0o!$ilation $ro0e,, -ro! the a2erage u,er/ A, a re,ult" !o,t $rogra!!er, re!ain un-a!iliar with the eha2ior o- the linker whi0h i, 0ru0ial -or 0ro,,-0o!$ilation/ )o go -ro! ,our0e to inarie," there are a 2ariet1 o- ,te$, in2ol2ed/ )he,e 0an e ,u!!ariAed a,( #re$ro0e,,ing .o!$ilation A,,e! l1 &inking : Je0t )ran,lation #re$ro0e,,ing" 0o!$ilation" and a,,e! l1 -low a, e4$e0ted/ A, i, nor!al" -or ,$eed" 1ou !a1 want to 0reate indi2idual o Je0t -ile, -or ea0h in$ut -ile/ When we get to the linking ,tage" howe2er" thing, e0o!e e40iting/ )o ,tart with" when 1ouBre $rogra!!ing the at!ega8" 1ouBre o$erating without an1 o- the ene-it, o- an :S/ )hi, !ean, that 1ou need to take 0are o- a 2ariet1 o- low-le2el ta,k, that" a, a $rogra!!er" 1ou arenBt t1$i0all1 0on0erned with/ We $ro2ide 1ou with a ,!all a,,e! l1 routine that will $er-or! the !o,t a,i0 initialiAation" ut i- 1ou need to in,tall interru$t handler," 1ou will e -or0ed to !odi-1 thi, 0ode to ,et the a$$ro$riate Ju!$ $oint, and di,tingui,h etween di--erent interru$t ,our0e,/ 5or tho,e o- 1ou who are un-a!iliar with the -un0tion, $er-or!ed 1 the linker (ld)" it re,ol2e, la el, a0ro,, a nu! er o- in$ut -ile," and then !a$, the! to 2ariou, ,eg!ent, o!e!or1/ )here are ,eg!ent, -or 0ode" read-onl1 2aria le," initialiAed glo al 2aria le,"
uninitialiAed glo al 2aria le," and a 2ariet1 o- ,ta0k,/ )o ,o!e e4tent" the,e !a$$ing, are 0ontrolled 1 the !e!or1 la1out o- the at!ega8 (i/e/ 0ode" read-onl1 2aria le," and the initial 2alue, o- initialiAed glo al, need to li2e in the $art or !e!or1 re,er2ed -or 5&AS? wherea, ,$a0e i, re,er2ed -or initialiAed glo al, to e 0o$ied into" -or uninitaliAed 2aria le," and -or ,ta0k, in the the RA</ 5inall1" on0e the linking ha, een $er-or!ed" 1ou need to 0on2ert the out$ut /el- -ile to a / he4 -ile that 0an e loaded dire0tl1 into the $ro0e,,orB, -la,h/ )hi, o$eration i, $er-or!ed 1 the o J0o$1 utilit1/ 9e0au,e o- the nu!erou, ,te$, in2ol2ed" it i, ,trongl1 re0o!!ended that 1ou take one othe $ro2ided <ake-ile, and !odi-1 that to -it 1our need,/ *- 1ou are e4$erien0ed in the dark art, o- 0reating <ake-ile," there i, a lot o- roo! -or i!$ro2e!ent in the $ro2ided -ile and the 0our,e ,ta-- would greatl1 a$$re0iate an1 u,e-ul tweak, to an ad!ittedl1 ugl1" al eit -un0tional" <ake-ile/
'e4t" letB, look at the )er!inal8e!o/0 -ile( )hi, -ile holdB, the !ain $rogra!/ Iou !a1 want to $a1 ,$e0ial attention to the,e line,( Line !ermina"#emo.c% 'inc"(de )stdio.$+ 0-LE 1ser2 ser34devopen !erm,.end5 !erm,/et5 0%2 E&p"anation Standard *: li rar1 .reate a ,trea! $ointer to e u,ed 1 2ariou, *: -un0tion,/ 5un0tion that e,ta li,he, that all *: -un0tion, ,hould u,e the ;AR) dri2er a, their $ri!iti2e in$ut out$ut -un0tion,/ Al,o a,,ign, the ,trea!/ E&p"anation )hi, ,$e0i-ie, the na!e o- the de2i0e we are u,ing/ 8o not 0hange thi,/ )he -or!at u,ed 1 the S)+500 to download the $rogra! onto the AVR/ )ARGF) ,$e0i-ie, the -ile that 0ontain, !ain/ SR. !u,t in0lude the target and an1 other . -ile 1ou wi,h to 0o!$ile/
&a,tl1" letB, look at the !ake-ile( Line Ma6e4i"e% ' MC7 name MC7 3 atmega8 ' 9(tp(t 4ormat. can :e srec5 i$e&5 :inar;% 09RMA! 3 i$e& ' !arget 4i"e name <it$o(t e&tension%. !AR/E! 3 !ermina"#emo ' List C so(rce 4i"es $ere. C dependencies are a(tomatica""; generated.% .RC 3 = !AR/E!%.c ' -4 t$ere is more t$an one so(rce 4i"e .RC >3 !ermina"#river.c
'ote that a 0o!$lete e4$lanation -or the line, a o2e 0an e -ound in the AVR re-eren0e !anual and on the AVR-&*9. we $age (htt$(LLwww/nongnu/orgLa2r-li 0Lu,er!anualLinde4/ht!l)/ 9a,i0all1" thi, $rogra! ,et, u$ the ;AR) routine, )er!MSend and )er!MGet a, the a,i0 ato!i0 -un0tion, to e u,ed 1 $rint-" $ut0" get0" et0/ *t then $rint, out N?ello WorldO onto a ter!inal ,0reen and then e0ho, whate2er 0hara0ter, 1ou enter into the ter!inal a0k to 1ou/ Sin0e there are dedi0ated *: $ort, re,er2ed -or the ;AR)" no ,$e0ial $ort 0on-iguration goe, on in thi, $rogra!/ )he onl1 0on-iguration i, 2ia fdevopen to ,etu$ the ,0reen -un0tion,/ 9a,i0 in-or!ation on the how the ;AR) work, 0an e -ound in the at!ega8 data ,heet/ 9e-ore downloading the $rogra! to the AVR" o$en )era )er!/ .on-igure )era )er! to u,e the $ort not o00u$ied 1 the A)AVR*S# and ,et it to a aud rate o- 2400" no $arit1" one ,to$ it and no -low 0ontrol/
'ow that we know how the $rogra! work," letB, te,t it out/ Re-er to the a o2e ,e0tion, a, to how to 0o!$ile the $rogra! and download it to the AVR/ :n0e the $rogra! i, downloaded" 1ou ,hould ,ee N?ello WorldO $rinted out and e a le e0ho 0hara0ter, to the ter!inal/ At thi, $oint 1ou ,hould e $re$ared -or a ,lightl1 !ore a! itiou, $roJe0t/
)he intera0tion etween 2ariou, ,etting, o- #:R)4 and 88R4 are ,hown elow( 88R it Q 1 :ut$ut ?igh :ut$ut &ow 88R it Q 0 *n$ut #ull-;$ *n$ut 5loating
#ort it Q 1 #ort it Q 0
Sa!$le 0ode to read -ro! and write to #:R). -ollow,/ Rin0lude Sio/hT int !ain(2oid) U #*'. Q 04AAV 88R. Q 04AAV LL9it $attern o- 10101010" ,etting e2er1 other it a, in$ut LL<i!i0 the $attern o- #*'. to turn on the $ull-u$ regi,ter,/
LL)wo wa1, to read data 2olatile int readV read Q #*'.V LL9e 0are-ul to read onl1 tho,e it, whi0h are in$ut, read Q in (#*'.)V LLSa!e a, a o2e LL)wo wa1, to write 1B, to all o- the out$ut $ort, #:R). Q 04AAV out (#:R)." 04AA)V return 0V W )hu," 0o!!uni0ation with the AR< 0an e -a,hioned 2ia u,age o- the a o2e 0o!!and,/ 'ote that 1our 0o!!uni0ation, $roto0ol, will !o,t likel1 ha2e to $oll 0ertain it, o- the 0ntl line,/ )hi, 0an e a00o!$li,hed through while loo$, or through ,$e0ial !a0roB, in the io!8/h/ 'ote al,o that it i, $ro a l1 in 1our e,t intere,t to write a -ew !a0ro, o- 1our own to e4$edite 0ertain re$eated 0ode and to a2oid little ug, that !a1 re,ult -ro! a00identall1 writing to a wrong $ort/ Connecting t$e .RAM 5ir,t" $la0e the SRA< 0hi$ a o2e or elow the AVR !i0ro0ontroller (-or ea,e o- wiring)/ Re-er to the SRA< data,heet a, an aid -or the re,t o- thi, ,e0tion/ )he ta le elow ,how, the re0o!!ended 0onne0tion, to 0onne0t the SRA< to AVR/ 'ote" that we are onl1 u,ing the -ir,t G addre,, it, whi0h li!it, our SRA< to e 4+ due to re,tri0ted $in andwidth on the AVR/ Al,o" we dual u,e the AVR $rogra!!er line, -or the ,a!e rea,on,/ AVR Connection 5V (V00) on read oard Ground on read oard #85 #8= #8> #.0-#.5 #82-#84 Ground on read oard #90-#9> .RAM Connection V00 V,, WF :F .F A0-A5 A=-A8 AG-A14 *:0-*:8
.o4t<are to Comm(nicate <it$ t$e .RAM We $ro2ide 1ou with ,o!e ,o-tware/ &ook it o2erP <ake and u$load the ,ra!te,t/0/ )he outlook ,hould look a, -ollow,(
Hello World Starting SRAM Tests erase completed successfully AA write/read completed successfully 55 read/write completed successfully SRAM successfully tested
Re-eren0e,
)he at!ega8 8ata Sheet( htt$(LLwww/at!el/0o!Ld1nLre,our0e,L$rodMdo0u!ent,Ldo0248=/$d)he AVR *SA Re-eren0e <anual( htt$(LLwww/at!el/0o!Ld1nLre,our0e,L$rodMdo0u!ent,Ldo0085=/$d)he <AE272 8ata Sheet( htt$(LL$d-,er2/!a4i!-i0/0o!LenLd,L<AE202F-<AE241F/$dSolderle,, 9read oard walkthrough( Solderle,, 9read oarding -or 5un and #ro-it/do0 Getting Started with the )in1AR< walkthrough( getting ,tarted with the )in1AR</do0