loops - iowa state universityclass.ece.iastate.edu/ee185/pdfs/basics/loops.pdf · basics: loops...
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051805/5ff81c07e4222a557a1ccb7d/html5/thumbnails/8.jpg)
Review
Afterthislab,youshouldhaveagoodunderstandingofthefollowingtopics.Ifyou’renotsureaboutsomeofthem,gobackthroughthelabandtrytoafindagoodplacetoexplorethetopic.
Youshouldbeabletodothefollowing• Explainthelogicalflowofwhileloopsandforloopswithblockdiagrams.• Usethe“loopvariable”ofaforlooptoperformcleveriterativeoperations,suchaslightingup
LEDsinasequence.• Useawhilelooptoexecutecodeuntilaconditionismet.
Application
Tryapplyingtheskillsyoulearnedfromthislab.Youcancomeupwithyourownprojectideaortryonefromtheflowchart.