Transcript
Page 1: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

EE185Lab

Basics:LoopsLoopsallowustorepeatsectionsofcode,sothatwedon’thavetoretypethem.

SuggestedPrerequisites• ProgrammingIntro• ConditionalStatements

SetupandPreparation

ListofMaterials• TIMSP430G2LaunchPad• Breadboard• M/FJumperWires(4)• LEDsofanycolor(3)

PreparetheCircuitCreatethefollowingcircuit(Figure1).

Figure1:LoopsCircuit

Page 2: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

PinNumberingSometimes,itiseasiertorefertopinsontheLaunchPadbytheirpinnumbers,asopposedtotheirprintedpinname.ThefullpinmapguidefortheMSP430G2553isgiveninFigure2.

Figure2:PinMapforMSP430G2553

Page 3: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Program1:WhileLoop

Createthefollowingprogram(Figure3),anduploadittotheLaunchPad.

Figure3:Whileloopprogram

Thingstoconsider• YoucanreplaytheprogrambypressingtheRESETbuttonontheLaunchPad.• Considerthesimilaritiesbetweenanifstatementandawhileloop.• Manythingsarehappeninginthisprogram.Youshouldconsidertryingtoexplainthiswhileloop

usingablockdiagram.• Adescriptionofthewhileloopcanbefoundatthefollowinglink:

o http://energia.nu/reference/while/

Page 4: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Program2:Forloop

Createthefollowingprogram(Figure4),anduploadittotheLaunchPad.

Figure4:Forloopprogram

Thingstoconsider• YoucanreplaytheprogrambypressingtheRESETbuttonontheLaunchPad.• Considerthesimilaritiesbetweenawhileloopandaforloop.• Youcantrytoexplainthisforloopusingablockdiagram.• ItisverycommontoreplaceloopNumber(alsoknownasthe“loopvariable”)withasingle

lettervariable,suchasi,j,orkforthesakeofsimplicity.• Adescriptionoftheforloopcanbefoundatthefollowinglink:

o http://energia.nu/reference/for/

Page 5: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Program3:Iteratingoverasequence

Createthefollowingprogram(Figure5),anduploadittotheLaunchPad.

Figure5:IteratingoverLEDsprogram

Thingstoconsider• YoucanreplaytheprogrambypressingtheRESETbuttonontheLaunchPad.• ThisprogramisnearlyidenticaltoProgram2,butusesasubtleandclevermodification.• Youcantrytogeneralizewhenforloopsaremostuseful.

Page 6: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Program4:Waitingforacondition

Createthefollowingprogram(Figure6),anduploadittotheLaunchPad.

Figure6:Waitingforconditionprogram

Thingstoconsider• UsethePUSH2buttonontheLaunchPadtocharacterizethebehavioroftheprogram.• YoucanreplaytheprogramusingtheRESETbuttonontheLaunchPad.• Youcantrytogeneralizewhenwhileloopsaremostuseful.

Page 7: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Program5:Infiniteloop

Createthefollowingprogram(Figure7),anduploadittotheLaunchPad.

Figure7:Infiniteloopprogram

Thingstoconsider• Thisprogrambehavesidenticallytothebasicblinkprogram,butdoesnotutilizethenative

loop()function.

Page 8: Loops - Iowa State Universityclass.ece.iastate.edu/ee185/pdfs/basics/Loops.pdf · Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them

Review

Afterthislab,youshouldhaveagoodunderstandingofthefollowingtopics.Ifyou’renotsureaboutsomeofthem,gobackthroughthelabandtrytoafindagoodplacetoexplorethetopic.

Youshouldbeabletodothefollowing• Explainthelogicalflowofwhileloopsandforloopswithblockdiagrams.• Usethe“loopvariable”ofaforlooptoperformcleveriterativeoperations,suchaslightingup

LEDsinasequence.• Useawhilelooptoexecutecodeuntilaconditionismet.

Application

Tryapplyingtheskillsyoulearnedfromthislab.Youcancomeupwithyourownprojectideaortryonefromtheflowchart.


Recommended