Anda di halaman 1dari 23

SJSU ME106

AVR Studio Simulator Introduction and Exercises LEARNING OBJECTIVES


get a brief introduction to most of the major debugging features AVR Studio read through a refresher on C programming and an introduction to !" on AVR microcontro##ers gain an appreciation of $hat the AVR Studio simu#ator is and $hat it%s good for #earn ho$ to use the AVR Studio user interface to contro# program e&ecution and to simu#ate !"

COMPONENTS RE !IRE"
a 'indo$s (C )or a Mac running VM$are *usion+ or 'ine on ,inu&do$n#oaded AVR Studio insta##ation pac.age+ /AVR Studio 0112 )bui#d 620-/ from http3!!$$$1atme#1com!d4n!products!too#s5card1asp6too#5id7898: do$n#oaded AVR Studio ser;ice pac.+ /AVR Studio 0112 S(1 )bui#d 6<8-/+ a#so from http3!!$$$1atme#1com!d4n!products!too#s5card1asp6too#5id7898: do$n#oaded AVR C compi#er too#chain+ http3!!sourceforge1net!projects!$ina;r!fi#es!'inAVR!80100110!'inAVR=80100110= insta##1e&e!do$n#oad

PREPARATION
*or both a C=#anguage refresher as $e## as an introduction to the basics of programming for the Atme# AVR A>mega microcontro##ers+ it is high#4 recommended that 4ou ta.e the time to go through the tutoria# at http3!!$$$81tech1purdue1edu!ecet!courses!referencemateria#!atme#!1 >his document pro;ides a good foundation for a#most a## of the #aborator4 e&periments that 4ou%## be doing this semester+ so no$%s the time to start dea#ing $ith it1

INTRO"!CTION
t is often be#ie;ed that $ithout target hard$are it is difficu#t+ if not impossib#e+ to de;e#op and test soft$are for a microcontro##er project1 >his is often not the case as man4 of the microcontro##er manufacturers )or their partners- offer soft$are simu#ators that e&ist for just this purpose1 ?ot on#4 does Atme#%s free @E ) ntegrated @e;e#opment En;ironment-+ AVR Studio+ pro;ide the frame$or. for compi#ing programs and do$n#oading them to the microcontro##er+ but it a#so comes $ith the abi#it4 to simu#ate programs for most of their AVR microcontro##ers1 >his simu#ator has the abi#it4 to not on#4 e&ecute AVR instructions but a#so to simu#ate #imited digita# !" )input!output-1 So it%s not the case that if 4ou don%t ha;e an S>A:00 )the hard$are de;e#opment board that 4ou%## be using this semester- in front of 4ou that 4ou can%t be $riting and testing soft$are for home$or.+ #abs or fina# projects1 AVR Studio shou#d fee# re#ati;e#4 simi#ar to other @Es that 4ou ma4 ha;e used in 4our introduction to programming c#asses+ #i.e Microsoft Studio E&press C+ Ec#ipse+ or ?etBeans1 @Es a## #oo. prett4 Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 much the same at 80+000 feet1 >he4 a## gi;e 4ou an eas4 $a4 to edit fi#es+ to describe and bui#d projects+ and to e&ecute and )usua##4- debug them1 n most respects+ once 4ou%;e seen one @E 4ou%;e seen them a##1 >he tric. is .no$ing $here a## of the options are hiding for the things that 4ou need to set+ ho$ c#ear#4 the4%re organiCed+ and ho$ $e## the4 perform for $hat%s most important to 4ou1 "ne of the best things that AVR Studio )and 'inAVR+ $hich actua##4 pro;ides the 'indo$s ;ersion of the compi#er too#s- has going for it is that it%s free and that it performs prett4 $e## D it%s reasonab#4 fast and doesn%t seem to crash ;er4 often1 A#though 4ou $on%t be using one this semester )un#ess 4ou spring E100=8:0 for one 4ourse#f- AVR Studio can a#so be used $ith a USB de;ice ca##ed an AVR J>AF programmer!debugger to debug rea# running hard$are from 4our (C using e&act#4 the same interface as this simu#ator1 >his AVR J>AF de;ice is an interface bet$een AVR Studio+ 4our (C%s USB port and a 6=pin )or 10=pin- connector on 4our target hard$are board1 t a##o$s 4ou to ;ie$ and change an4 piece of data inside the running microcontro##er+ as $e## as to contro# program e&ecution in e&act#4 the same $a4 that 4ou%## be doing in the AVR Simu#ator1 So b4 #earning ho$ to use the AVR Simu#ator+ 4ou%## a#so be #earning ho$ in= circuit debugging $or.s on rea# hard$are shou#d 4ou e;er do that in the future1 >he $a4 that AVR Studio )and most @Es- is organiCed is that one $or.s on projects1 A project is a co##ection of fi#es+ their interre#ationships+ and configuration settings necessar4 to bui#d source+ header fi#es and #ibraries together into a fina# unit1 >his fina# unit is usua##4 a program to be simu#ated or do$n#oaded to a chip for e&ecution1 A project is composed of a #ist of fi#es to be compi#ed+ the compi#er settings+ the configuration of the chip being de;e#oped for+ as $e## as the #a4out and contents of the ;arious $indo$s on the screen+ etc1 Just as changes to fi#es need to be sa;ed to dis.+ an4 changes to a project a#so need to be sa;ed+ other$ise a## of this )meta-information is #ost and $i## ha;e to be entered again1 So don%t forget to sa;e project settings as $e## as 4our source fi#es )assuming 4ou don%t rea##4 $ant to discard project settings- $hen e&iting AVR Studio1 "n shared #ab computers it is additiona##4 he#pfu# to gi;e 4our project a more uniGue name than H,ab8I+ #i.e H,ab8*riI+ so that 4ou can more easi#4 identif4 it1 >hat $a4 4ou can differentiate 4our project from others% /,ab8/1 >here%s nothing funnier than $atching a #ab group ma.e change after change to their code+ recompi#e and re=run it o;er=and=o;er again+ and a#$a4s ha;e it beha;e e&act#4 the same $a41 t%s because the4 .eep do$n#oading someone e#se%s o#d #ab code $ith the same name to their chip1 (rice#essJ 3=@on%t forget to cop4 4our entire project director4 off to a f#ash stic. or emai# them off the machine $hen 4ou%re done1 t%s not rea##4 recommended to $or. right on the f#ash dri;e because the4%re considerab#4 s#o$er than the interna# hard dis.1 >he #ab machines do get periodica##4 $iped=c#ean+ so $hat%s there one $ee. ma4 not be there the ne&t1 About the simu#ator in particu#ar+ it shou#d be fair#4 ob;ious that it%s not possib#e to direct#4 hoo.=up hard$are #i.e s$itches+ ,E@s+ or motors and the #i.e as there%s no ph4sica# hoo.=ups to the simu#ator1 Ko$e;er+ one is ab#e to #oo. at a representation of the digita# portion of the );irtua#- ph4sica# interface to the chip $ith the simu#ator1 *or e&amp#e+ 4ou can #oo. at a ph4sica# output #ine on the contro##er to see that an ,E@ $ou#d!$ou#dn%t be #it if one $ere connected1 Simi#ar#4 for input+ one can direct the simu#ator to beha;e as though a gi;en digita# pin $ere high!#o$ as if a s$itch $ere current#4 Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 open!c#osed1 Microcontro##er programs produce outputs dependent on the program%s current state and inputs1 A simu#ated stream of inputs can be pro;ided for e;er4 genera#=purpose digita# input pin on the microcontro##er being simu#ated1 >his is done b4 pro;iding $hat is ca##ed a stimu#us fi#e+ $hich is rea##4 just a time=#ine containing the state of some or a## of the inputs at e;er4 point in time1 >his feature $i## be presented in a #ater section1

