Contenido New PIC

22
Contents Preface ............................................................................................. xiii Acknowledgments ................................................................................ xv Chapter 1: Microcomputer Systems.......................................................... 1 1.1 Introduction .................................................................................................. 1 1.2 Microcontroller Systems ............................................................................... 1 1.2.1 RAM ................................................................................................. 5 1.2.2 ROM ................................................................................................. 5 1.2.3 PROM ............................................................................................... 5 1.2.4 EPROM............................................................................................. 6 1.2.5 EEPROM .......................................................................................... 6 1.2.6 Flash EEPROM ................................................................................. 6 1.3 Microcontroller Features ............................................................................... 6 1.3.1 Supply Voltage .................................................................................. 7 1.3.2 The Clock.......................................................................................... 7 1.3.3 Timers ............................................................................................... 7 1.3.4 Watchdog .......................................................................................... 8 1.3.5 Reset Input ........................................................................................ 8 1.3.6 Interrupts ........................................................................................... 8 1.3.7 Brown-out Detector ........................................................................... 9 1.3.8 Analog-to-Digital Converter ............................................................... 9 1.3.9 Serial Input-Output ............................................................................ 9 1.3.10 EEPROM Data Memory .................................................................. 10 1.3.11 LCD Drivers .................................................................................... 10 1.3.12 Analog Comparator .......................................................................... 10 1.3.13 Real-time Clock ............................................................................... 11 1.3.14 Sleep Mode ..................................................................................... 11 1.3.15 Power-on Reset ................................................................................ 11

Transcript of Contenido New PIC

Contents

Preface............................................................................................. xiii

Acknowledgments ................................................................................ xv

Chapter 1: Microcomputer Systems.......................................................... 11.1 Introduction..................................................................................................11.2 Microcontroller Systems ...............................................................................1

1.2.1 RAM .................................................................................................51.2.2 ROM .................................................................................................51.2.3 PROM...............................................................................................51.2.4 EPROM.............................................................................................61.2.5 EEPROM ..........................................................................................61.2.6 Flash EEPROM .................................................................................6

1.3 Microcontroller Features...............................................................................61.3.1 Supply Voltage ..................................................................................71.3.2 The Clock..........................................................................................71.3.3 Timers ...............................................................................................71.3.4 Watchdog ..........................................................................................81.3.5 Reset Input ........................................................................................81.3.6 Interrupts ...........................................................................................81.3.7 Brown-out Detector ...........................................................................91.3.8 Analog-to-Digital Converter ...............................................................91.3.9 Serial Input-Output ............................................................................91.3.10 EEPROM Data Memory ..................................................................101.3.11 LCD Drivers....................................................................................101.3.12 Analog Comparator..........................................................................101.3.13 Real-time Clock...............................................................................111.3.14 Sleep Mode .....................................................................................111.3.15 Power-on Reset................................................................................11

www.newnespress.com

1.3.16 Low-Power Operation ....................................................................111.3.17 Current Sink/Source Capability ......................................................111.3.18 USB Interface ................................................................................121.3.19 Motor Control Interface .................................................................121.3.20 CAN Interface ...............................................................................121.3.21 Ethernet Interface...........................................................................121.3.22 ZigBee Interface ............................................................................12

1.4 Microcontroller Architectures....................................................................121.4.1 RISC and CISC ...............................................................................13

1.5 Number Systems.......................................................................................131.5.1 Decimal Number System .................................................................141.5.2 Binary Number System....................................................................141.5.3 Octal Number System ......................................................................151.5.4 Hexadecimal Number System ..........................................................15

1.6 Converting Binary Numbers into Decimal.................................................161.7 Converting Decimal Numbers into Binary.................................................161.8 Converting Binary Numbers into Hexadecimal..........................................181.9 Converting Hexadecimal Numbers into Binary..........................................201.10 Converting Hexadecimal Numbers into Decimal .......................................211.11 Converting Decimal Numbers into Hexadecimal .......................................221.12 Converting Octal Numbers into Decimal...................................................231.13 Converting Decimal Numbers into Octal...................................................231.14 Converting Octal Numbers into Binary .....................................................241.15 Converting Binary Numbers into Octal .....................................................261.16 Negative Numbers ....................................................................................261.17 Adding Binary Numbers ...........................................................................271.18 Subtracting Binary Numbers .....................................................................291.19 Multiplication of Binary Numbers.............................................................291.20 Division of Binary Numbers .....................................................................311.21 Floating Point Numbers ............................................................................311.22 Converting a Floating Point Number into Decimal ....................................33

1.22.1 Normalizing Floating Point Numbers .............................................341.22.2 Converting a Decimal Number into Floating Point .........................341.22.3 Multiplication and Division of Floating Point Numbers ..................361.22.4 Addition and Subtraction of Floating Point Numbers ......................37

1.23 BCD Numbers ..........................................................................................381.24 Summary..................................................................................................401.25 Exercises ..................................................................................................40

