Microcontroller Programming The Microchip PIC® 7189_C000a.indd 1 10/30/06 3:00:15 PM Microcontroller Programming The Microchip PIC® Julio Sanchez Minnesota State University, Mankato Maria P. Canton South Central College, North Mankato, Minnesota Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Group, an informa business 7189_C000a.indd 2 10/30/06 3:00:15 PM Microcontroller Programming The Microchip PIC® Julio Sanchez Minnesota State University, Mankato Maria P. Canton South Central College, North Mankato, Minnesota Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Group, an informa business 7189_C000a.indd 3 10/30/06 3:00:15 PM CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487‑2742 © 2007 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed in the United States of America on acid‑free paper 10 9 8 7 6 5 4 3 2 1 International Standard Book Number‑10: 0‑8493‑7189‑9 (Hardcover) International Standard Book Number‑13: 978‑0‑8493‑7189‑9 (Hardcover) This book contains information obtained from authentic and highly regarded sources. Reprinted material is quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts have been made to publish reliable data and information, but the author and the publisher cannot assume responsibility for the validity of all materials or for the consequences of their use. No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any informa‑ tion storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive, Danvers, MA 01923, 978‑750‑8400. CCC is a not‑for‑profit organization that provides licenses and registration for a variety of users. For orga‑ nizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com 7189_C000a.indd 4 10/30/06 3:00:16 PM Table of Contents Preface xv Chapter 1 - Basic Electronics 1 1.0 The Atom 1 1.1 Isotopes and Ions 2 1.2 Static Electricity 3 1.3 Electrical Charge 4 1.3.1 Voltage 4 1.3.2 Current 4 1.3.3 Power 5 1.3.4 Ohm'sLaw 5 1.4 Electrical Circuits 6 1.4.1 TypesofCircuits 6 1.5 Circuit Elements 8 1.5.1 Resistors 9 1.5.2 RevisitingOhm'sLaw 9 1.5.3 ResistorsinSeriesandParallel 10 1.5.4 Capacitors 12 1.5.5 CapacitorsinSeriesandinParallel 13 1.5.6 Inductors 14 1.5.7 Transformers 15 1.6 Semiconductors 15 1.6.1 IntegratedCircuits 16 1.6.2 SemiconductorElectronics 16 1.6.3 P-TypeandN-TypeSilicon 17 1.6.4 TheDiode 17 Chapter 2 - Number Systems 19 2.0 Counting 19 2.0.1 TheTallySystem 19 2.0.2 RomanNumerals 20 2.1 The Origins of the Decimal System 20 2.1.1 NumberSystemsforDigital-Electronics 22 2.1.2 PositionalCharacteristics 22 2.1.3 RadixorBaseofaNumberSystem 23 v vi Microcontroller Programming 2.2 Types of Numbers 23 2.2.1 WholeNumbers 24 2.2.2 SignedNumbers 24 2.2.3 Rational,Irrational,andImaginaryNumbers 24 2.3 Radix Representations 25 2.3.1 DecimalversusBinaryNumbers 25 2.3.2 HexadecimalandOctal 26 2.4 Number System Conversions 27 2.4.1 Binary-to-ASCII-Decimal 28 2.4.2 Binary-to-HexadecimalConversion 29 2.4.3 Decimal-to-BinaryConversion 29 Chapter 3 - Data Types and Data Storage 33 3.0 Electronic-Digital Machines 33 3.1 Character Representations 33 3.1.1 ASCII 34 3.1.2 EBCDICandIBM 36 3.1.3 Unicode 36 3.2 Storage and Encoding of Integers 37 3.2.1 SignedandUnsignedRepresentations 37 3.2.2 WordSize 38 3.2.3 ByteOrdering 39 3.2.4 Sign-MagnitudeRepresentation 40 3.2.5 RadixComplementRepresentation 41 3.3 Encoding of Fractional Numbers 44 3.3.1 Fixed-PointRepresentations 45 3.3.2 Floating-PointRepresentations 46 3.3.3 StandardizedFloating-PointRepresentations 47 3.3.4 IEEE754SingleFormat 48 3.3.5 EncodingandDecodingFloating-PointNumbers 50 3.4 Binary-Coded Decimals (BCD) 51 3.4.1 Floating-PointBCD 52 Chapter 4 - Digital Logic, Arithmetic, and Conversions 55 4.0 Microcontroller Logic and Arithmetic 55 4.0.1 CPUFlags 55 4.0.2 WordSize 56 4.1 Logical Instructions 56 4.1.1 LogicalAND 57 4.1.2 LogicalOR 57 4.1.3 LogicalXOR 57 4.1.4 LogicalNOT 58 4.2 Microcontroller Arithmetic 58 4.2.1 UnsignedandTwo’sComplementArithmetic 58 4.2.2 OperationsonDecimalNumbers 60 4.3 Bit Manipulations and Auxiliary Operations 62 4.3.1 BitShiftandRotate 62 4.3.2 ComparisonOperations 63 4.3.3 OtherSupportOperations 63 Contents vii 4.4 Unsigned Binary Arithmetic 64 4.4.1 Multi-byteUnsignedAddition 64 4.4.2 UnsignedMultiplication 65 4.4.3 UnsignedDivision 67 4.5 Signed Binary Arithmetic 67 4.5.1 OverflowDetectioninSignedArithmetic 69 4.5.2 SignExtensionOperations 70 4.5.3 Multi-byteSignedOperations 71 4.6 Data Format Conversions 72 4.6.1 BCDDigitstoASCIIDecimal 72 4.6.2 UnsignedBinarytoASCIIDecimalDigits 73 4.6.3 ASCIIDecimalStringtoUnsignedBinary 73 4.6.4 UnsignedBinarytoASCIIHexadecimalDigits 75 4.6.6 SignedNumericalConversions 76 Chapter 5 - Circuits and Logic Gates 77 5.0 Digital Circuits 77 5.1 The Diode Revisited 78 5.1.1 TheLight-EmittingDiode(LED) 79 5.2 The Transistor 81 5.2.1 BipolarTransistor 81 5.2.2 MOSTransistor 83 5.3 Logic Gates 84 5.4 Transistor-Transistor Logic 85 5.4.1 InverterGates 86 5.4.2 TheANDGate 87 5.4.3 TheNANDGate 87 5.4.4 TheORGate 88 5.4.5 TheNORGate 88 5.4.6 PositiveandNegativeLogic 89 5.4.7 TheXORGate 90 5.4.8 SchmittTriggerInverter 91 5.5 Other TTL Logic Families 93 5.6 CMOS Logic Gates 93 Chapter 6 - Circuit Components 95 6.0 Power Supplies 95 6.1 Clocked Logic and Flip-flops 96 6.1.1TheRSFlip-flop 96 6.1.2 ClockedRSFlip-flop 98 6.1.3 TheDFlip-flop 99 6.1.4 TheEdge-triggeredDFlip-flop 100 6.1.5 PresetandClearSignals 101 6.1.6 DFlip-flopWaveformAction 102 6.1.7 Flip-flopApplications 103 6.2 Clocks 103 6.2.1 ClockWaveforms 104 6.2.2 TheTTLClock 105 6.2.3 The555Timer 106 viii Microcontroller Programming 6.2.4 MicrocontrollerClocks 106 6.3 Frequency Dividers and Counters 107 6.3.1 FrequencyDividers 107 6.3.2 TheJKFlip-flopCounter 107 6.3.3 RippleCounters 108 6.3.4 DecodingGates 110 6.3.5 SynchronousCounters 110 6.3.6 CounterICs 112 6.3.7 ShiftRegisters 113 6.4 Multiplexers and Demultiplexers 115 6.4.1 Multiplexers 115 6.4.2 Demultiplexers 118 6.4.3 MultiplexerandDemultiplexerICs 118 6.5 Input Devices 118 6.5.1 Switches 118 6.5.2 SwitchContactBounce 120 6.5.3 Keypads 121 6.6 OutputDevices 122 6.6.1 Seven-segmentLED 122 6.6.2 LiquidCrystalDisplays 124 6.6.3 LCDTechnologies 125 Chapter 7 - The Microchip PIC 129 7.0 The PICMicro Microcontroller 129 7.0.1 ProgrammingthePIC 130 PICProgrammers 131 DevelopmentBoards 131 7.0.2 PrototypingthePICCircuit 132 7.1 PIC Architecture 134 7.1.1 BaselinePICFamily 134 PIC10Devices 135 PIC12Devices 135 PIC14Devices 138 7.1.2 Mid-rangePICFamily 138 PIC16Devices 139 7.1.3 High-PerformancePICFamily 139 PIC18Devices 139 Chapter 8 - Mid-range PIC Architecture 141 8.0 Processor Architecture and Design 142 8.0.1 HarvardArchitecture 142 8.0.2 RISCCPUDesign 143 8.0.3 Single-wordInstructions 143 8.0.4 InstructionFormat 144 8.0.5 Mid-RangeDeviceVersions 145 8.1 The Mid-range Core Features 145 8.1.1 Oscillator 145 8.1.2 SystemReset 147 8.1.3 Interrupts 148 8.2 Mid-Range CPU and Instruction Set 149 Contents ix 8.2.1 Mid-RangeInstructionSet 149 8.2.2 STATUSandOPTIONRegisters 151 8.3 EEPROM Data Storage 153 8.3.1 EEPROMinMid-RangePICs 153 8.4 Data Memory Organization 154 8.4.1 ThewRegister 154 8.4.2 TheDataRegisters 154 MemoryBanks 154 TheSFRs 155 TheGPRs 157 8.4.3 IndirectAddressing 158 8.5 Mid-range I/O and Peripheral Modules 158 8.5.1 I/OPorts 159 8.5.2 TimerModules 160 8.5.3 Capture-and-CompareModule 160 8.5.4 MasterSynchronousSerialPort(MSSP)Module 161 8.5.5 USARTModule 161 8.5.6 A/DModule 161 Chapter 9 - PIC Programming: Tools and Techniques 163 9.0 Microchip’s MPLAB 163 9.0.1 EmbeddedSystems 164 9.1 Integrated Development Environment 165 9.1.1 InstallingMPLAB 165 9.1.2 CreatingtheProject 167 9.1.3 ProjectBuildOptions 169 9.1.4 BuildingtheProject 169 9.2 Simulators and Debuggers 170 9.2.1 MPLABSIM 171 9.2.2 MPLABHardwareDebuggers 172 9.2.3 A“Quick-and-Dirty”Debugger 174 9.3 Programmers 174 9.4 Engineering PIC Software 175 9.4.1 UsingProgramComments 176 ProgramHeader 176 CommentedBanners 177 CommentedBitmaps 178 9.4.2 DefiningDataElements 179 ThecblockDirective 179 9.4.3 BankingTechniques 180 ThebankselDirective 180 BankSelectionMacros 180 DeprecatedBankingInstructions 181 9.4.4 ProcessorandConfigurationControls 182 ConfigurationBits 182 9.4.5 NamingConventions 184 9.4.6 ErrorlevelDirective 186 9.5 Pseudo Instructions 186
Description: