[go: up one dir, main page]

0% found this document useful (0 votes)
108 views7 pages

Explanation of PIC 16F84A Processor Data Sheet Part 2: More On The PIC

The document discusses the timer module and prescalar of the PIC 16F84A processor. It explains how the timer can be configured using the OPTION_REG register to implement delays of different time periods. It also describes the memory architecture of the PIC, including program memory, registers for timers and I/O ports, and hexadecimal representation which is used to conveniently represent binary numbers in a shorter format.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views7 pages

Explanation of PIC 16F84A Processor Data Sheet Part 2: More On The PIC

The document discusses the timer module and prescalar of the PIC 16F84A processor. It explains how the timer can be configured using the OPTION_REG register to implement delays of different time periods. It also describes the memory architecture of the PIC, including program memory, registers for timers and I/O ports, and hexadecimal representation which is used to conveniently represent binary numbers in a shorter format.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii

ExplanationofPIC16F84AprocessordatasheetPart2:MoreonthePIC
ThisisthesecondofthethreepartoverviewofthePICprocessor.Wewillfirstdiscussthetimermoduleandprescalar. Wewillalsoreviewthehexadecimalrepresentation.ThenwewillcovermemoryandIO.

TimerModuleandPrescalar
ManyapplicationsofthePICrequirethattheprocessorfulfilltimingrequirements.Forexample,intheBlinkingLED2 example, the program required a 1 second delay function. This function was implemented by having it call another function,whichhada1msdelay.Themilliseconddelayfunctionwaswrittensothatitusesapproximately1000clock cycles. The problem with this approach is that the programmer must carefully count the number of machine instructions. Analternativeapproachistouseatimercircuitwhichindicateshowmuchtimehaselapsed.ThePIChassuchacircuit calledTIMER0.Itisan8bittimer/counter,andinmanycasescanbeaccessedlikeavariable.Itcanbeconfiguredso thatitwillincrementeveryclockcycle.NextisanaiveexampleimplementationofusingTIMER0.Itisadelayfunction fora100usdelay.ItjustcheckswhenTIMER0hasreachedthevalue100. void delayNaive( ) // A delay of (approximately) 100 clock cycles { TIMER0 = 0; while (TIMER0 < 100); } This will not work because we have not configured TIMER0 before accessing it. We will discuss the configuration processabitlaterbutfornowletusdiscusshowtomakethisdelaylonger.NotethatTIMER0islimitedbyitssize,which is8bits.Ifwewantalongerdelay,wemustusetheprescalar.TheprescalerslowsTIMER0downbyreducingtheclock signalratetoTIMER0.Theoptionsofratesare1:2,1:4,1:8,....,or1:256.Notethatthestepdownvaluesareallpowers oftwo. The next delay function delayNaive2 has a delay of approximately 10,000 clock cycles. First, we configure the prescalarto1:256.Thus,thetimerwillincrementevery256clockcycles.ThefunctionwillterminatewhenTIMER0= 40,i.e.,after40x256=10,240clockcycles.Thus,delayNaive2hasadelayofapproximately10,000clockcycles. void delayNaive2( ) // A delay of (approximately) 10000 clock cycles { TIMER0 = 0; // Configuring the step down rate of the TIMER0 to 1:256. // Now we set the OPTION_REG so that TOCS, TOSE, PSA, and PS2-PS1 are 0. OPTION_REG = 0b00000111; while (TIMER0 < 40); } 1

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii The line OPTION_REG = 0b00000111 is the configuration of TIMER0. (Section 5 of the PIC processor datasheet describestheTIMER0moduleandhowtoconfigureit.)Themodulehastwomodes:TimerorCounterMode.Wewant TimerModewhichusesthePICsownclocksignalforsynchronization.(CounterModeusesanexternalclocksignal.) TheTIMER0moduleisconfiguredbysettingthebitsofthe8bitregisterOPTION_REG,whichisshowninFigure1. Theregisterhasbits TOCS(bit5):TOCSdeterminesifTIMER0isinTimerorCounterMode,whereTOCS=0meansTimerMode. PSA(bit3):TheprescalarcanbeassignedtoTIMER0orthewatchdogtimer.ClearingPSAto0willassignthe prescalartoTIMER0.PSA=1assignsTIMER0tothewatchdogtimer.WewantPSA=0forourdelayfunction. PS2PS0(bits20):Determinesthestepdownrateoftheprescalar.PS2PS0=111means1:256,whilePS2PS0 =000means1:2.

