Top Banner
EE 185 Lab Basics: Loops Loops allow us to repeat sections of code, so that we don’t have to retype them. Suggested Prerequisites Programming Intro Conditional Statements Setup and Preparation List of Materials TI MSP430G2 LaunchPad Breadboard M/F Jumper Wires (4) LEDs of any color (3) Prepare the Circuit Create the following circuit (Figure 1). Figure 1: Loops Circuit
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.

Sep 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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.