Chapter 2: PIC18F Microcontroller Series .............................................. 432.1 PIC18FXX2 Architecture..........................................................................46

2.1.1 Program Memory Organization ........................................................50

www.newnespress.com

vi Contents

2.1.2 Data Memory Organization ..............................................................512.1.3 The Configuration Registers.............................................................522.1.4 The Power Supply ...........................................................................572.1.5 The Reset ........................................................................................572.1.6 The Clock Sources...........................................................................602.1.7 Watchdog Timer ..............................................................................672.1.8 Parallel I/O Ports .............................................................................682.1.9 Timers .............................................................................................742.1.10 Capture/Compare/PWM Modules (CCP) ..........................................842.1.11 Analog-to-Digital Converter (A/D) Module ......................................932.1.12 Interrupts ....................................................................................... 101

2.2 Summary.................................................................................................. 1152.3 Exercises .................................................................................................. 115

Chapter 3: C Programming Language....................................................1193.1 Structure of a mikroC Program................................................................. 120

3.1.1 Comments ..................................................................................... 1213.1.2 Beginning and Ending of a Program .............................................. 1213.1.3 Terminating Program Statements.................................................... 1213.1.4 White Spaces ................................................................................. 1223.1.5 Case Sensitivity ............................................................................. 1223.1.6 Variable Names ............................................................................. 1233.1.7 Variable Types .............................................................................. 1233.1.8 Constants ....................................................................................... 1263.1.9 Escape Sequences .......................................................................... 1283.1.10 Static Variables.............................................................................. 1293.1.11 External Variables ......................................................................... 1293.1.12 Volatile Variables .......................................................................... 1303.1.13 Enumerated Variables .................................................................... 1303.1.14 Arrays ........................................................................................... 1313.1.15 Pointers ......................................................................................... 1333.1.16 Structures ...................................................................................... 1353.1.17 Unions........................................................................................... 1383.1.18 Operators in C ............................................................................... 1393.1.19 Modifying the Flow of Control ...................................................... 1483.1.20 Mixing mikroC with Assembly Language Statements ..................... 159

3.2 PIC Microcontroller Input-Output Port Programming ................................ 1603.3 Programming Examples ............................................................................ 1613.4 Summary.................................................................................................. 1653.5 Exercises .................................................................................................. 165

www.newnespress.com

viiContents

Chapter 4: Functions and Libraries in mikroC.........................................1694.1 mikroC Functions ..................................................................................... 169

4.1.1 Function Prototypes ......................................................................... 1734.1.2 Passing Arrays to Functions............................................................. 1774.1.3 Passing Variables by Reference to Functions.................................... 1804.1.4 Variable Number of Arguments ....................................................... 1814.1.5 Function Reentrancy ........................................................................ 1844.1.6 Static Function Variables ................................................................. 184

4.2 mikroC Built-in Functions ........................................................................ 1844.3 mikroC Library Functions......................................................................... 188

4.3.1 EEPROM Library ............................................................................ 1894.3.2 LCD Library.................................................................................... 1924.3.3 Software UART Library .................................................................. 1994.3.4 Hardware USART Library ............................................................... 2044.3.5 Sound Library.................................................................................. 2064.3.6 ANSI C Library............................................................................... 2084.3.7 Miscellaneous Library...................................................................... 212

4.4 Summary.................................................................................................. 2184.5 Exercises .................................................................................................. 219

Chapter 5: PIC18 Development Tools ...................................................2215.1 Software Development Tools .................................................................... 222

5.1.1 Text Editors..................................................................................... 2225.1.2 Assemblers and Compilers............................................................... 2225.1.3 Simulators ....................................................................................... 2235.1.4 High-Level Language Simulators ..................................................... 2245.1.5 Integrated Development Environments (IDEs).................................. 224

5.2 Hardware Development Tools................................................................... 2245.2.1 Development Boards........................................................................ 2255.2.2 Device Programmers........................................................................ 2395.2.3 In-Circuit Debuggers ....................................................................... 2425.2.4 In-Circuit Emulators ........................................................................ 2455.2.5 Breadboards..................................................................................... 248

5.3 mikroC Integrated Development Environment (IDE) ................................. 2515.3.1 mikroC IDE Screen ......................................................................... 2515.3.2 Creating and Compiling a New File................................................. 2585.3.3 Using the Simulator ......................................................................... 2655.3.4 Using the mikroICD In-Circuit Debugger......................................... 2725.3.5 Using a Development Board ............................................................ 277

5.4 Summary.................................................................................................. 2855.5 Exercises .................................................................................................. 285

www.newnespress.com

viii Contents

Chapter 6: Simple PIC18 Projects ........................................................2876.1 Program Description Language (PDL) ...................................................... 288