PROCE"!RES
Installin# and Runnin# *irst+ do$n#oad and insta## the soft$are )as administrator- in the order #isted in the COMPONENTS section abo;e onto 4our (C1 Reboot the (C after$ard if 4ou%re prompted to do so for the insta##ation to comp#ete proper#41 "nce 4ou ha;e the soft$are #oaded on 4our machine+ 4ou shou#d be ab#e to go ahead and run AVR Studio1 f it didn%t create a des.top icon $hen it $as insta##ed+ 4ou%## just ha;e to rough it and find it in the Windows Start menu1 Creatin# a Pro$ect

"nce AVR Studio is running 4ou shou#d be prompted for a project to $or. $ith+ either b4 creating a Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 ne$ one+ or b4 using an e&isting project1 n this case+ c#ic. Ne% Pro$ect1

Se#ect Project type AVR GCC+ enter Project name simcounter+ se#ect both Create initial file and Create folder and se#ect )and rememberJ- a director4 in $hich to put the ne$ simcounter project director4+ then c#ic. Next1

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

Se#ect Debug Platform AVR Simulator+ and Device ATme#a&'+ then c#ic. (inis)1

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

Lou shou#d no$ see the defau#t $indo$ #a4out in AVR Studio1 Because 4ou chec.ed Create initial file $hen creating the project+ AVR Studio has created a b#an. simcounter.c fi#e and pre=opened this fi#e for 4ou1 f 4ou c#ic. on the e&pand=group icon )the (#us icon- in front of Source Files in the #eft= hand $indo$+ 4ou%## find that simcounter.c has been added to the simcounter project1 f 4ou needed to inc#ude an4 additiona# fi#es source fi#es for 4our project+ 4ou $ou#d right=c#ic. on Source Files and choose the appropriate method to do this1 Lou $on%t need to for this e&amp#e+ but 4ou $i## for #abs #ater in the semester1 At this point+ cut=and=paste the contents of simcounter.c from the fina# section of this document into the b#an. $indo$ created for 4our project%s simcounter.c1 'hi#e 4ou%re at it+ sa;e the contents of this fi#e b4 doing a File->Save )or t4ping CTRL-S+ or pressing the f#opp4 dis. icon-1 f 4ou had a cop4 of simcounter1c a#read4 on 4our computer+ 4ou cou#d a#so ha;e used Add Existing File and pointed it to the fi#e%s #ocation to add it to the project%s source fi#es1

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

Create a ne$ fi#e ca##ed simcounter-PORTA.sti b4 right=c#ic.ing on t!er Files in the #eft=hand $indo$ and se#ecting Create "ew File1 Cut=and=paste the contents of this fi#e from the simcounter-PORTA.sti section at the bottom of this document into the $indo$ that $as created for this fi#e+ and simi#ar#4 sa;e it to dis.1 Con*i#urin# and Buildin# Pro$ect

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

