Table Of ContentDesigning Embedded Systems with PIC Microcontrollers
Principles and applications
Designing Embedded Systems with
PIC Microcontrollers
Principles and applications
Tim Wilmshurst
AMSTERDAM • BOSTON (cid:127) HEIDELBERG (cid:127) LONDON (cid:127) NEWYORK (cid:127) OXFORD
PARIS (cid:127) SANDIEGO (cid:127) SANFRANCISCO (cid:127) SINGAPORE (cid:127) SYDNEY (cid:127) TOKYO
NewnesisanimprintofElsevier
NewnesisanimprintofElsevier
LinacreHouse,JordanHill,OxfordOX28DP,UK
TheBoulevard,LangfordLane,Kidlington,OxfordOX51GB,UK
84Theobald’sRoad,LondonWC1X8RR,UK
Radarweg29,POBox211,1000AEAmsterdam,TheNetherlands
30CorporateDrive,Suite400,Burlington,MA01803,USA
525BStreet,Suite1900,SanDiego,CA92101-4495,USA
Firstedition2007
Copyright©2007,TimothyWilmshurst.PublishedbyElsevierLtd.Allrightsreserved
TherightofTimothyWilmshursttobeidentifiedastheauthorofthisworkhasbeen
assertedinaccordancewiththeCopyright,DesignsandPatentsAct1988
Nopartofthispublicationmaybereproduced,storedinaretrievalsystem
ortransmittedinanyformorbyanymeanselectronic,mechanical,photocopying,
recordingorotherwisewithoutthepriorwrittenpermissionofthepublisher
PermissionsmaybesoughtdirectlyfromElsevier’sScience&TechnologyRights
DepartmentinOxford,UK:phone(+44)(0)1865843830;fax(+44)(0)1865853333;
email:permissions@elsevier.com.Alternativelyyoucansubmityourrequestonlineby
visitingtheElsevierwebsiteathttp://elsevier.com/locate/permissions,andselecting
ObtainingpermissiontouseElseviermaterial
Notice
Noresponsibilityisassumedbythepublisherforanyinjuryand/ordamagetopersons
orpropertyasamatterofproductsliability,negligenceorotherwise,orfromanyuse
oroperationofanymethods,products,instructionsorideascontainedinthematerial
herein.Becauseofrapidadvancesinthemedicalsciences,inparticular,independent
verificationofdiagnosesanddrugdosagesshouldbemade
BritishLibraryCataloguinginPublicationData
Wilmshurst,Tim
DesigningembeddedsystemswithPICmicrocontrollers:
principlesandapplications
1. Embeddedcomputersystems–Designandconstruction
2. Microprocessors–Designandconstruction
I. Title
004.1’6
LibraryofCongressControlNumber:2006933361
ISBN-13:978-0-7506-6755-5
ISBN-10:0-7506-6755-9
ForinformationonallNewnespublications
visitourwebsiteatwww.books.elsevier.com
PrintedandboundinGreatBritain
07 08 09 10 11 11 10 9 8 7 6 5 4 3 2 1
Contents
Introduction xxi
Acknowledgements xxv
Section 1 Getting Started with Embedded Systems 1
1 Tinycomputers,hiddencontrol 3
1.1 Themainidea–embeddedsystemsintoday’sworld 3
1.1.1 Whatisanembeddedsystem? 3
1.2 Someexampleembeddedsystems 4
1.2.1 Thedomesticrefrigerator 4
1.2.2 Acardoormechanism 5
1.2.3 Theelectronic‘ping-pong’ 6
1.2.4 TheDerbotAutonomousGuidedVehicle 7
1.3 Somecomputeressentials 8
1.3.1 Elementsofacomputer 9
1.3.2 Instructionsets–CISCandRISC 9
1.3.3 Memorytypes 10
1.3.4 Organisingmemory 10
1.4 Microprocessorsandmicrocontrollers 11
1.4.1 Microprocessors 11
1.4.2 Microcontrollers 12
1.4.3 Microcontrollerfamilies 13
1.4.4 Microcontrollerpackagingandappearance 14
1.5 MicrochipandthePICmicrocontroller 15
1.5.1 Background 15
1.5.2 PICmicrocontrollerstoday 15
1.6 AnintroductiontoPICmicrocontrollersusingthe12Series 17
1.6.1 The12F508architecture 18
1.7 Whatothersdo–aFreescalemicrocontroller 20
Summary 22
References 22
Section 2 Minimum Systems and the PIC® 16F84A 23
2 IntroducingthePIC® 16Seriesandthe16F84A 25
2.1 Themainidea–thePIC16Seriesfamily 25
2.1.1 Afamilyoverview 25
vi Contents
2.1.2 The16F84A 27
2.1.3 Acautiononupgrades 27
2.2 Anarchitectureoverviewofthe16F84A 27
2.2.1 TheStatusregister 29
2.3 Areviewofmemorytechnologies 29
2.3.1 StaticRAM(SRAM) 30
2.3.2 EPROM(ErasableProgrammableRead-OnlyMemory) 31
2.3.3 EEPROM(ElectricallyErasableProgrammableRead-OnlyMemory) 31
2.3.4 Flash 31
2.4 The16F84Amemory 32
2.4.1 The16F84Aprogrammemory 32
2.4.2 The16F84AdataandSpecialFunctionRegistermemory(‘RAM’) 33
2.4.3 TheConfigurationWord 35
2.4.4 EEPROM 35
2.5 Someissuesoftiming 37
2.5.1 Clockoscillatorandinstructioncycle 37
2.5.2 Pipelining 38
2.6 Power-upandReset 38
2.7 Whatothersdo–theAtmelAT89C2051 40
2.8 Takingthingsfurther–the16F84Aon-chipresetcircuit 41
Summary 44
References 44
3 Parallelports,powersupplyandtheclockoscillator 45
3.1 Themainidea–parallelinput/output 46
3.2 Thetechnicalchallengeofparallelinput/output 46
3.2.1 Buildingaparallelinterface 46
3.2.2 Portelectricalcharacteristics 49
3.2.3 Somespecialcases 49
3.3 Connectingtotheparallelport 52
3.3.1 Switches 52
3.3.2 Light-emittingdiodes 53
3.4 ThePIC16F84Aparallelports 55
3.4.1 The16F84APortB 55
3.4.2 The16F84APortA 55
3.4.3 Portoutputcharacteristics 56
3.5 Theclockoscillator 59
3.5.1 Clockoscillatortypes 59
3.5.2 Practicaloscillatorconsiderations 60
3.5.3 The16F84Aclockoscillator 60
3.6 Powersupply 61
3.6.1 Theneedforpower,anditssources 61
3.6.2 16F84Aoperatingconditions 62
3.7 Thehardwaredesignoftheelectronicping-pong 63
Contents vii
Summary 64
References 64
4 Startingtoprogram–anintroductiontoAssembler 65
4.1 Themainidea–whatprogramsdoandhowwedevelopthem 66
4.1.1 TheproblemofprogrammingandtheAssemblercompromise 66
4.1.2 TheprocessofwritinginAssembler 67
4.1.3 Theprogramdevelopmentprocess 68
4.2 ThePIC16Seriesinstructionset,withalittlemoreontheALU 69
4.2.1 MoreonthePIC16SeriesALU 69
4.2.2 ThePIC16Seriesinstructionset–anintroduction 70
4.3 AssemblersandAssemblerformat 71
4.3.1 IntroducingAssemblersandtheMicrochipMPASMTM Assembler 71
4.3.2 Assemblerformat 71
4.3.3 Assemblerdirectives 72
4.3.4 Numberrepresentation 72
4.4 Creatingsimpleprograms 73
4.4.1 Asimpledatatransferprogram 73
4.5 Adoptingadevelopmentenvironment 76
4.5.1 IntroducingMPLAB 76
4.5.2 TheelementsofMPLAB 76
4.5.3 TheMPLABfilestructure 77
4.6 AnintroductoryMPLABtutorial 77
4.6.1 Creatingaproject 77
4.6.2 Enteringsourcecode 79
4.6.3 Assemblingtheproject 80
4.7 Anintroductiontosimulation 81
4.7.1 Gettingstarted 81
4.7.2 Generatingportinputs 81
4.7.3 Viewingmicrocontrollerfeatures 82
4.7.4 Resettingandrunningtheprogram 82
4.8 Downloadingtheprogramtoamicrocontroller 83
4.9 Whatothersdo–abriefcomparisonofCISCandRISCinstructionsets 86
4.10 Takingthingsfurther–the16Seriesinstructionsetformat 87
Summary 88
References 88
5 BuildingAssemblerprograms 89
5.1 Themainidea–buildingstructuredprograms 89
5.1.1 Flowdiagrams 89
5.1.2 Statediagrams 91
5.2 Flowcontrol–branchingandsubroutines 92
5.2.1 Conditionalbranchingandworkingwithbits 92
5.2.2 SubroutinesandtheStack 94
viii Contents
5.3 Generatingtimedelaysandintervals 95
5.4 Dealingwithdata 97
5.4.1 IndirectaddressingandtheFileSelectRegister 97
5.4.2 Look-uptables 98
5.4.3 Exampleprogramwithdelaysandlook-uptable 99
5.5 Introducinglogicalinstructions 101
5.6 IntroducingarithmeticinstructionsandtheCarryflag 102
5.6.1 Usingaddinstructions 102
5.6.2 Usingsubtractinstructions 102
5.6.3 Anarithmeticprogramexample 102
5.6.4 UsingindirectaddressingtosavetheFibonacciseries 104
5.7 TamingAssemblercomplexity 106
5.7.1 IncludeFiles 106
5.7.2 Macros 107
5.7.3 MPLABspecialinstructions 108
5.8 MoreuseoftheMPLABsimulator 109
5.8.1 Breakpoints 109
5.8.2 Stopwatch 110
5.8.3 Trace 110
5.9 Theping-pongprogram 112
5.9.1 Astructurefortheping-pongprogram 112
5.9.2 Exploringtheping-pongprogramcode 115
5.10 Simulatingtheping-pongprogram–tutorial 116
5.10.1 Settingupinputstimulus 116
5.10.2 SettinguptheWatchwindow 116
5.10.3 Singlestepping 116
5.10.4 Animate 117
5.10.5 Run 117
5.10.6 Breakpoints 117
5.10.7 Stopwatch 117
5.10.8 Trace 117
5.10.9 Debuggingthefullprogram 118
5.11 Whatothersdo–graphicalsimulators 118
Summary 119
References 119
6 Workingwithtime:interrupts,countersandtimers 120
6.1 Themainidea–interrupts 121
6.1.1 Interruptstructures 121
6.1.2 The16F84Ainterruptstructure 122
6.1.3 TheCPUresponsetoaninterrupt 124
6.2 Workingwithinterrupts 125
6.2.1 Programmingwithasingleinterrupt 125
6.2.2 Movingtomultipleinterrupts–identifyingthesource 126
Contents ix
6.2.3 Stoppinginterruptsfromwreckingyour
program1–contextsaving 127
6.2.4 Stoppinginterruptsfromwreckingyourprogram2–critical
regionsandmasking 130
6.3 Themainidea–countersandtimers 131
6.3.1 Thedigitalcounterreviewed 131
6.3.2 Thecounterastimer 132
6.3.3 The16F84ATimer0module 134
6.4 Applyingthe16F84ATimer0,withexamplesusingtheelectronicping-pong 136
6.4.1 Objectoreventcounting 136
6.4.2 Hardware-generateddelays 137
6.5 TheWatchdogTimer 138
6.6 Sleepmode 139
6.7 Whatothersdo 140
6.8 Takingthingsfurther–interruptlatency 141
Summary 142
Section 3 Larger Systems and the PIC® 16F873A 143
7 LargersystemsandthePIC® 16F873A 145
7.1 Themainidea–thePIC16F87XA 146
7.2 The16F873AblockdiagramandCPU 146
7.2.1 OverviewofCPUandcore 146
7.2.2 Overviewofmemory 147
7.2.3 Overviewofperipherals 150
7.3 16F873Amemoryandmemorymaps 150
7.3.1 The16F873Aprogrammemory 150
7.3.2 The16F873AdatamemoryandSpecialFunctionRegisters 152
7.3.3 TheConfigurationWord 154
7.4 ‘Special’memoryoperations 155
7.4.1 AccessingEEPROMandprogrammemory 155
7.4.2 In-CircuitSerialProgramming(ICSPTM) 156
7.5 The16F873Ainterrupts 158
7.5.1 Theinterruptstructure 158
7.5.2 Theinterruptregisters 159
7.5.3 Interruptidentificationandcontextsaving 161
7.6 The16F873Aoscillator,resetandpowersupply 161
7.6.1 Theclockoscillator 161
7.6.2 Resetandpowersupply 161
7.7 The16F873Aparallelports 161
7.7.1 The16F873APortA 163
7.7.2 The16F873APortB 164
7.7.3 The16F873APortC 164