6.1.1 START-END .................................................................................. 2886.1.2 Sequencing...................................................................................... 2886.1.3 IF-THEN-ELSE-ENDIF .................................................................. 2886.1.4 DO-ENDDO ................................................................................... 2896.1.5 REPEAT-UNTIL............................................................................. 290

Project 6.1—Chasing LEDs ............................................................................ 290Project 6.2—LED Dice ................................................................................... 295Project 6.3—Two-Dice Project........................................................................ 301Project 6.4—Two-Dice Project Using Fewer I/O Pins ..................................... 303Project 6.5—7-Segment LED Counter............................................................. 313Project 6.6—Two-Digit Multiplexed 7-Segment LED...................................... 319Project 6.7—Two-Digit Multiplexed 7-Segment LED Counter

with Timer Interrupt...................................................................................... 326Project 6.8—Voltmeter with LCD Display ...................................................... 334Project 6.9—Calculator with Keypad and LCD ............................................... 341Project 6.10—Serial Communication–Based Calculator ................................... 352

Chapter 7: Advanced PIC18 Projects—SD Card Projects .........................3717.1 The SD Card ............................................................................................ 371

7.1.1 The SPI Bus.................................................................................... 3737.1.2 Operation of the SD Card in SPI Mode ........................................... 377

7.2 mikroC Language SD Card Library Functions .......................................... 384Project 7.1—Read CID Register and Display on a PC Screen ......................... 385Project 7.2—Read/Write to SD Card Sectors................................................... 392Project 7.3—Using the Card Filing System ..................................................... 392Project 7.4—Temperature Logger ................................................................... 397

Chapter 8: Advanced PIC18 Projects—USB Bus Projects .........................4098.1 Speed Identification on the Bus ................................................................ 4138.2 USB States ............................................................................................... 4138.3 USB Bus Communication......................................................................... 414

8.3.1 Packets............................................................................................ 4148.3.2 Data Flow Types............................................................................. 4168.3.3 Enumeration.................................................................................... 417

8.4 Descriptors ............................................................................................... 4188.4.1 Device Descriptors .......................................................................... 4188.4.2 Configuration Descriptors................................................................ 4218.4.3 Interface Descriptors ....................................................................... 4238.4.4 HID Descriptors .............................................................................. 4258.4.5 Endpoint Descriptors ....................................................................... 426

www.newnespress.com

ixContents

8.5 PIC18 Microcontroller USB Bus Interface ................................................ 4278.6 mikroC Language USB Bus Library Functions ......................................... 429Project 8.1—USB-Based Microcontroller Output Port ..................................... 430Project 8.2—USB-Based Microcontroller Input/Output .................................... 456Project 8.3—USB-Based Ambient Pressure Display on the PC........................ 464

Chapter 9: Advanced PIC18 Projects—CAN Bus Projects ........................4759.1 Data Frame............................................................................................. 481

9.1.1 Start of Frame (SOF) .................................................................... 4829.1.2 Arbitration Field............................................................................ 4829.1.3 Control Field................................................................................. 4849.1.4 Data Field ..................................................................................... 4849.1.5 CRC Field..................................................................................... 4849.1.6 ACK Field .................................................................................... 485

9.2 Remote Frame ........................................................................................ 4859.3 Error Frame............................................................................................ 4859.4 Overload Frame...................................................................................... 4859.5 Bit Stuffing ............................................................................................ 4869.6 Types of Errors ...................................................................................... 4869.7 Nominal Bit Timing ............................................................................... 4869.8 PIC Microcontroller CAN Interface ........................................................ 4899.9 PIC18F258 Microcontroller..................................................................... 491

9.9.1 Configuration Mode ...................................................................... 4939.9.2 Disable Mode................................................................................ 4939.9.3 Normal Operation Mode................................................................ 4939.9.4 Listen-only Mode .......................................................................... 4939.9.5 Loop-Back Mode .......................................................................... 4949.9.6 Error Recognition Mode................................................................ 4949.9.7 CAN Message Transmission.......................................................... 4949.9.8 CAN Message Reception............................................................... 4949.9.9 Calculating the Timing Parameters ................................................ 496

9.10 mikroC CAN Functions .......................................................................... 4989.10.1 CANSetOperationMode ............................................................... 4999.10.2 CANGetOperationMode .............................................................. 5009.10.3 CANInitialize .............................................................................. 5009.10.4 CANSetBaudRate ........................................................................ 5019.10.5 CANSetMask .............................................................................. 5019.10.6 CANSetFilter .............................................................................. 5029.10.7 CANRead.................................................................................... 5029.10.8 CANWrite................................................................................... 503

9.11 CAN Bus Programming .......................................................................... 504Project 9.1—Temperature Sensor CAN Bus Project ........................................ 504

www.newnespress.com

x Contents

Chapter 10: Multi-Tasking and Real-Time Operating Systems....................51510.1 State Machines ....................................................................................... 51610.2 The Real-Time Operating System (RTOS) .............................................. 518