n Project#$Configuration ptions#$%eneral+ set Fre&uency to 8000000 and ptimi'ation to -O0 (note that this is capita#=oh Cero-1 t shou#d be noted that this optimiCation setting is usua##4 not the most popu#ar for generating code to be do$n#oaded to a rea# microcontro##er+ but it seems to be a safe choice for code to be simu#ated1 n the future+ once 4ou%re read4 to start programming 4our microconto##ers in the #ab+ 4ou%## often be directed to change this setting -O2 as 4ou%## see in subseGuent $ee.s1 Shou#d 4ou need to re;ert to simu#ating code again generating code for a rea# processor+ don%t forget to reset this option bac. to -O0. f 4ou e;er start seeing strange beha;ior $hen stepping e&ecution through simu#ated programs+ it%s probab#e that 4ou $eren%t using -O0 for the code to be simu#ated1 'hat usua##4 happens is that $hi#e stepping through 4our program+ the cursor doesn%t accurate#4 ad;ance from #ine to #ine correct#41 After changing this optimiCation setting to -O0+ do a (uild#$)ebuild All+ and 4ou shou#d be a## set1 (#ease note that doing a )ebuild All rather than just a (uild is always a good idea after 4ou ha;e changed an4 project configuration options that ha;e to do $ith compi#ation1 n fact+ it%s a#$a4s safe)stto do a )ebuild All rather than just a (uild D it%s just that it ma4 ta.e some$hat #onger1

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

n Debug#$Select device and debug platform+ set Debug Platform to AVR Simulator and Device to Atmega16+ then c#ic. inis!1

Compi#e the simcounter project $ith (uild#$(uild )or "-1 'hen the bui#d is comp#ete+ confirm that the #ast #ine in the Bui#d $indo$ at the bottom of the screen is #uil$ succee$e$ %it! 0 &arnings... as sho$n abo;e1 f this isn%t $hat 4ou see+ someho$ 4ou boffed 4our cut=and=paste+ so go bac. and tr4 it again1 *or some reason+ there are certain configuration settings in AVR Studio that cannot be set un#ess 4ou%;e a#read4 started simu#ating a program+ and the fo##o$ing is one of them1 >o do so+ initiate the simu#ation $ith Debug#$Start Debugging1 ?o$ in Debug#$A*) Simulator ptions 4ou shou#d set Fre&uency to 8.00'()1 Fo figure11 Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 Startin# and Ste++in# t)rou#) Execution f 4ou hadn%t just done so immediate#4 abo;e+ norma##4 4ou%d start simu#ating the e&ecution of the program $ith Debug#$Start Debugging+ or b4 pressing the green right=facing triang#e

in the too#bar1 >he first thing that the simu#ator then does is to start=up and then stop before the ;er4 first simu#ated instruction is e&ecuted1 t stops here to gi;e 4ou the chance to do a## the setup 4ou need before starting the run1

>he important thing to notice at this point is that there shou#d be a #itt#e 4e##o$ right=facing arro$ pointing to the opening cur#4=brace of the function main(*1 >his arro$ indicates the next statement to be e&ecuted1

At an4 point $hen 4ou ha;e contro# of the running program )that is+ $hen it isn%t free=running at fu##= speed uninterrupted-+ 4ou can step through the program s#o$#4+ #ine=b4=#ine+ at 4our o$n pace1 >here are three $a4s to step D step into+ step over+ and step out1 Step into and step over beha;e simi#ar#4 e&cept $hen a function ca## is the ne&t statement to be e&ecuted1 Step into steps into )imagine thatJ- the function and pauses e&ecution at the beginning of the ca##ed function1 Step over e&ecutes the entire function as a sing#e statement and ad;ances to the ne&t statement after the function ca##1 Step over is a huge time=sa;er once 4ou .no$ that 4ou don%t care about stepping through that function #ine=b4=#ine1 Ko$e;er if 4ou $ant!need to see $hat%s going on in the function+ b4 a## means step into it1 Step out is $hat%s used once 4ou%re in a function and 4ou no #onger care about $hat%s going on inside the rest of it and just $ant to get bac. to the code that ca##ed the function1 Step ver comp#etes e&ecuting the function and gets 4ou bac. to $here 4ou $ere $hen 4ou ca##ed it1 Just to get the hang of sing#e=stepping+ $e%re going to start $ith Step +nto1 Lou can do this )and most of the simu#ator functions- one of three $a4s1 n decreasing order of pain+ the4 are3 in the menu bar do Debug#$Step +nto+ in the too#bar c#ic. on the Step +nto icon )the one immediate#4 to the right of the 4e##o$ arro$ in the too#bar+ or b4 pressing the (&& .e41 ,earn to .no$ and #o;e the (&& )and (&,+ Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 $hich is coming=up #ater- .e4+ as the function .e4s are b4 far the easiest ones to use :00 times in an hour1

