microcontrollers and design pa1erns

34
Microcontrollers and Design Pa1erns J.R. Leeman and C. Marone Techniques of Geoscien>fic Experimenta>on September 15, 2016

Upload: others

Post on 04-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

MicrocontrollersandDesignPa1erns

J.R.LeemanandC.Marone

TechniquesofGeoscien>ficExperimenta>on

September15,2016

Todaywe’regoingtocovermicrocontrollerinterfacesandcodedesignbasics

• PWM• Serial,Parallel• I2C,SPI• TimersandInterrupts• StateMachines• Polling

Pulsewidthmodula>onchangesthedutycycleofasquarewave

Image:sparkfun.com

PWMisalsousedtodriveservos

Parallelinterfacestransfermanydatabitsatonce

Image:sparkfun.com

Thesewereonceverycommononprintersandothercomputerperipherals

Image:karbosguide.com

Arduinoportmanipula>oncanbedone!

Serialinterfacestransferdataonebitata>me

Image:sparkfun.com

Thebaudratemustbeagreedupon,aswellasanumberofotherfactors

Image:sparkfun.com

Therearecost/data-ratetradeoffsaswellaspincount

Serialcommunica>onsiseasywiththeArduino

Image:sparkfun.com

SPIisahighthroughputfour-wirebus

Mul>pleslavedevicescanbeaddedontoabus

Image:sparkfun.com

Somedevicesu>lizea“daisy-chain”configura>on

Image:sparkfun.com

I2Cusesonlytwowiresandallowsmul>plemasterdevices

Image:sparkfun.com

TheprotocolissignificantlymorecomplicatedthanSPIthough

Image:sparkfun.com

Func>onschunkupcodeintoreusableblocks

Image:arudino.com

Let’screateamul>plyfunc>on

Pollingrepeatedlychecksforacondi>onorevent

Interruptsalloweventhandling,butcanbedifficulttodebug

Interrupt Occurs (IRQ)

Save Place

ISR Called

Restore State(?)

Interruptsalloweventhandling,butcanbedifficulttodebug

Interruptsalloweventhandling,butcanbedifficulttodebug

Timerscanalsogenerateinterrupts

Readupon>mersbeforeusingthem-itcangetcomplex

Statemachinesareusedtocontrolmanysystems,especiallyinstrumentsandlaboratoryapparatus

Image:oracle.com

Implemen>ngastatemachine

1. Outer While Loop2. Inner Case Structure3. Initial State4. Next State Set5. Shutdown State

Let’screateastatemachineforanultrasonictapemeasure

Images:Sparkfun,Instructables

Mul>threaded-producer/consumercanbeusefulforsensinganddataloggingapplica>ons

Image:ni.com

Assignment:ArduinoStopLight

DUE:9/27/16

Ac>vity:XRDStateMachine

DUE:9/20/16