10.2.1 The Scheduler.............................................................................. 51810.3 RTOS Services ....................................................................................... 52110.4 Synchronization and Messaging Tools .................................................... 52110.5 CCS PIC C Compiler RTOS................................................................... 522

10.5.1 Preparing for RTOS .................................................................... 52310.5.2 Declaring a Task ......................................................................... 524

Project 10.1—LEDs........................................................................................ 524Project 10.2—Random Number Generator....................................................... 528Project 10.3—Voltmeter with RS232 Serial Output ......................................... 532

Index...............................................................................................541

www.newnespress.com

xiContents

Contents

About the Authors .........................................................................................................xiii

Section I. An Introduction to PIC Microcontrollers ..............................................................1

Chapter 1. The PIC Microcontroller Family .............................................................................. 31.1 12-bit Instruction Word ................................................................................................. 61.2 14-bit Instruction Word ................................................................................................. 71.3 16-bit Instruction Word ............................................................................................... 111.4 Inside a PIC Microcontroller ....................................................................................... 12

Chapter 2. Introducing the PIC® 16 Series and the 16F84A ................................................... 392.1 The Main Idea—the PIC 16 Series Family ................................................................. 392.2 An Architecture Overview of the 16F84A .................................................................. 422.3 A Review of Memory Technologies ............................................................................ 442.4 The 16F84A Memory .................................................................................................. 462.5 Some Issues of Timing ................................................................................................ 512.6 Power-Up and Reset .................................................................................................... 542.7 What Others Do—the Atmel AT89C2051 .................................................................. 552.8 Taking Things Further—the 16F84A On-Chip Reset Circuit ..................................... 562.9 Summary ..................................................................................................................... 59References .......................................................................................................................... 59

Chapter 3. Parallel Ports, Power Supply and the Clock Oscillator .......................................... 613.1 The Main Idea—Parallel Input/Output ........................................................................ 623.2 The Technical Challenge of Parallel Input/Output ...................................................... 623.3 Connecting to the Parallel Port .................................................................................... 683.4 The PIC 16F84A Parallel Ports ................................................................................... 713.5 The Clock Oscillator ................................................................................................... 743.6 Power Supply............................................................................................................... 783.7 The Hardware Design of the Electronic Ping-Pong .................................................... 803.8 Summary ..................................................................................................................... 82References .......................................................................................................................... 82

Section II. Programming PIC Microcontrollers Using Assembly Language ............................83

Chapter 4. Starting to Program—An Introduction to Assembler ............................................ 85 4.1 The Main Idea—What Programs Do and How We Develop Them .......................... 86 4.2 The PIC 16 Series Instruction Set, with a Little More on the ALU .......................... 89 4.3 Assemblers and Assembler Format ........................................................................... 92 4.4 Creating Simple Programs ......................................................................................... 94 4.5 Adopting a Development Environment ..................................................................... 97 4.6 An Introductory MPLAB Tutorial ............................................................................. 99 4.7 An Introduction to Simulation ................................................................................. 103 4.8 Downloading the Program to a Microcontroller ..................................................... 106 4.9 What Others Do—A Brief Comparison of CISC and RISC Instruction Sets ......... 1084.10 Taking Things Further—The 16 Series Instruction Set Format .............................. 1094.11 Summary ................................................................................................................. 110References ........................................................................................................................ 110

Chapter 5. Building Assembler Programs ............................................................................. 111 5.1 The Main Idea—Building Structured Programs ..................................................... 111 5.2 Flow Control—Branching and Subroutines ............................................................ 114 5.3 Generating Time Delays and Intervals .................................................................... 118 5.4 Dealing with Data .................................................................................................... 120 5.5 Introducing Logical Instructions ............................................................................. 125 5.6 Introducing Arithmetic Instructions and the Carry Flag ......................................... 125 5.7 Taming Assembler Complexity ............................................................................... 130 5.8 More Use of the MPLAB Simulator ....................................................................... 132 5.9 The Ping-Pong Program .......................................................................................... 1365.10 Simulating the Ping-Pong Program—Tutorial ........................................................ 1405.11 What Others Do—Graphical Simulators ................................................................. 1435.12 Summary ................................................................................................................. 143References ........................................................................................................................ 144

Chapter 6. Further Programming Techniques ........................................................................ 145 6.1 Program Timing ....................................................................................................... 145 6.2 Hardware Counter/Timer ......................................................................................... 147 6.3 Interrupts ................................................................................................................. 152 6.4 More Register Operations........................................................................................ 158 6.5 Special Features ....................................................................................................... 163 6.6 Program Data Table ................................................................................................. 167 6.7 Assembler Directives ............................................................................................... 170 6.8 Special Instructions ................................................................................................. 173 6.9 Numerical Types ...................................................................................................... 1746.10 Summary ................................................................................................................. 175

vi Contents

www.newnespress.com

www.newnespress.com

