InlearninghowtoprogrammingaPCinahighlevellan-guage,thetraditionalfirstprogramwrites“HelloWorld”to
thescreen.SincePICsdon’thaveascreen,thefirstMBasic
programtraditionallyblinksanLED.We’lldothatideaone
better,buildinguptofourstateswithoneLEDandonePIC
pin.But,firstwe’llstartwithtwoLEDsandtwopinsas
showninFigure3-7.
Figure3-6:Possibleswitchingconfigurations.
Figure3-7:LEDconnections.
Program3-1
i var byte
Fori=B0toB1 ;LEDsareonB0…B1
OutputI ;sowemakethemoutputs Next
Main
Fori=B0toB1 ;somewillilluminatewithalow
Lowi
Next
Pause1000
Fori=B0toB1 ;somewillilluminatewithahigh
Highi
Next
Pause1000 GoToMain
End
Thecodeisstraightforward;Afterdeclaringourindexvariablei,wesetpinsRB0…RB1tobeoutputswith
theOutputprocedureinsideaFor…Nextloop.TheOutput(i) proceduretakesthepinaddressasitsargu-ment,withirangingfromB0toB1,pre-definedinMBasicasthenumericaladdressesofpinsRB0…RB1.
Wethensetthesepinstoalternatebetweenlowandhigh,with1second(1,000milliseconds)ineachstate
usingMBasic’sHighandLowproceduresinsidetwoFor…Nextloops,eachfollowedbyaPause(1000) procedure.Anendlessloop(Main…GoToMain)causesthealternatinghigh/
lowstepstoberepeated.
D1illuminatedwhenRB0low—WhenRB0goeslow,currentfromthe+5V
supplygoesthroughseriescombinationofLEDD1,resistorR3andthe
internalresistanceofRB0.LEDsmayberegardedasadevicethathave
approximatelyaconstantvoltagedropfortypicaloperatingcurrentsinthe
rangefrom1mAtotensofmA.Figure3-8illustrates,forcurrentlevels
between1and50mA,theLED’svoltagedropisbetween1.7and2.2V.
Withonlyasmallerror,wemayregardtheLEDasaconstantvoltagede-vice,withabouta2Vdrop.(There’saslightdifferenceinvoltagedropfor
differentoutputcolors,butforalmostallred,greenandyellowLEDs,we
maycalculatethecurrentlimitingresistorsassuminga2voltsdrop.)
WemaynowsolvethecurrentloopequationforthecircuitinvolvingD1,re-memberingthatalowpinisfunctionallyequivalenttoa25ohmresistor:
5V=2V+250I+25I
rearranging
5V–2V+250I+25Iso3V=275I
or
I= 3 = mA
275 10 9.
WhereIisthecurrentthroughtheLEDandseriesresistor.
Moreoften,wewishtocalculatetheseriescurrentlimitingresistorneededforaparticularLEDcurrentI(in
mA)wheretheLEDisonwhenthePICdrivingpinislow:
R3 ImA 3000 25
= −
Figure3-8:E/Icurvetraceof
redLED.Horiz:0.5V/divVert:
5mA/div.
WefudgedabitbyassumingthevoltagedropacrossD1isconstantregardlessofcurrent,butthesesimple
equationswillbewithin10%ofamoredetailedcalculation,morethanaccurateenoughfordeterminingthe
currentthroughanLEDindicator.
D2IlluminatedwhenRB1high—WhenRB1goeshigh,currentfromtheVDD(the+5VsupplyinBasic
Micro’sdevelopmentboards)goesthroughseriescombinationofLEDD2,resistorR4andtheinternal
resistanceofRB1.ThisisonlyaslightrearrangementofourearlieranalysisofD1,withtheinternal
equivalentresistanceofthehighpinbeing85ohms.Hence,
5V=2V+220I+85I
rearranging
5V–2V+220I+85Iso3V=305I
or
I= 3 = mA
305 9 8.
WhereIisthecurrentthroughtheLEDandseriesresistor.
Or,tocalculatetheseriescurrentlimitingresistorwheretheLEDisonwhenthePICdrivingpinishigh(inmA):
R4 ImA 3000 85
= −
Inadditiontotheconstantvoltagedropfudge,thisanalysis
assumesahighpinismodeledaccuratelybyasan85ohm
resistorinserieswithVDD .AsFigure3-5shows,thisassump-tionstartstofailasthesourcedcurrentexceeds15mAand
theplotofIversusEdivergesfromastraightline.
TwoLED’sononepin—WecanconnecttwoLEDstoone
pinusingthecircuitswejustdevelopedasshowninFig-ure3-9.ThecurrentforeachLEDiscalculatedusingthe
sameequationsforindividualpinconnections.
Fourstatesfromonepin—UsingtheconnectionofFigure
3-10,it’spossibleforonepintoproducefourstatesin
a2-pindualLED.(MostdualLEDshavetwopins,but
somedualLEDshavethreepinspermittingthecircuitof
Figure3-9tobeused.)Fairchild’sMV5491Atwo-pindual
LEDisconfiguredasaredandgreenLEDinanti-parallel
wherebycurrentflowinonedirectionprovidesredlight
whiletheoppositedirectionprovidesgreenlight.
InthecircuitofFigure3-10whenRB2ishigh,currentflows
fromRB2throughD1andR2.WhenRB2islow,current
flowsfromthe+5VsupplythroughR1,D2andissunkat
RB2.Thesuggestedresistorsyield6.9mAcurrentforthe
greenLED(D1)and8.6mAfortheredLED(D2).
It’spossibletogetathirdcoloroutofthisdesignaswell.By
rapidlyswitchingbetweentheredandgreenLEDs,theeyeperceivesorange.Thefollowingcodefragment
willaccomplishthis,switchingatapproximately100Hz.
Figure3-9:TwoLEDsononepin.
Figure3-10:Onepin,fourstates.
Main
HighB2
Pause5
LowB2
Pause5
GoToMain
Finallyifafourthcondition,LEDoff,isdesired,switchRB2toinput.Asaninput,RB2isessentiallyan
opencircuit,andneitherD1norD2willbeilluminated.Thistrickwillnotworkwiththeconfigurationof
Figure3-9,asbothdiodeswillilluminateinthatstate.
Program3-2exercisesallfourstatesofFigure3-10’sdualLED.
Program3-2
;FourstatesfromonedualcolorLEDandonePICPin
;Assumesbi-colorLEDonRB2
;Withvoltagedividercircuit
i Var Byte
Main
HighB2 ;Green
Pause1000
LowB2 ;Red
Pause1000
Fori=0to255 ;Orange
HighB2
InputB2 ;noillumination
Pause1000
GoToMain End
Program3-2firstilluminatesthegreenLEDfor1secondfollowedbyredfor1second,followedby2.5
secondsoforangewhenboththeredandgreendiodesaresequentiallyactivefor5ms.Finally,thediodeis
darkfor1second.