Figure1.Table51fromthedatasheetforthePIC. The other bits of OPTION_REG are irrelevant for our purposes. (For example, TOSE (bit 4) is used when TIMER0 is in CounterMode,butweuseTimerMode.) WheneverTIMER0iswrittentobyamachineinstruction(e.g.,CLRF)thentheprescalarisclearedto000.Thisiswhywe setOPTION_REGafterclearingTIMER0. ThefunctiondelayNaive2maynotwork.TheOPTION_REG= 0b00000111willsettheprescalarbyhavingPS2PS0to 111,anditsetsthebitsTOCSandPSAto0.Butitalsoclearsbits7and6ofOPTION_REG.Thenextimplementationwill setbits05ofOPTION_REGwithoutchangingbits7and6. void delayNaive3( ) // A delay of (approximately) 10000 clock cycles { unsigned n; TIMER0 = 0; // Initialize TIMER0. This clears the prescalar. // Now we set the OPTION_REG so that TOCS, TOSE, and PSA are 0, and set // the prescalar to 256:1. The prefix 0b means the number is binary n = OPTION_REG & 0b11000111; // Clear TOCS, TOSE, and PSA n = n | 0b00000111; // Set prescalar to 256:1 OPTION_REG = n; while (TIMER0 < 40); } 2

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii Theoperationn=OPTION_REG&0b11000111isabitwiseANDoperation withthebitstring11000111,whichisthe mask.Themaskwillclearbits3,4,and5to0.ThesearetheTOCS,TOSE,andPSAbits. Thenextlinen = n | 0b00000111isabitwiseORingthevariablenwiththebitstring00000111,whichisanother mask.Thissetsbits20tothevalue111,whichinturnwillsettheprescalerto256:1. ThentheinstructionOPTION_REG = nproperlysetsOPTION_REG. Figure2showstheschematicdiagramoftheTIMER0moduleandprescalar.CLOCKOUTistheinternalclockofthePIC. Notice how it is routed through two multiplexers, then the prescalar (which is an 8bit counter), and the prescalars outputisthenroutedtotheTIMER0register.Notethatthe8:1multiplexerselectsabitoutoftheprescalarsoutput. AlsonoticehowthePSAandTOCSbitsareusedtoroutethesignalsthroughthe2:1multiplexers,andthePS2PS0bits areusedtocontrolthe8:1multiplexer.

Figure2.TIMER0andprescalercircuitry.ThiscomesfromFigure52ofthedatasheetofthePIC.