Chapter 7. Prototype Hardware .............................................................................................. 177 7.1 Hardware Design ..................................................................................................... 177 7.2 Hardware Construction ............................................................................................ 178 7.3 Demo Board ............................................................................................................ 183 7.4 Demo Board Applications ....................................................................................... 186 7.5 Summary ................................................................................................................. 198

Chapter 8. More PIC Applications and Devices .................................................................... 199 8.1 16F877 Application ................................................................................................. 199 8.2 16F818 Application ................................................................................................. 219 8.3 12F675 Application ................................................................................................. 220 8.4 18F452 Application ................................................................................................. 221 8.5 Summary ................................................................................................................. 226

Chapter 9. The PIC12F50x Series (8-pin PIC Microcontrollers) .......................................... 227 9.1 Differences from the PIC16F54 .............................................................................. 227 9.2 Example Project: PIC Dice ...................................................................................... 231

Chapter 10. Intermediate Operations Using the PIC12F675 ................................................. 23710.1 The Inner Differences .............................................................................................. 23810.2 Interrupts ................................................................................................................. 24210.3 EEPROM ................................................................................................................. 25210.4 Analog to Digital Conversion .................................................................................. 25910.5 Comparator Module ................................................................................................ 26410.6 Final Project: Intelligent Garden Lights .................................................................. 270

Chapter 11. Using Inputs ....................................................................................................... 27511.1 Switch Flowchart ..................................................................................................... 27711.2 Program Development ............................................................................................. 27811.3 Scanning (Using Multiple Inputs) ........................................................................... 28311.4 Switch Scanning ...................................................................................................... 28311.5 Control Application—A Hot Air Blower ................................................................ 287

Chapter 12. Keypad Scanning ................................................................................................ 29112.1 Programming Example for the Keypad ................................................................... 291

Chapter 13. Program Examples ............................................................................................. 30713.1 Counting Events ...................................................................................................... 30713.2 Look-Up Table ......................................................................................................... 31113.3 7-Segment Display .................................................................................................. 31113.4 Numbers Larger than 255 ........................................................................................ 32113.5 Long Time Intervals ................................................................................................ 32713.6 One Hour Delay ....................................................................................................... 330

Contents vii

www.newnespress.com

Section III. Programming PIC Microcontrollers Using PicBasic .........................................333

Chapter 14. PicBasic and PicBasic Pro Programming ........................................................... 33514.1 PicBasic Language .................................................................................................. 33514.2 PicBasic Pro Language ............................................................................................ 35714.3 Liquid Crystal Display (LCD) Interface and Commands ....................................... 36914.4 Interrupts ................................................................................................................. 38014.5 Recommended PicBasic Pro Program Structure ..................................................... 38114.6 Using Stepping Motors ............................................................................................ 38114.7 Using Servomotors .................................................................................................. 384

Chapter 15. Simple PIC Projects ........................................................................................... 38715.1 Project #1—Flashing an LED ................................................................................. 38715.2 Project #2—Scrolling LEDs .................................................................................... 39115.3 Project #3—Driving a 7-Segment LED Display ..................................................... 397

Chapter 16. Moving On with the 16F876 .............................................................................. 40516.1 Project #4—Accessing Port A I/O .......................................................................... 40516.2 Project #5—Analog-to-Digital Conversion ............................................................. 41216.3 Project #6—Driving a Servomotor .......................................................................... 421

Chapter 17. Communication .................................................................................................. 42917.1 Project #7—Driving an LCD Module ..................................................................... 42917.2 Project #8—Serial Communication ......................................................................... 43917.3 Project #9—Driving an LCD with a Single Serial Connection ............................... 447

Section IV. Programming PIC Microcontrollers Using MBasic...........................................463

Chapter 18. MBasic Compiler and Development Boards ...................................................... 46518.1 The Compiler Package ............................................................................................ 46518.2 BASIC and Its Essentials ......................................................................................... 46718.3 Development Boards ............................................................................................... 47018.4 Programming Style .................................................................................................. 47318.5 Building the Circuits and Standard Assumptions .................................................... 47518.6 Pins, Ports and Input/Output ................................................................................... 47618.7 Pseudo-Code and Planning the Program ................................................................. 48518.8 Inside the Compiler ................................................................................................. 487References ........................................................................................................................ 491

Chapter 19. The Basics—Output ........................................................................................... 49319.1 Pin Architectures ..................................................................................................... 49419.2 LED Indicators ........................................................................................................ 49819.3 Switching Inductive Loads ...................................................................................... 503

viii Contents

www.newnespress.com

19.4 Low Side Switching ................................................................................................ 50619.5 Isolated Switching ................................................................................................... 52419.6 Fast Switching—Sound from a PIC ........................................................................ 533References ........................................................................................................................ 536

