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