TimerModuleandPrescalar
NextwewillgoovermemoryinthePICarchitecture.Figure3hasablockdiagramofthePIC.TheProgramMemoryin theupperleftcornerisflashtechnologywhichcanstore1024instructionwords.RecallthatClanguagewillcompiletoa longermachineprogram.SothePICmaybelimitedtojustacouplehundredCinstructionlines. 3

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii AttachedtotheProgramMemoryisthe InstructionRegister.Thishasthecurrentinstructionthatisbeingexecuted. ProgramCounter,whichisacountercircuitthatpointstothenextinstructiontobeexecuted. InthemiddleofthefigureistheArithmeticLogicUnit(ALU).TheALUisthecalculatorfortheCPU.Itdoesmuchofthe arithmeticandthebitwiselogicoperationssuchasANDandOR.NoticethattheoutputoftheALUisaregisterW.This registercanbeaccessedbycertainmachineinstructions. ThelowerrightinFigure3hastheTIMER0andIOports.TheuppermiddleoftheblockdiagramhastheRAMmemory. Figure4showstheregistermapofthePIC.TheseregistersincludeTIMER0,PORTA,TRISA,andOPTION_REG.Italso includes general purpose registers. These general purpose registers are stored in RAM. Note that the registers have numbersassignedtothemforidentification.Forexample,registerTIMER0hasthenumber01h.Thesenumbersare calledregisternumbers,addresses,orindices.Thesuffixhindicatesthatthenumberisinhexadecimalrepresentation, whichisdiscussedbelow.Indecimalrepresentation,01hisplainold1. HexadecimalNumberRepresentation Hexadecimal representation is similar to binary or decimal representation, so let us discuss these first. Decimal representationofavalueisastringofdigits.Forexample,423isathreedigitrepresentationofthevaluefourhundred twenty three. The base of decimal representation is the value 10. The representation 423 is a representation the formula 4 10 2 + 2 101 + 3 10 0 . Noticethatthedigitsarecoefficientsofpowersof10,whichisthebase.Eachdigitisavaluerangingfrom0to9(10 possiblevalues).Binaryissimilarexceptthatthebaseis2.Thedigitsarecalledbinarydigitsorbits.Eachdigitisavalue rangingfrom0to1(2possiblevalues),andarecoefficientsofpowersof2,whichisthebase. Hexadecimal is another representation where the base is 16. Each digit is a value ranging from 0 to 15 (16 possible values).Thesedigitsarerepresentedbythesymbols:0,1,2,...9,A,B,...,F.Here,Arepresentsthevalue10,F representsthevalue15,etc.Thus,theaddress0Bhishexadecimalforthevalue11,and82hishexadecimalforthe value 8 161 + 2 10 0 = 130. Hexadecimal is used because it is a convenient way to represent binary numbers. To convertabinarynumbertohexadecimal,youmustbreakupthebitstringintochunksof4bitsstartingfromtheright. Forexample,thebinarynumber1001001111001101isbrokenupinto1001001111001101.Eachchunkof4bitscan be converted into a hexadecimal digit: 1001(=9) 0011(=3) 1100(=C) 1101(=D). The hexadecimal representation is 93CDh.Thisismuchshorterandeasiertorecognizethantheoriginalbitstring.Toconvertahexadecimalnumberto binary,justconverteachhexadecimaldigittoitsequivalent4bitbinaryrepresentation. For the PIC, hexadecimal numbers are denoted by the suffix h. However, there are other ways to indicate hexadecimal.Forexample,theprefix0xindicateshexadecimal,whichwewilluseinlecturesandhomework.

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii

Figure3.BlockdiagramofthePIC.ThisisfromFigure11inthedatasheetforthePIC.

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii

Figure4.Registermap.ThisisfromFigure22ofthedatasheets.

GalenSasaki,DepartmentofElectricalEngineering,UniversityofHawaii

InstructionProcessingCycle
Likeallcomputers,thePICfollowsaninstructionprocessingcycle,sometimesknownastheVonNeumannprocessing cycle (named after a famous mathematician and computer scientist John Von Neumann). This cycle is basically what computersdocontinually.Figure5showsthiscycle.Itisaloopof: 1. Fetchinstruction(fromprogrammemory). 2. Decodeinstruction(sothattheCPUunderstandstheinstructionandwhatstobedone). 3. Executeinstruction. Figure6hastheportionoftheblockdiagramofthePICthatisinvolvedwithfetch,decode,andexecuteinstruction.To fetch the instruction, the CPU will use the Program Counter, which points to the next instruction to execute. This instructionisstoredinInstructionRegister.TheoutputoftheInstructionRegisterisconnectedtothecircuitrycalled Instruction Decode and Control. This circuitry will first decode the instruction so that the CPU understands what the instructionis.Thengivenwhattheinstructionis,theCPUwillsendcontrolsignalstothedatapathofthecomputerto executetheinstruction(notethatthecontrollinesarenotshown,probablybecauseitwouldmakethediagramvery messytolookat).

InstructionFetch InstructionDecode InstructionExecute

Figure5.InstructionProcessingCycle Figure6.PortionofthePICthathas instructionfetchanddecode.

You might also like