Chapter 20. The Basics—Digital Input .................................................................................. 53920.1 Introduction ............................................................................................................. 53920.2 Switch Bounce and Sealing Current ........................................................................ 54820.3 Hardware Debouncing ............................................................................................. 54920.4 Software Debouncing .............................................................................................. 55120.5 Isolated Switching ................................................................................................... 55520.6 Reading a Keypad .................................................................................................... 557Reference .......................................................................................................................... 562

Chapter 21. Introductory Stepper Motors .............................................................................. 56321.1 Stepper Motor Basics .............................................................................................. 56321.2 Programs .................................................................................................................. 586References ........................................................................................................................ 613

Chapter 22. Digital Temperature Sensors and Real-Time Clocks ......................................... 61522.1 DS18B20 Temperature Sensor ................................................................................ 61522.2 Reading Multiple Sensors on the Same Bus ........................................................... 62822.3 DS1302 Real-Time Clock ....................................................................................... 63322.4 Combination Date, Time and Temperature ............................................................. 64722.5 Ideas for Modifi cations to Programs and Circuits ................................................... 653References ........................................................................................................................ 656

Chapter 23. Infrared Remote Controls ................................................................................... 65723.1 Common Encoding Standards ................................................................................. 65923.2 IR Receiver .............................................................................................................. 66123.3 Characterizing Wide/Narrow Pulse Intervals .......................................................... 66423.4 Decoding a REC-80 Controller .............................................................................. 68023.5 Ideas for Modifi cations to Programs and Circuits .................................................. 693References ........................................................................................................................ 694

Section V. Programming PIC Microcontrollers Using C ....................................................695

Chapter 24. Getting Started .................................................................................................... 69724.1 The Plan ................................................................................................................... 69724.2 Checklist .................................................................................................................. 69724.3 Coding ..................................................................................................................... 69824.4 Review ..................................................................................................................... 707

Contents ix

www.newnespress.com

Books ................................................................................................................................ 710Links ................................................................................................................................. 710

Chapter 25. Programming Loops ........................................................................................... 71125.1 The Plan ................................................................................................................... 71125.2 Checklist .................................................................................................................. 71125.3 Coding ..................................................................................................................... 71225.4 Using the Logic Analyzer ........................................................................................ 71925.5 Review ..................................................................................................................... 720Books ................................................................................................................................ 723Links ................................................................................................................................. 723

Chapter 26. More Pattern Work, More Loops ....................................................................... 72526.1 The Plan ................................................................................................................... 72526.2 Checklist .................................................................................................................. 72526.3 Coding ..................................................................................................................... 72526.4 Testing with the Logic Analyzer ............................................................................. 73226.5 Using the Explorer16 Demonstration Board ........................................................... 73426.6 Review ..................................................................................................................... 734Books ................................................................................................................................ 736Links ................................................................................................................................. 736

Chapter 27. NUMB3RS ......................................................................................................... 73727.1 The Plan ................................................................................................................... 73727.2 Checklist .................................................................................................................. 73727.3 Coding ..................................................................................................................... 73727.4 Notes for C Experts ................................................................................................. 74227.5 Measuring Performance .......................................................................................... 74327.6 Review ..................................................................................................................... 746Links ................................................................................................................................. 749

Chapter 28. Interrupts ............................................................................................................ 75128.1 The Plan ................................................................................................................... 75128.2 Checklist .................................................................................................................. 75128.3 Coding ..................................................................................................................... 75128.4 Managing Multiple Interrupts ................................................................................. 76428.5 Review ..................................................................................................................... 765Books ................................................................................................................................ 768Links ................................................................................................................................. 768

Chapter 29. Taking a Look Under the Hood .......................................................................... 76929.1 The Plan ................................................................................................................... 76929.2 Checklist .................................................................................................................. 769

x Contents

www.newnespress.com

29.3 Coding ..................................................................................................................... 76929.4 Review ..................................................................................................................... 783Books ................................................................................................................................ 785Links ................................................................................................................................. 785

Section IV. Appendices ................................................................................................787

Appendix A. The PIC® 16 Series Instruction Set. ................................................................. 789

Appendix B. The Electronic Ping-Pong. ............................................................................... 791

Appendix C. DIZI-2 Board and Lock Application. ............................................................... 797

Appendix D. Program M. ....................................................................................................... 821

Appendix E. Program N ........................................................................................................ 827

Appendix F. Program O ........................................................................................................ 831

Appendix G. Program P ......................................................................................................... 835

Appendix H. Program Q ........................................................................................................ 839

Appendix I. Useful PIC Data ................................................................................................ 845

Appendix J. PIC 16F84A Data Sheet ................................................................................... 859

Index ....................................................................................................................................... 903

Contents xi

v

CONTENTS

Introduction xi

Acknowledgments xxi

Chapter 1 Embedded Microcontrollers 1Microcontroller Types 1Internal Hardware 2Applications 5Processor Architectures 7Instructions and Software 12Peripheral Functions 17Memory Types 21Microcontroller Communication 28Device Packaging 35Application Development Tools 39

