AfterChapter3’sexaminationoftheoutputmode,we’llnowturntoPICpinsusedasdigitalinputdevices.
ManyPICsincludeanalog-to-digitalconvertersandwe’llcoveranaloginputsinChapter11.
Digitalsignallevelsareeitheralogicallow(0)oralogicalhigh(1)—whatcouldbesimpler?Aswiththe
restofthisbook,weassumeVDDis5voltsandVSSis0volts.InPIClogic,a0voltinputcorrespondstoa
logicallow.Likewise,a5Vinputisalogicalhigh.But,supposeourinputis1.7V.Isitalogicalloworisita
logicalhigh?Doestheanswertothisquestiondependonourchoiceofaninputpin?And,doesitdependon
thevoltageattheinputpinearlierintime?We’llfindoutinthischapter.
Introduction
First,let’sdefineafewterms,asillustratedinFigure4-1:
VIL—Themaximumvoltageonaninputpinthatwillbereadasa
logicallow.
VIH—Theminimumvoltageonaninputpinthatwillbereadasa
logicalhigh.
Undefinedregion—Theundefinedregionthevoltagelevel
betweenVILandVIH.Inputvoltagesintheundefinedregion
maybereadasaloworasahigh,asthePIC’sinputcir-cuitrymayproduceoneresultortheother.(Obviouslythe
voltagewillbereadaseitheraloworahigh;it’sjustthatwe
havenoassurancewhichoneitwillbe.)
Thresholdvoltage—Theinputvoltagethatseparatesalowfromahigh;thethresholdvoltage,VT,minusa
smallincrementisreadasalowwhilethethresholdvoltageplusasmallincrementisreadasahigh.
Thethresholdvoltagediffersfromlogicfamilytologicfamilyandsomewhatbetweendifferentchipsof
thesametype.ThedifferencesbetweenVTandVILandVIH aredesignmarginsaccountingfordevice-to-deviceprocesstolerances,temperatureeffectsandthelike.
Inanideallogicdevice,VILequalsVIHandthereisnoundefinedregion.
MicrochiphaschosentobuildPICswithvaryinginputdesignsandassociatedvaryingVILandVIHvalues.
Wewillnotconsiderafewspecialpurposepins,suchasthoseassociatedwiththeoscillator,inthischapter.
Evenso,the16F87xseriesPICshavethreeinputpinvariations:
TTLlevel—Ofthemanylogicfamiliesintroducedintheearlydaysofdigitalintegratedcircuits,transis-tor-transistorlogic(TTL)wasbyfarthemostsuccessfulwithTTLanditsdescendantsstillusedtoday.
PortAandPortBinputpinsmimicTTLlogicinputlevels,exceptforRA4,whichhasaSchmitttrigger
input.(Ofcourse,TTLstyleSchmitttriggerinputsexist;butsincetheyarenotfoundinthePICswe
consider,wewon’tfurtherconsiderthem.)
Figure4-1:Inputlevelrelationships.
Schmitttriggerinputs—Almostallotherinputpinsareof
Schmitttriggerdesign.ASchmitttriggerhasdifferent
transitionvoltages,dependingonwhethertheinputsignal
ischangingfromhightoloworlowtohigh,asillustrated
inFigure4-2.
SpecialSchmitttriggerinputs—PinsRC3andRC4aresoft-wareselectableSchmitttriggerorSMBusconfiguration.
(SMBusisaprotocoldevelopedbyIntelfordataexchange
betweenintegratedcircuits.Wewillnotfurtherdiscuss
SMBuscommunicationsinthisbook.)WhenRC3and
RC4areusedasnormal,generalpurpose,inputpins,their
parametersdifferslightlyfromthoseoftheotherSchmitt
triggerinputpins.
InputLogicLevelSpecifications:16F87xwith4.5V<VDD<5.5V
InputType VIL(max) VIH(min)
TTL 0.8V 2.0V
Schmitt 0.2VDD 0.8VDD
RC3/RC4Schmitt 0.3VDD 0.7VDD
0 2 4 6
0 2 4 6
Low to High Transition
High to Low Transition
Output Voltage
Input Voltage
Figure4-2:Schmitttriggerinput.
Figure4-3:Inputlogiclevelcomparison.
TTL Schmitt RC3/4 Schmitt
0
Input Logic Levels 16F87x PIC
Undefined High
Low
Input Voltage (V)
Input Pin Type
Figure 4-4: SN7400 TTL logic levels Ch1: X-axis
(gateinput);Ch2:Y-axis(gateoutput).
Figure4-3illustratesthedifferencesbetweenthethreeinputtypes.Toseehowtheseinputdesignsdifferin
practice,wecanapplyatimevarying0…5Vsignaltoaninputpinandplottheinputvoltageagainsttheoutput
valueusinganoscilloscope.Ifwehaveastand-alonelogicgate,thisisastraightforwardexercise.Figure4-4,
forexample,showstheinputversusoutputresultforaSN7400TTLquadNANDgate.(Toobtainanoninvert-ingoutput,thetestconfigurationplacestwoNANDgatesinseries,andtoyieldthefull5Vlogichigh,a2.2K
ohmpull-upresistorwasaddedtotheoutputgate.)Weseeaclear,narrowtransition,withVTRANSITIONaround
1.6V,fullyconsistentwithMicrochip’sVILandVIHparametersforTTLmimickinginputpins.
Ifwewishtoobtainasimilarplotwitha16F877,however,wemustsomehowdeterminethelogicalstate—
highorlow—oftheinputpincorrespondingtotheinputvoltageto.Theeasiestwaytodothisissimplyecho
theinputpin’svaluetoanoutputpin.Inpseudo-codethealgorithmis:
ReadPin:
ReadInputPin-IfInputPin<>1MakeOutputPin=0 ReadInputPin-IfInputPin<>0MakeOutputPin=1 GotoReadPin
Itturnsoutthattobeuseful,theprocessofreadingtheinputpinandmakingtheoutputpinmustbedonemore
quicklythanpossibleusingMBasic,we’lladdsomehighspeedassemblerintothemix.Thereasonforthe
unusualpseudo-codestructure(thenotequaloperator)willbecomeclearwhenwelookattherealcode.
Program4-1
;Program4-01 InputB0 OutputB1 Main ASM { ReadIn
btfss PortB,0 ;IfRB0=1thenskipsettingitto0
bcf PortB,1 ;makeRB1=0
btfsc PortB,0 ;IfRB0=0thenskipsettingitto1
bsf PortB,1 ;makeRb1=1
GoTo ReadIn ;Repeattheloop }
GoToMain End
Don’tworryifyoudon’tunderstandtheassemblerportionsofProgram4-1,aswewilllearnmoreabout
mixingassemblerandMBasiclater.However,theactualprogramtracksthepseudo-code.Wefirstmake
RB0aninputandRB1anoutputusingMBasic.Then,themainprogramisanendlessloop.
ThebtfssPortB,0statementreadsthe0’thbitofPortB(RB0)andifitis“set,”i.e.,ifitreadshigh,the
immediatelyfollowingstatementisskipped.Ifitislow,thestatementimmediatelyfollowingisexecuted.
(ThemnemonicisBitTestFile,SkipifSet,orbtfss).
btfss PortB,0 ;IfRB0=1thenskipsettingitto0
bcf PortB,1 ;makeRB1=0
TheabovecodereadsRB0andbranches,dependingonitsvalue.IfRB0isset—thatis,RB0=1,thenthe
nextstatement(bcfPortB,1)isskipped.Conversely,ifRB0=0,thenbcfPortB,1isexecuted.The
bcf —orbitclearfile—operatorclearsorsetstozeroabit,inthiscase,RB1.Thesetwostatements,there-fore,readRB0andsetRB1tozeroifRB1iszero.
btfsc PortB,0 ;IfRB0=0thenskipsettingitto1
bsf PortB,1 ;makeRb1=1
Thenexttwostatementsperformtheinverseoperation.RB0isreadasecondtimeandtested—butthistime
fortheclearstate—usingthebtfscoperation.(Thebtfscoperatorworksjustlikebtfss,exceptthenext
instructionisskippedifthetestedbitisclear.)IfRB0ishigh,thentheoperationbsfPortB,1 isper-formed,therebysettingRB1.
ExecutioncontinueswiththeGoToReadIn,whichloopsbacktoreadingRB0.
Toterminatethisprogram,powermustberemovedfromthePIC,oranotherprogramwrittenintoits
memory.
Program4-1isn’tthefastestwaytotransferaninputpinleveltoanoutputpin,butwe’lllookatmoreef-ficienttechniqueslater.Italsousesmultiplereadactions,therebycreatingthepossibilityofmishandlingif
theinputchangesvaluebetweenthetworeadoperations.
Witha20MHzclock,thisprogramhasa1.2µsoperat-ingcycle.(TheSN7400gate,performingthesetasksin
hardware,requireslessthan10ns,120timesfasterthanthe
PIC’ssoftware.)Hence,whenweexaminetheinput/output
relationshipwiththesamesetupweusedfortheSN7400
gate,wewillexpecttoseehorizontalsmearing,wherethe
outputlagstheinputbythisdelay.SeeFigure4-5.Wesee
VTHRESHOLDisapproximately1.5V,quiteclosetothevalue
measuredfortheSN7400trueTTLgate.
ModifyingProgram4-1toacceptRC0astheinputand
runningthesameinput/outputsweep,weseeinFigure
4-6thehysteresisoftheSchmitttrigger.Thelow-to-high
transitionoccursatapproximately3.1V,whilethehigh-to-lowtransitionoccursatapproximately1.8V.Thebeauty
ofseparatehigh-lowandlow-hightransitionlevelsisnoise
rejection.Supposetheinputsignalhasnoiseridingonit,
perhapsinducedfromhigh-speedlogicchipsonthesame
board.Onceatransitionfromonestatetotheotherhas
occurred,ittakesa1.3Vnoiseexcursiontocauseareverse
transition.Asweshallseelater,thishysteresisaddsgreatly
tonoiserejection.
WeunderstandMicrochip’sdecisiontouseTTLmimick-inginputsastheproductofbackwardcompatibilitywith
earlierPICsandaccesstothehugebaseofTTLdevices.
But,whyhasMicrochipdesignedtherestofitsPICinputs
withSchmitttriggerinputsinsteadofnormalCMOSin-puts,suchasthatseeninFigure4-7foraCD4001BEquad
NORgate?Afterall,PICsareCMOSdevicessoitmakes
sensetogivethemstandardCMOScharacteristics,right?
Figure 4-5: 16F876 TTL logic levels Ch1: X-axis
(RB0input);Ch2:Y-axis(RB1output).
Figure4-6:16F876SchmitttriggerlogiclevelsCh1:
X-axis(RC0input);Ch2:Y-axis(RB1output)
Figure4-7:CD4001BEquad2-inputNORCMOS
logic levels Ch1: X-axis (input); Ch2: Y-axis
(output).
Figure 4-8: Noisy input read differently by
standardCMOSandSchmittinputs.
IfMicrochipthoughitsPICswouldcommunicateonlywithotherintegratedcircuitsinstalledonthesame
board,itlikelywouldhaveadoptedstandardCMOSinputlevels.But,PICsoftenmustcommunicatewith
therealworld,throughsensorsandswitches,operatinginamuchlessbenignenvironmentwherethenoise
rejectingpropertiesoftheSchmitttriggercometothefore.Figure4-8illustratestheabilityofaSchmitt
triggerinputtocorrectlyreadaninputsignalinthepresenceoflargenoisevoltages.
Inordertoensurethatlevelsarecorrectlyread,regardlessoftheinputtype,weshouldaimforlogic
0inputlevelsnotexceeding0.8Vandlogichighlevelsofatleast4.0V.Ifwemeettheseobjectives,
allinputporttypeswillcorrectlyreadtheinputlevels.Iftheselevelscannotbeachieved,itwillbe
necessarytofurtherinvestigatethedesigntoensurereliabledatatransfer.
Regardlessoftheirtype,PIC
inputpinsrepresentahighinput
impedancetotheoutsideworld.
AsreflectedinFigure4-9,the
onlycurrentthatflowsinorout
ofaninputpinisduetoleakage
anddoesnotexceed1µA.(Inthe
16F876family,pinRA4’sleakage
currentmaybeupto5µA.)For
lowimpedancecircuitswemay
safelyconsideraninputpinasan
opencircuit,withzerocurrentflow
inoroutofthepin.Ahighimped-anceinputpincarrieswithitthe
possibilityofstaticdamage,asevenasmallstaticchargeproduceshighvoltagesintoahighimpedancepin.
AlthoughtheclampingdiodestoVDDandVSShelppreventdamage,it’sstillagoodideatofollowanti-static
precautionswhenhandlingPICs.Italsomeansthatinputpinsshouldbeprotectedwhenexposedtooutside
worldvoltageandcurrents.Wewillbrieflycoversomeoftheserealworldissuesinthisandlaterchapters.
Theexceptiontothisassumptionrelatestothesoftwareenabledinternalpull-upresistorsonpinsRB0…
RB7.Iftheweakpull-upfeatureonPortBisenabledviaMBasic’sprocedureSetPullUps,theinputpins
areconnectedtoVDDthroughaninternal20Kohmresistor.IfenabledthroughtheSetPullUpsPU_On
ordisabledthroughSetPullUpsPU_Offprocedure,theactionappliestoallPortBpins.Inthiscase,the
inputpinsources250µA.
We’vereferredtopull-upresistorswithoutexplainingwhyandwheretheyareused.Supposewewishto
readaswitchanddetermineifitisopenorclosed.Ifwesimplyconnecttheswitchtoaninputpin,asin
Figure4-10a,wecannotbeassuredof
RB0’sstatuswhentheswitchisopen.
Certainly,iftheswitchisclosed,RB0
willbeatgroundpotentialandwill
bereadasalogicallow.Whenopen,
however,thevoltageatRB0results
fromthePIC’sinternalrandomleak-agecurrent,pluswhateverstraysignal
thatoutsidecircuitrymayinduceinthe
connectionsbetweentheswitchand
Figure4-9:Simplifiedmodelofinputpins.
Figures4-10a,b,c:Pull-uprequiredtoreadswitch.
RB0.Theresultmaybealogicalhigh,orlow,andwearewithoutanyassurancethattheswitch’sposition
willbecorrectlyorconsistentlyread.
Ifinstead,weconnectRB0toVDD,eitherthroughtheinternalpull-upsource(Figure4-10c)orthroughan
externalpull-upresistor(Figure4-10b),whentheswitchisopenRB0willbe“pulledup”toVDDandthe
switchpositionwillcorrectlybereadasalogicalhigh.