(ress the (&& .e4 once1 t shou#d no$ point to the statement $here +uttonPresses is intia#iiCed1 ?ote again that this is the next statement to be e&ecuted1 (ress the 11 .e4 again and 4ou shou#d see the arro$ pointing to uint8,t t!isState- .re/State00100- c!ange$2

At this point+ $ou#dn%t it be coo# to .no$ that the ;a#ue of +uttonPresses is actua##4 Cero6 'e##+ 4ou can+ and there are t$o $a4s to do this1 >he first $a4 is to use 4our cursor to point to the ;ariab#e +uttonPresses an4$here in the source $indo$ and it%## pop=up a f#oating ba##oon that sa4s that the current ;a#ue is 0 )a#ong $ith some other detai#s about the ;ariab#e-1 >he second $a4 is the po$er=user $a4+ and 4ou%re a#so going to #earn that no$1 Lou can maintain a $indo$ on the screen containing an4 ;ariab#es that 4ou $ant to $atch a#ong $ith their current ;a#ues1 >his $indo$ is ca##ed the Watc! window+ and 4ou can open it b4 doing a *iew# $Watc!1 @o this and right=c#ic. the first ce## under the #abe# "ame and c#ic. Add +tem1 f 4ou enter +uttonPresses- it $i## gi;e 4ou an entr4 sho$ing the current name and ;a#ue for this ;ariab#e1 ?ot on#4 that+ this ;a#ue $i## be updated as 4ou e&ecute the program under 4our contro# )it isn%t updated as the program free=runs though-1 An a#ternate $a4 to add a ne$ ;ariab#e to be $atched is to doub#e= c#ic. in an empt4 "ame fie#d ce## and 4ou can enter a ;ariab#e name that $a41 f that $eren%t enough+ there%s third $a4 to add a ;ariab#e to the $atch $indo$ D just right=c#ic. on the ;ariab#e name an4$here the editing $indo$ and se#ect Add Watc!1 (rett4 s#ic.+ huh6

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

'hi#e 4ou are here+ use an4 of these three methods to add $atch entries for the ;ariab#es t!isState+ .re/State and c!ange$1 Lou shou#d notice at this point in program e&ecution that $e ha;e not 4et initia#iCed these three ;ariab#es1 f 4ou #oo. $here the 4e##o$ arro$ is pointing+ $e%re sti## before their initia#iCation statements1 >he4 are current#4 $hat%s .no$n as Hinitia#iCed to garbageI1 >his is programmer=spea. for a ;ariab#e that has been dec#ared but not 4et set )initia#iCed- to a .no$n ;a#ue D it contains $hate;er garbage $as there $hen that particu#ar memor4 #ocation $as #ast used1 t is a ;er4+ ;er4+ ;er4 bad thing to e;er use a ;a#ue that has not been initia#iCed1 Mu#tibi##ion do##ar sate##ites )and perhaps 4our job $ith it- can be #ost fore;er $hen this happens+ so it%s usua##4 best to tr4 to a;oid it1 So+ obser;e that these three ;ariab#es current#4 contain some random+ #i.e#4 non=Cero+ ;a#ue1 >hen go ahead an press 11 again to e&ecute their initia#iCation statements1 Mero no$ for a## of them+ prestoJ

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

Before 4ou go an4 farther 4ou%re going to #earn ho$ to ;ie$ the !" registers for the microcontro##er1 A## PORTA=re#ated registers can be ;ie$ed b4 e&panding the PORTA group )c#ic. the p#us=sign- in the right=hand +, *iew $indo$+ as $e## as for a## of the other functiona# groupings of !" registers1 Kere 4ou $i## find the current simu#ated ;a#ues for PORTA+ 33RA and P45A both there as $e## as in their o$n more=detai#ed=;ie$ $indo$ be#o$1 Fo ahead and e&pand the PORT# set a#so1 ?o$ if 4ou can%t see the fu## contents of each of these entries% co#umns+ especia##4 in the #o$er of the t$o $indo$s+ stretch 4our $indo$s out so that 4ou can see e;er4thing1 n particu#ar+ 4ou $ant to be ab#e to see a## eight sGuares in the (its co#umn for each of the three PORTA6PORT#=re#ated registers1 >hese t4pes of registers are ;er4 important as the4 are the chip%s interface to the outside $or#d1 At this point+ program e&ecution shou#d be right before the function ca## to init(*1 ,et%s demonstrate Step +nto first1 >he first time that 4ou hit 11 4ou%## find 4ourse#f entering init(*1 As 4ou hit 11 to e&ecute the fo##o$ing t$o #ines one at a time+ 33RA 0 01002 66 PORTA use$ entirel7 8or in.ut 33R# 0 01882 66 PORT# use$ entirel7 8or out.ut

Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106

Lou shou#d notice that the ;a#ues for DDRA and DDRB )a#so .no$n as (ort A!B @ata direction registerin the +, *iew $indo$ are updated1 Especia##4 note that the ho##o$ sGuares in the #o$er $indo$ are b#ac.ened for bits that contain high )1- ;a#ues1 >his representation is ;er4 usefu# for Guic.#4 ;isua#iCing $hich bits are set $ithin an 2=bit ;a#ue1 n the case of 33R#+ a## bits are high!b#ac. D assuming that 4ou ha;e the PORTB group se#ected in the upper $indo$1 Lou $i## often be using these $indo$s freGuent#4 $hen fo##o$ing 4our code%s e&ecution1 At this point 4ou shou#d be done e&ecuting the #ast statement in init(* and read4 to return1 Using 11 $i## return 4ou to the ne&t statement after the one that ca##ed this function1 Since $e%;e missed our on#4 opportunit4 to demonstrate the use of Step ut )because there%s on#4 one function ca## in our demonstration program+ and $e%re ne;er ca##ing it again-+ #et%s re=run the program from the beginning1 Lou%## do this often $hen 4ou get to a point $here 4ou%;e missed something that 4ou mind#ess#4 b#e$=o;er and need to start o;er again1 Unfortunate#4+ fe$ simu#ators are ab#e to bac.= up through a simu#ation+ so 4ou%## ha;e to start at the beginning again1

>o ha#t e&ecution and start o;er again+ press the Stop icon in the menu bar+ the b#ue sGuare )or Debug# $Stop Debugging-1 >o restart+ as before+ press the green triang#e )essentia##4+ start- to continue and ha#t at the first e&ecutab#e statement1 (ress 11 unti# 4ou get just inside init(*1 Rather than stepping through the rest of the function that 4ou no #onger care about+ 4ou can use Step ut to #ea;e this function1 As usua#+ 4ou can use the Step ut icon to do this+ Debug#$Step ut+ or the appropriate function .e41 >o demonstrate the fina# stepping contro#+ do a Debug#$Stop Debugging+ then Debug#$Start Debugging to start the program again+ and 11 unti# 4ou are just about to ca## init(* again1 ?o$ use Step ver to e&ecute init(* $ithout bothering to step into it1 Lou%## do this often once 4ou are comfortab#e $ith $hat a function does and no #onger care about cra$#ing through it again1 Stepping into functions that 4ou don%t care about is often a huge $aste of time1 E;en better+ notice that (&, is the .e4board shortcut for Step ver1 10 is e;en more usefu# than 11 in man4 circumstances1 >o be honest+ ne;er .no$ remember $hich one is $hich1 %## either use the menu or the too#bar to pic. $hich one that $ant the first time+ #oo. at the *unctionAe4 #abe# that is disp#a4ed+ then just use the *unctionAe4 from that point on1 Just trust me and use the .e4s D repetiti;e=stress injuries aren%t prett4 and e;er4 mouse c#ic. adds up+ trust Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 me1 B4 the $a4+ one thing that%s eas4 to do is to .eep on pressing 11 and then end up in a function 4ou didn%t intend to enter1 n that case+ just do a Step ut and 4ou%re bac. to $here 4ou $anted to be1 n summar4+ those are the three f#a;ors of sing#e=stepping and ho$ each of them are usefu#+ or more efficient+ depending on ho$ de#iberate 4ou need to be $hen stepping through code1 Vie%in#-c)an#in# Varia.les/ and I-O Re#isters/ State At this point 4ou shou#d ha;e just entered the %!ile #oop1 >his #oop is coded to run fore;er unti# P45A is read and has the ;a#ue P45A,953,VAL:9 )a## buttons pressed-+ at $hich time it brea.s out of the #oop1 "ther$ise+ e;er4 time through the #oop the current contents of P45A are compared to the pre;ious ;a#ue to see if an4 bits ha;e changed+ indicating that a button has changed state1 (ress 10 a coup#e doCen times and see if 4ou can figure out $hat the program is doing so far1 t shou#d be e;entua##4 become ob;ious that nothing%s going to change because+ $e##+ nothing%s changing1 C#ear#4 there aren%t an4 buttons connected our simu#ated A>mega16 on P45A6PORTA+ and unti# that happens+ 4ou%## be stuc. on#4 e&ecuting the boring part of the #oop fore;er1 Ko$e;er+ a .e4 feature of the simu#ator is that 4ou can manua##4 change ;a#ues on=the=f#4 to get the program to beha;e #i.e ph4sica# input changes ha;e occurred on the chip%s digita# inputs1 *irst+ #et%s change the ;ariab#e t!isState $hich contains a cop4 of the current contents of P45A1 "pen the Watc! $indo$ that $as introduced before if it%s no #onger around1 f the ;ariab#e t!isState is no #onger being $atched for some reason+ add it again as $as done abo;e1 Sing#e=step $ith 10 unti# 4ou get to the fo##o$ing statement3 i8 (t!isState 00 P45A,953,VAL:9* So far it%s been the case that t!isState has a#$a4s been Cero+ causing 4ou to do #itt#e in the rest of the #oop and to return to the top again1 Ko$e;er+ #et%s force a change1 Use the Watc! $indo$ to change the ;a#ue of t!isState from 0100 to 01011 Lou do this b4 doub#e=c#ic.ing on the 0100 ;a#ue and entering 0101 in its p#ace )and pressing 95T9R6R9T:R5-1 ?o$ + $ith a non=Cero ;a#ue for t!isState at this point+ pressing 10 shou#d e;entua##4 get us into the i8 b#oc. that $e%;e ne;er been in before1 >he first press of 10 shou#d get 4ou past the +rea; statement+ as usua#1 Another 10 $i## cause c!ange$ to be set to the difference bet$een the current ;a#ue and the ;a#ue from the pre;ious time through the #oop1 C!ange$ becomes 0&01 because the #o$er bit has changed since the #ast time+ $hich then gets 4ou to the inner 8or #oop1 B4 doing enough 10s and fo##o$ing a#ong carefu##4+ 4ou shou#d see that a button press is detected+ and +uttonPresses is incremented to 11 f 4ou continue 10=ing enough 4ou%## find 4ourse#f bac. in the outer #oop1 Assuming that 4ou don%t manua##4 a#ter the ;a#ue of t!isState as $e did the #ast time+ P45A $i## be read again as 0+ transferred into t!isState+ and it%## #oo. #i.e the #o$ bit has changed bac. to 0+ signif4ing that the Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 button has been re#eased1 Lou%## head into the inner 8or #oop+ nothing $i## appear as a ne$ .e4 press+ and e;entua##4 4ou%## be bac. to $here 4ou started+ e;er4thing Cero+ nothing changing+ #ooping in the outer part of the %!ile #oop fore;er1

nteresting things happened a#ong the $a4 though1 .uttonPresses is no$ set to one because $e%;e HseenI a button press1 And if 4ou #oo. in the +, *iew $indo$+ 4ou shou#d notice that the ;a#ue of PORT# has changed to ref#ect the running count of presses detected1 f 4ou c#ic. on PORT# in the upper $indo$+ 4ou%## see the detai#ed ;ie$ of the PORT#=re#ated registers in the #o$er $indo$1 Since PORT# $ou#d be connected to ,E@s in our Hrea#I hard$are+ one of them $ou#d no$ be #it=up+ just #i.e the b#ac.ened=sGuare in the #o$er=right $indo$+ in the 0&01 bit position1 Another important feature in addition to being ab#e to change ;ariab#es% ;a#ues on=the=f#4 is that 4ou can a#so change the !" registers% ;a#ues on=the=f#41 Just as 4ou manua##4 changed t!isState to modif4 the captured ;a#ue of P45A+ 4ou can modif4 P45A direct#4 in a simi#ar fashion1 Fo ahead and c#ic. on PORTA in the upper !" Vie$ $indo$1 P45A%s bits in the #o$er $indo$ shou#d a## be Cero D c#ear sGuares in the (its disp#a41 ?o$ c#ic. on one of the c#ear sGuares for P45A+ and 4ou%## see it s$itch to b#ac.1 Lou%;e turned this bit on manua##4+ and 4ou%## notice that the disp#a4ed numerica# ;a#ue changes $ith it+ high#ighted in red1 >his red high=#ighting is ;er4 he#pfu# feature a#so+ and it%s used to point out $hat ;a#ue $as most recent#4 changed on the disp#a41

Fo ahead and press on a## these sGuares to #ight them a## up except one1 >his $ou#d denote that a## buttons e&cept one are current#4 being pressed!he#d at the same time1 ?o$ if 4ou go bac. and step though the %)ile and *or #oops+ 4ou%## see these additiona# )fa.e- button presses counted+ e;entua##4 resu#ting in a .uttonPresses ;a#ue of 8+ and the binar4 representation for eight disp#a4ed on PORT#1 And just as 4ou can simu#ate button presses+ 4ou can simu#ate re#eases b4 c#earing the bits in the disp#a4 for P45A a#so1 f 4ou set a## eight P45A bits at the same time+ that%s the magic seGuence that causes the program to brea.=out of the outermost %)ile #oop and essentia##4 end the program1 A## buttons pressed resu#ts in P45A being 01 + to $hich $e%;e associated the constant #abe# P45A,953,VAL:9. So in summar4+ 4ou%;e seen in this section that 4ou%re ab#e to modif4 C=#anguage ;ariab#es on=the=f#4 in the Watc! $indo$+ and ab#e to modif4 PORT input pins on=the=f#4+ and can ;ie$ their output state in Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 the +, *iew $indo$1 Pro$ect "is+la0 Mode 1s2 "e.u##in# "is+la0 Mode f 4ou ha;en%t noticed a#read4+ AVR Studio pops bac.=and=forth bet$een t$o modes $hi#e 4ou%re using it1 As soon as 4ou hit Start Debugging+ the #eft=hand $indo$ s$itches to a Processor $indo$1 'hen 4ou hit Stop Debugging+ it re;erts to its starting )editing- state and sho$s 4ou the A*) %CC $indo$+ $hich is basica##4 a project ;ie$ and the fi#es contained therein1

>he point here being that if 4ou%re e&pecting or $anting to see the project ;ie$ in the #eft=hand $indo$ and it%s not there at the time+ 4ou can s$itch to it b4 hitting the $indo$ tab for it below the $indo$1 !sin# a Stimulus (ile *or Pla0in#3.ac4 a Se5uence o* In+uts t%s c#ear#4 ;er4 con;enient that 4ou can fa.e !" input in the simu#ator b4 manua##4 setting!c#earing bits in PORT registers+ but $hat%s e;en more con;enient is that 4ou can automate this process1 B4 specif4ing a time=#ine of $hat 4ou $ant the input pins to be+ the AVR Studio Simu#ator $i## change them for 4ou automatica##4 at specified points during the e&ecution of 4our program1 >he mechanism for doing this is b4 pro;iding a stimulus file for an4 ("R> bits that 4ou ha;e configured for input1 Lou%;e a#read4 got one of these fi#es D it%s the simcounter3PORTA2sti fi#e that 4ou cut=and=pasted at the beginning of this e&ercise1 'e%## ta.e a #oo. at this fi#e1 Lou can use the 'indo$s *i#eManager to find it+ but 4ou can a#so get to it in the t!er Files section of the project disp#a4 $indo$ )A*) %CC-1 Regard#ess of 4ou ho$ $ant to #oo. at it+ get it open no$ so $e can ta#. about its contents1 ... 0001<000=00 0001>000=01 0001?000=0< 00016000=01 @ A stimu#us fi#e is essentia##4 a t$o=co#umn fi#e1 >he first co#umn is basica##4 a time designation and the second co#umn is the ;a#ue that 4ou%d #i.e the PIN register to be at that point in time1 >he #ast ;a#ue in Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 the fi#e must be <<<<<<<<+ according to the documentation1 "ne stimu#us fi#e is good for on#4 one PORT-PIN eight=bit register1 n our case a## of our input is on PORTA-PINA+ so $e on#4 need one fi#e1

>o attach a stimu#us fi#e to the simu#ator+ go to Debug#$A*) Simulator ptions#$Stimuli and -ogging1 %;e noticed that if 4ou ha;en%t started a simu#ator run )Start Debugging-+ the AVR Simu#ator "ptions option ma4 be gra4ed=out+ just #i.e that other goof4 case 4ou sa$ abo;e1 f so+ start a simu#ator session and at the first pause+ pop open that config pane#1 Choose Port PORTA+ Function Stimuli and use the H111I button to set File to the #ocation $here simcounter3PORTA2sti is #ocated1 'hen 4ou c#ic. Add Entry+ 4ou shou#d see the ne$ entr4 sho$ up in Action -ist1 *ina##4+ press OK1 Simi#ar#4+ if 4ou%d #i.e to #og the output of PORT#+ 4ou can do so b4 se#ecting ,ogging and b4 pro;iding a fi#ename for output1 Lou don%t necessari#4 ha;e to do this no$+ but mention it just to #et 4ou .no$ that e&ists1 t might e;en be possib#e get E&ce# to import both the input stimu#us fi#e and output #og fi#e and p#ot them on the timesca#e so that 4ou can compare their interre#ationship+ just #i.e a t$o=channe# osci##oscope $ou#d do1 !se o* Brea4+oints ?o$ that 4ou%;e got the stimu#us fi#e in p#ace+ #et%s start running again1 "nce again+ do a Start Debugging )Stop Debugging first if 4ou need to-. and .eep banging a$a4 on (&, unti# something interesting happens1 "A+ don%t bother an4more after 4ou%;e done this for about 80 seconds because it%s Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 going to be a $hi#e unti# something happens1 >he point here is that in rea# #ife+ most microprocessors e&ecute so Guic.#4 that e;en if 4ou $ere to press a button right a$a4 after 4our hard$are starts running 4our program+ 4ou%re rea##4 going through the $hi#e #oop hundreds of thousands of times before c!ange$ $i## e;er become non=Cero1 >his isn%t because it ta.es the microcontro##er so #ong to figure out that 4ou%;e pressed the button+ it%s because it%s so incredib#4 fast that it e&ecutes those fi;e statements just inside the %!ile #oop appro&imate#4 800+000 times per second1 So if 4ou $ere to simu#ate the program $aiting for a button press that occurred one second after the start of e&ecution+ 4ou%d ha;e to press 10 about )#itera##4- a mi##ion times before 4ou e;er sa$ that inner 8or #oop being hit1 Borrrrrrrrrr=ingJ *ortunate#4 there%s a feature )in a#most e;er4 debugger- ca##ed brea/points that te## the simu#ator )or debugger- to just go ahead and run fu##=speed unti# it hits a particu#ar p#ace in the code1 'hen a brea.point is hit+ the program stops e&ecution and passes contro# bac. 4ou to so that 4ou can either start sing#e=stepping+ #oo. at ;ariab#es+ or do $hate;er e#se 4ou $ant to do1

Since the point in the program $here things are most interesting is around $hen a button press has been Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 detected+ #et%s set a brea.point there1 'ithin the source code $indo$ there is a ;ertica# gra4 bar a#ong the #eft side that is used to se#ect actions or to ;ie$ settings on a #ine=b4=#ine basis1 'ithin this gra4 bar+ right=c#ic. on the same #ine as the statement +uttonPressesAA2 and se#ect 0oggle brea/point1 >he operati;e $ord here is toggle1 f there%s a brea.point there a#read4+ it remo;es it+ and if there%s none there it%## add one1 "nce 4ou%;e got as man4 brea.points as 4ou $ant set+ 4ou can #et the program free=run unti# it hits one of them1 n our case+ there%s e&act#4 one1 >o #et the program run $i#d unti# it hits a brea.point )or ends-+ do a Debug#$)un+ press the )un icon+ or (61

"nce 4ou hit the brea.point+ the state of the simu#ation is froCen1 Lou ha;e access to a## the current program ;ariab#es as $e## as the ("R> pins1 *rom here 4ou can often te## e&act#4 $hat set of conditions $ere that got 4ou to this brea.point1 Brea.points are an incredib#4 usefu# feature1 ?ot on#4 are the4 huge time=sa;ers+ but the4%re remar.ab#4 efficient for detecting error conditions D assuming 4ou $rite 4our code to tr4 to detect them in the first p#ace1 A## 4ou ha;e to do is to put brea.points e;er4$here $ere 4ou don%t e&pect 4our code to e;er end=up1 f 4ou e;er hit one of these brea.points+ 4ou%## be stopped e&act#4 $here 4ou $ant to be to trac. do$n the prob#em1 Lou $ant spots in 4our code to detect situations #i.e this1 Ko$e;er instead of ha;ing a brea.point for it in the debugger+ 4ou%d ha;e the program set off an a#arm+ hit a .i##=s$itch+ or perhaps e;en reset itse#f1 "ther$ise+ 4ou might just be .issing that sate##ite goodb4e wit!out even /nowing w!y1 Simulator Limitations >he most notab#e #imitation of the AVR Studio Simu#ator is that a#though it does good job of simu#ating digita# input+ ana#og input simu#ation is not imp#emented1 >his means that 4ou cannot use a stimu#us fi#e to feed ana#og input ;a#ues into an A@C pin )4ou%## get to these #ater-1 Ko$e;er+ this #imitation can be $or.ed=around b4 $riting 4our code if it%s $ritten proper#41 Lou can insert+ )or e;en better 4et+ interna##4 generate- 4our o$n fa.e A@C ;a#ues+ and 4ou can ma.e a huge amount of progress that $a41 >here are p#ent4 of other features of AVR Studio that $e ha;en%t discussed here1 >here is both reasonab#4 comp#ete bui#t=in he#p a;ai#ab#e as $e## as a prett4=$e## organiCed set of menus and pane#s to po.e around in1 >here is a#so a fair amount of customiCation that 4ou can do if 4ou don%t #i.e the Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 defau#t #a4out of the $indo$s $ithin the main frame of the app#ication1 >here%s a #ot of stuff here+ but it%s $here 4ou #i;e if 4ou%re spending a## da4 $ith it+ so 4ou might as $e## get comfortab#e $ith it1 Kopefu##4 b4 no$ 4ou%re con;inced that there%s a #ot of soft$are $or. that can be in )and beforeJ- the #ab before the circuit%s been $ired=up1 n most of the #abs this semester+ the soft$are can a#most be comp#ete b4 the time that 4ou%re read4 to f#ip the po$er=s$itch on rea# hard$are1 And there%s no reason $h4 there has to be on#4 one Hsoft$are gu4I per group D as there usua##4 is1 E;er4one can sho$ up to #ab $ith soft$are1 >his is especia##4 important because t4pica##4 rough#4 ha#f the #ab groups don%t finish ha#f the #abs during the a##otted time1 So enjo4 the AVR Studio Simu#ator1 t contains a## the fun of embedded programming $ithout an4 of that stin.4 short=circuited hard$are sme##1 And it%s a## free111 s$eetJ

OPTIONAL E7ERCISES
11 Modif4 the program so that it acts as a Hdead man%s s$itchI+ that is+ that an a#arm sounds $hene;er a button is not being pressed1 Assume that an a#arm is connected to the #o$ pin of ("R>C and that it sounds $hen 4ou dri;e that #ine%s output high1 81 Modif4 the program to detect $hen a button is pressed $hen one are more buttons are a#read4 do$n+ and sound the a#arm $hen this occurs1 N1 Use the e&isting stimu#us fi#e to dri;e the same seGuence of ,E@s f#ashing as output on PORT#1 'hen an4 button is pressed on PORTA+ the corresponding ,E@ shou#d be #it for the duration of the press+ and turned=off $hen re#eased1

S!PPLEMENTAL (ILES
simcounter2c8
/* * simcounter.c * * This program is primaril inten!e! to "e use! in the A#R Stu!io * Simulator $or !emonstration o$ !e"ugging $eatures. * This program counts "utton presses on eight "uttons on PORTA% * an! continuousl !ispla s the current count on eight &'Ds * on PORTB. * * B going through the e(ercises that are paire! )ith this program% * ou )ill "ecome $amiliar )ith the A#R Stu!io Simulator an! * De"ugger. */ *inclu!e +avr/io.h> Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 *!e$ine P,-A.'-D.#A&/' voi! init12 3 DDRA 4 0(005 DDRB 4 0($$5 6 0(FF

// PORTA use! entirel // PORTB use! entirel

$or input $or output

int main12 3 int "uttonPresses405 uint7.t thisState40(00% prevState40(00% change!40(005 init125 )hile 182 3 thisState 4 P,-A5 i$ 1thisState 44 P,-A.'-D.#A&/'2 "rea95 change! 4 thisState : prevState5 i$ 1change! ;4 02 3 $or 1int i405 i+75 i<<2 3 uint7.t theBit 4 18 ++ i25 i$ 11change! = theBit2 == 1thisState = theBit22 3 "uttonPresses<<5 PORTB 4 "uttonPresses5 6 6 6 prevState 4 thisState5 6 PORTB 4 "uttonPresses5 )hile 182 5 /*-OTR'A?@'D*/ 6 // >ust a place to put a $inal "rea9point

simcounter3PORTA2sti8
00000000A00 00080000A08 Eric B1 'ertC 8010!08!00 );0108-

SJSU ME106 00088000A00 0008B000A08 0008C000A00 0008D000A08 0008E000A0C 0008F000A08 0008G000A0C 00087000A08 0008H000A00 000B0000A0F 000B8000A00 000BB000A0C 000BC000A0B 000BD000A08 000BE000A00 00800000AFF HHHHHHHHA00

Eric B1 'ertC 8010!08!00 );0108-

Anda mungkin juga menyukai