Chapter 2 The Microchip PIC Microcontroller 43Accessing the Microchip Web Site 43PIC Microcontroller Feature Summary 48Features Unique to the PIC Microcontroller 54PIC Microcontroller Families 59

Chapter 3 Software Development Tools 63Tools Overview 65High Level Languages 83Microchip MPLAB IDE 103

Chapter 4 Programming PIC Microcontrollers 155Hex File Format 156Code Protection 158Parallel Programming 159PIC ICSP Programmer Interface 166Microchip Programmers 178My Programmers 181Third-Party Programmers 204

Chapter 5 Emulators and Debuggers 207MPLAB ICE-2000 210MPLAB REAL ICE 213

For more information about this title, click here

vi CONTENTS

MPLAB ICD 2 Debugger 214The Emu-II 219Other Emulators 241

Chapter 6 The Microchip PIC MCU Processor Architecture 243The CPU 244Hardware and File Registers 248The PIC Microcontroller’s ALU 254Data Movement 260The Program Counter and Stack 264Reset 268Interrupts 271Architecture Differences 273

Chapter 7 Using the PIC MCU Instruction Set 293Setting Up the MPLAB IDE Simulator with a Test Template 294PIC MCU Instruction Types 297The Mid-Range Instruction Set 303Low-End PIC Microcontroller Instruction Set 348PIC18 Instruction Set 356

Chapter 8 Assembly-Language Software Techniques 373Sample Template 374Labels, Addresses, and Flags 376Subroutines with Parameter Passing 381Subtraction, Comparing and Negation 385Bit AND and OR 38916-Bit Operations 390MulDiv, Constant Multiplication and Division 392Delays 400Patch Space 405Structures, Pointers, and Arrays 407Sorting Data 414Interrupts 419Reentrant Subroutines 423Simulating Logic 423Event-Driven Programming 426State Machine Programming 429Porting Code Between PIC Microcontroller Device Architectures 430Optimizing PIC Microcontroller Applications 438A Baker’s Dozen Rules to Follow That Will Help to Avoid Application SoftwareProblems 443

Chapter 9 Basic Operating Features 445Power Input and Decoupling 446Configuration Fuses 451OPTION Register 470TMR0 478Interrupt Operation 483The Right PIC Microcontroller to Learn On 485

CONTENTS vii

Chapter 10 Macro Development 489PIC Microcontroller Assembly-Language Macros 489The Difference Between Defines and Macros 492The Assembler Calculator 494Multiline C Macros 499Conditional Assembly/Compilation 500Using Defines and Conditional Assembly for Application Debug 507Debugging Macros 509Structured Programming Macros 513

Chapter 11 Building and Linking 519Creating Linked Applications 519

Chapter 12 Bootloaders 527Bootloader Requirements 528Mid-Range Bootloaders 530PIC18 Bootloaders 535

Chapter 13 Real-Time Operating Systems 537Low-End and Mid-Range RTOSs 541PIC18 RTOS Design 542

Chapter 14 Debugging Your Applications 565Document the Expected State 566Characterize the Problem 567Hypothesize and Test Your Hypothesis 569Propose Corrective Actions 571Test Fixes 572Release Your Solution 576Debug: An Application to Test Your Debug Skills 577

Chapter 15 PIC Microcontroller Application Designand Hardware Interfacing 589

Requirements Definition 590PIC Microcontroller Resource Allocation 595Effective User Interfacing 597Project Management 599Power Management 603Reset 608Interfacing to External Devices 611

Chapter 16 PIC MCU Optional Hardware Features 617Mid-Range Built-in EEPROM/Flash Access 618TMR1 624TMR2 626Compare/Capture/PWM (CCP) Module 628Serial I/O 633Analog I/O 649Parallel Slave Port (PSP) 657In-Circuit Serial Programming (ICSP) 659

viii CONTENTS

Chapter 17 PIC MCU Input and Output Device Interfacing 661LEDs 661Switch Bounce 665Matrix Keypads 668LCDs 672Analog I/O 682Audio Output 690Relays and Solenoids 692Asynchronous (NRZ) Serial Interfaces 693Synchronous Serial Interfaces 704

Chapter 18 Motor Control 711Dc Motors 711Stepper Motors 724R/C Servo Control 733

Chapter 19 Practical PC Interfacing 739PC Software Application Development Tools 740Serial Port 742Parallel Port 749

Chapter 20 PIC Microcontroller Application Basics 755Jumping Around 755Some Basic Functions 771Analog Input/Output 798I/O with Interrupts 810Serial I/O 832

Chapter 21 Projects 853Low-End Devices 853Mid-Range Devices 878PIC18 Devices 953

Appendix A Resources 965Microchip 965Books to Help You Learn Moreabout the PIC Microcontroller 966Useful Books 967Recommended PIC Microcontroller Websites 970Periodicals 971Other Websites of Interest 972Part Suppliers 973

Appendix B PIC Microcontroller Summary 977Feature to Part Number Table 977Instruction Sets 977I/O Register Addresses 1016Device Pinouts 1030

CONTENTS ix

Appendix C Useful Tables and Data 1061Electrical Engineering Formulas 1063Mathematical Formulas 1065Mathematical Conversions 1066ASCII 1067

Appendix D Miscellaneous Electronic Reference Information 1073Basic Electronic Components and Their Symbols 1073Test Equipment 1080

Appendix E Basic Programming Language 1089PICBASIC 1091

Appendix F C Programming Language 1123Common Library Functions 1130PICC Library Functions 1133Microchip C18 Library Functions 1138

Appendix G Reuse, Return, and Recycle 1149Useful Snippets 1150Mykemacs.inc 1160Sixteen-Bit Numbers 1200

Glossary 1213

Index 1229

Contents

Acknowledgements ixPreface to the third edition xi

1 Introduction 1Some tips before starting 2

Binary, decimal and hexadecimal 2An 8-bit system 5

Initial steps 5Choosing your PIC microcontroller 6Writing 10Assembling 10

The file registers 10A program template 13

2 Exploring the PIC5x series 20Your first program 20

Configuration bits 22Testing the program 23

Simulating 23Emulating 23Blowing the PIC microcontroller 24

Hardware 24Using the testing instructions 29Timing 32Seven-segment displays 44The program counter 46Subroutines and the stack 48Logic gates 65The watchdog timer 69Final instructions 73The STATUS file register 74The carry and digit carry flags 75Pages 76What caused the reset? 79Indirect addressing 80Some useful (but not vital) tricks 82Final PIC5x program – ‘Bike buddy’ 85

3 The PIC12F50x series (8-pin PIC microcontrollers) 90Differences from the PIC16F54 90

The STATUS register 90The OSCCAL register 91Inputs and outputs 92The OPTION register 92The TRIS register 93The general purpose file registers 93The MCLR 93Configuration bits 93

Example project: ‘PIC dice’ 94Random digression 95

4 Intermediate operations using the PIC12F675 100The inner differences 101

The OPTION and WPU registers 102The TRISIO register 103Calibrating the internal oscillator 103PCLATH: Higher bits of the program counter 104Remaining differences 105

Interrupts 105INTCON 106The interrupt service routine 107Interrupts during sleep 109Maintaining the STATUS quo 109New program template 110Example project: ‘Quiz game controller’ 112

EEPROM 116EECON1 116Reading from the EEPROM 116Writing to the EEPROM 117Example project: ‘Telephone card chip’ 118Further EEPROM examples: Music maker 122Power monitor 122

Analogue to digital conversion 122ADCON0 123ANSEL: Analogue select register 124A/D conversion interrupt 125Example project: ‘Bath monitor’ 125

Comparator module 129Voltage reference 130Comparator interrupts 130Comparator example: ‘Sun follower’ 131Comparator example: Reading many buttons from one pin 132

Final project: Intelligent garden lights 134

vi Contents

5 Advanced operations and the future 138Extra timers: TMR1 & … 138Capture/Compare/PWM 139USART: Serial communication 140Programming tips 142

6 A PIC development environment 143

7 Sample programs 145Program A LedOn – Turns an LED on 145Program B PushButton (1.0) – If a push button is pressed,

turns on an LED 146Program C PushButton (2.0) – Shorter version of PushButton 1.0 147Program D Timing – LED states toggled every second, and buzzer

on every five seconds 148Program E Traffic – Pedestrian traffic lights junction is simulated 150Program F Counter (1.0) – Counts signals from a push button,

resets after 16 152Program G Counter (2.0) – Stop reading button twice (otherwise,

as Counter 1.0) 154Program H Counter (3.0) – Solves button bounce (otherwise, as

Counter 2.0) 156Program I StopClock – A stop clock displaying tenths of

seconds to minutes 158Program J LogicGates – Acts as the eight different gates 162Program K Alarm – An alarm system which can be set or disabled 164Program L BikeBuddy – A speedometer and mileometer for bikes 165Program M PIC Dice – A pair of dice are simulated 171Program N Quiz – Indicates which of three push buttons has been

pressed first 175Program O Phonecard – To act like a phonecard which decrements

a file register 177Program P TempSense – Displays whether temperature is too

hot, too cold or OK 181Program Q 183

Appendix A Specifications of some Flash PIC microcontrollers 189Appendix B Pin layouts of some Flash PIC microcontrollers 191Appendix C Instructions glossary 192Appendix D Number system conversion 195Appendix E Bit assignments of various file registers 196Appendix F If all else fails, read this 203Appendix G Contacts and further reading 204Appendix H PICKit™ 1 & BFMP Info 205Appendix I Answers to the exercises 207Appendix J Some BASIC commands in assembly 222

Index 223

Contents vii