Sunteți pe pagina 1din 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

www.newnespress.com
vi Contents

1.3.16 Low-Power Operation ....................................................................11


1.3.17 Current Sink/Source Capability ......................................................11
1.3.18 USB Interface ................................................................................12
1.3.19 Motor Control Interface .................................................................12
1.3.20 CAN Interface ...............................................................................12
1.3.21 Ethernet Interface...........................................................................12
1.3.22 ZigBee Interface ............................................................................12
1.4 Microcontroller Architectures.................................................................... 12
1.4.1 RISC and CISC ...............................................................................13
1.5 Number Systems....................................................................................... 13
1.5.1 Decimal Number System .................................................................14
1.5.2 Binary Number System ....................................................................14
1.5.3 Octal Number System ......................................................................15
1.5.4 Hexadecimal Number System ..........................................................15
1.6 Converting Binary Numbers into Decimal................................................. 16
1.7 Converting Decimal Numbers into Binary................................................. 16
1.8 Converting Binary Numbers into Hexadecimal.......................................... 18
1.9 Converting Hexadecimal Numbers into Binary.......................................... 20
1.10 Converting Hexadecimal Numbers into Decimal ....................................... 21
1.11 Converting Decimal Numbers into Hexadecimal ....................................... 22
1.12 Converting Octal Numbers into Decimal................................................... 23
1.13 Converting Decimal Numbers into Octal................................................... 23
1.14 Converting Octal Numbers into Binary ..................................................... 24
1.15 Converting Binary Numbers into Octal ..................................................... 26
1.16 Negative Numbers .................................................................................... 26
1.17 Adding Binary Numbers ........................................................................... 27
1.18 Subtracting Binary Numbers ..................................................................... 29
1.19 Multiplication of Binary Numbers............................................................. 29
1.20 Division of Binary Numbers ..................................................................... 31
1.21 Floating Point Numbers ............................................................................ 31
1.22 Converting a Floating Point Number into Decimal .................................... 33
1.22.1 Normalizing Floating Point Numbers .............................................34
1.22.2 Converting a Decimal Number into Floating Point .........................34
1.22.3 Multiplication and Division of Floating Point Numbers ..................36
1.22.4 Addition and Subtraction of Floating Point Numbers ......................37
1.23 BCD Numbers .......................................................................................... 38
1.24 Summary.................................................................................................. 40
1.25 Exercises .................................................................................................. 40

Chapter 2: PIC18F Microcontroller Series .............................................. 43


2.1 PIC18FXX2 Architecture.......................................................................... 46
2.1.1 Program Memory Organization ........................................................50

www.newnespress.com
Contents vii

2.1.2 Data Memory Organization ..............................................................51


2.1.3 The Configuration Registers.............................................................52
2.1.4 The Power Supply ...........................................................................57
2.1.5 The Reset ........................................................................................57
2.1.6 The Clock Sources...........................................................................60
2.1.7 Watchdog Timer ..............................................................................67
2.1.8 Parallel I/O Ports .............................................................................68
2.1.9 Timers .............................................................................................74
2.1.10 Capture/Compare/PWM Modules (CCP) ..........................................84
2.1.11 Analog-to-Digital Converter (A/D) Module ......................................93
2.1.12 Interrupts ....................................................................................... 101
2.2 Summary.................................................................................................. 115
2.3 Exercises .................................................................................................. 115

Chapter 3: C Programming Language....................................................119


3.1 Structure of a mikroC Program................................................................. 120
3.1.1 Comments ..................................................................................... 121
3.1.2 Beginning and Ending of a Program .............................................. 121
3.1.3 Terminating Program Statements.................................................... 121
3.1.4 White Spaces ................................................................................. 122
3.1.5 Case Sensitivity ............................................................................. 122
3.1.6 Variable Names ............................................................................. 123
3.1.7 Variable Types .............................................................................. 123
3.1.8 Constants ....................................................................................... 126
3.1.9 Escape Sequences .......................................................................... 128
3.1.10 Static Variables.............................................................................. 129
3.1.11 External Variables ......................................................................... 129
3.1.12 Volatile Variables .......................................................................... 130
3.1.13 Enumerated Variables .................................................................... 130
3.1.14 Arrays ........................................................................................... 131
3.1.15 Pointers ......................................................................................... 133
3.1.16 Structures ...................................................................................... 135
3.1.17 Unions........................................................................................... 138
3.1.18 Operators in C ............................................................................... 139
3.1.19 Modifying the Flow of Control ...................................................... 148
3.1.20 Mixing mikroC with Assembly Language Statements ..................... 159
3.2 PIC Microcontroller Input-Output Port Programming ................................ 160
3.3 Programming Examples ............................................................................ 161
3.4 Summary.................................................................................................. 165
3.5 Exercises .................................................................................................. 165

www.newnespress.com
viii Contents

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


4.1 mikroC Functions ..................................................................................... 169
4.1.1 Function Prototypes ......................................................................... 173
4.1.2 Passing Arrays to Functions............................................................. 177
4.1.3 Passing Variables by Reference to Functions.................................... 180
4.1.4 Variable Number of Arguments ....................................................... 181
4.1.5 Function Reentrancy ........................................................................ 184
4.1.6 Static Function Variables ................................................................. 184
4.2 mikroC Built-in Functions ........................................................................ 184
4.3 mikroC Library Functions......................................................................... 188
4.3.1 EEPROM Library ............................................................................ 189
4.3.2 LCD Library.................................................................................... 192
4.3.3 Software UART Library .................................................................. 199
4.3.4 Hardware USART Library ............................................................... 204
4.3.5 Sound Library.................................................................................. 206
4.3.6 ANSI C Library............................................................................... 208
4.3.7 Miscellaneous Library...................................................................... 212
4.4 Summary.................................................................................................. 218
4.5 Exercises .................................................................................................. 219

Chapter 5: PIC18 Development Tools ...................................................221


5.1 Software Development Tools .................................................................... 222
5.1.1 Text Editors..................................................................................... 222
5.1.2 Assemblers and Compilers............................................................... 222
5.1.3 Simulators ....................................................................................... 223
5.1.4 High-Level Language Simulators ..................................................... 224
5.1.5 Integrated Development Environments (IDEs).................................. 224
5.2 Hardware Development Tools................................................................... 224
5.2.1 Development Boards........................................................................ 225
5.2.2 Device Programmers........................................................................ 239
5.2.3 In-Circuit Debuggers ....................................................................... 242
5.2.4 In-Circuit Emulators ........................................................................ 245
5.2.5 Breadboards..................................................................................... 248
5.3 mikroC Integrated Development Environment (IDE) ................................. 251
5.3.1 mikroC IDE Screen ......................................................................... 251
5.3.2 Creating and Compiling a New File................................................. 258
5.3.3 Using the Simulator ......................................................................... 265
5.3.4 Using the mikroICD In-Circuit Debugger......................................... 272
5.3.5 Using a Development Board ............................................................ 277
5.4 Summary.................................................................................................. 285
5.5 Exercises .................................................................................................. 285

www.newnespress.com
Contents ix

Chapter 6: Simple PIC18 Projects ........................................................287


6.1 Program Description Language (PDL) ...................................................... 288
6.1.1 START-END .................................................................................. 288
6.1.2 Sequencing...................................................................................... 288
6.1.3 IF-THEN-ELSE-ENDIF .................................................................. 288
6.1.4 DO-ENDDO ................................................................................... 289
6.1.5 REPEAT-UNTIL............................................................................. 290
Project 6.1—Chasing LEDs ............................................................................ 290
Project 6.2—LED Dice ................................................................................... 295
Project 6.3—Two-Dice Project........................................................................ 301
Project 6.4—Two-Dice Project Using Fewer I/O Pins ..................................... 303
Project 6.5—7-Segment LED Counter............................................................. 313
Project 6.6—Two-Digit Multiplexed 7-Segment LED...................................... 319
Project 6.7—Two-Digit Multiplexed 7-Segment LED Counter
with Timer Interrupt...................................................................................... 326
Project 6.8—Voltmeter with LCD Display ...................................................... 334
Project 6.9—Calculator with Keypad and LCD ............................................... 341
Project 6.10—Serial Communication–Based Calculator ................................... 352
Chapter 7: Advanced PIC18 Projects—SD Card Projects .........................371
7.1 The SD Card ............................................................................................ 371
7.1.1 The SPI Bus.................................................................................... 373
7.1.2 Operation of the SD Card in SPI Mode ........................................... 377
7.2 mikroC Language SD Card Library Functions .......................................... 384
Project 7.1—Read CID Register and Display on a PC Screen ......................... 385
Project 7.2—Read/Write to SD Card Sectors................................................... 392
Project 7.3—Using the Card Filing System ..................................................... 392
Project 7.4—Temperature Logger ................................................................... 397

Chapter 8: Advanced PIC18 Projects—USB Bus Projects .........................409


8.1 Speed Identification on the Bus ................................................................ 413
8.2 USB States ............................................................................................... 413
8.3 USB Bus Communication......................................................................... 414
8.3.1 Packets............................................................................................ 414
8.3.2 Data Flow Types............................................................................. 416
8.3.3 Enumeration.................................................................................... 417
8.4 Descriptors ............................................................................................... 418
8.4.1 Device Descriptors .......................................................................... 418
8.4.2 Configuration Descriptors................................................................ 421
8.4.3 Interface Descriptors ....................................................................... 423
8.4.4 HID Descriptors .............................................................................. 425
8.4.5 Endpoint Descriptors ....................................................................... 426

www.newnespress.com
x Contents

8.5 PIC18 Microcontroller USB Bus Interface ................................................ 427


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

Chapter 9: Advanced PIC18 Projects—CAN Bus Projects ........................475


9.1 Data Frame............................................................................................. 481
9.1.1 Start of Frame (SOF) .................................................................... 482
9.1.2 Arbitration Field............................................................................ 482
9.1.3 Control Field................................................................................. 484
9.1.4 Data Field ..................................................................................... 484
9.1.5 CRC Field..................................................................................... 484
9.1.6 ACK Field .................................................................................... 485
9.2 Remote Frame ........................................................................................ 485
9.3 Error Frame............................................................................................ 485
9.4 Overload Frame...................................................................................... 485
9.5 Bit Stuffing ............................................................................................ 486
9.6 Types of Errors ...................................................................................... 486
9.7 Nominal Bit Timing ............................................................................... 486
9.8 PIC Microcontroller CAN Interface ........................................................ 489
9.9 PIC18F258 Microcontroller..................................................................... 491
9.9.1 Configuration Mode ...................................................................... 493
9.9.2 Disable Mode................................................................................ 493
9.9.3 Normal Operation Mode................................................................ 493
9.9.4 Listen-only Mode .......................................................................... 493
9.9.5 Loop-Back Mode .......................................................................... 494
9.9.6 Error Recognition Mode................................................................ 494
9.9.7 CAN Message Transmission.......................................................... 494
9.9.8 CAN Message Reception............................................................... 494
9.9.9 Calculating the Timing Parameters ................................................ 496
9.10 mikroC CAN Functions .......................................................................... 498
9.10.1 CANSetOperationMode ............................................................... 499
9.10.2 CANGetOperationMode .............................................................. 500
9.10.3 CANInitialize .............................................................................. 500
9.10.4 CANSetBaudRate ........................................................................ 501
9.10.5 CANSetMask .............................................................................. 501
9.10.6 CANSetFilter .............................................................................. 502
9.10.7 CANRead.................................................................................... 502
9.10.8 CANWrite................................................................................... 503
9.11 CAN Bus Programming .......................................................................... 504
Project 9.1—Temperature Sensor CAN Bus Project ........................................ 504

www.newnespress.com
Contents xi

Chapter 10: Multi-Tasking and Real-Time Operating Systems....................515


10.1 State Machines ....................................................................................... 516
10.2 The Real-Time Operating System (RTOS) .............................................. 518
10.2.1 The Scheduler.............................................................................. 518
10.3 RTOS Services ....................................................................................... 521
10.4 Synchronization and Messaging Tools .................................................... 521
10.5 CCS PIC C Compiler RTOS................................................................... 522
10.5.1 Preparing for RTOS .................................................................... 523
10.5.2 Declaring a Task ......................................................................... 524
Project 10.1—LEDs ........................................................................................ 524
Project 10.2—Random Number Generator....................................................... 528
Project 10.3—Voltmeter with RS232 Serial Output ......................................... 532
Index...............................................................................................541

www.newnespress.com
Contents
About the Authors .........................................................................................................xiii

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


Chapter 1. The PIC Microcontroller Family .............................................................................. 3
1.1 12-bit Instruction Word ................................................................................................. 6
1.2 14-bit Instruction Word ................................................................................................. 7
1.3 16-bit Instruction Word ............................................................................................... 11
1.4 Inside a PIC Microcontroller ....................................................................................... 12

Chapter 2. Introducing the PIC® 16 Series and the 16F84A ................................................... 39


2.1 The Main Idea—the PIC 16 Series Family ................................................................. 39
2.2 An Architecture Overview of the 16F84A .................................................................. 42
2.3 A Review of Memory Technologies ............................................................................ 44
2.4 The 16F84A Memory .................................................................................................. 46
2.5 Some Issues of Timing ................................................................................................ 51
2.6 Power-Up and Reset .................................................................................................... 54
2.7 What Others Do—the Atmel AT89C2051 .................................................................. 55
2.8 Taking Things Further—the 16F84A On-Chip Reset Circuit ..................................... 56
2.9 Summary ..................................................................................................................... 59
References .......................................................................................................................... 59

Chapter 3. Parallel Ports, Power Supply and the Clock Oscillator .......................................... 61
3.1 The Main Idea—Parallel Input/Output........................................................................ 62
3.2 The Technical Challenge of Parallel Input/Output ...................................................... 62
3.3 Connecting to the Parallel Port .................................................................................... 68
3.4 The PIC 16F84A Parallel Ports ................................................................................... 71
3.5 The Clock Oscillator ................................................................................................... 74
3.6 Power Supply............................................................................................................... 78
3.7 The Hardware Design of the Electronic Ping-Pong .................................................... 80
3.8 Summary ..................................................................................................................... 82
References .......................................................................................................................... 82
vi Contents

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 ......... 108
4.10 Taking Things Further—The 16 Series Instruction Set Format .............................. 109
4.11 Summary ................................................................................................................. 110
References ........................................................................................................................ 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 .......................................................................................... 136
5.10 Simulating the Ping-Pong Program—Tutorial ........................................................ 140
5.11 What Others Do—Graphical Simulators ................................................................. 143
5.12 Summary ................................................................................................................. 143
References ........................................................................................................................ 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...................................................................................................... 174
6.10 Summary ................................................................................................................. 175

www.n ewn es p res s .c om


Contents vii

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 ................................................. 237
10.1 The Inner Differences .............................................................................................. 238
10.2 Interrupts ................................................................................................................. 242
10.3 EEPROM ................................................................................................................. 252
10.4 Analog to Digital Conversion .................................................................................. 259
10.5 Comparator Module ................................................................................................ 264
10.6 Final Project: Intelligent Garden Lights .................................................................. 270
Chapter 11. Using Inputs ....................................................................................................... 275
11.1 Switch Flowchart ..................................................................................................... 277
11.2 Program Development ............................................................................................. 278
11.3 Scanning (Using Multiple Inputs) ........................................................................... 283
11.4 Switch Scanning ...................................................................................................... 283
11.5 Control Application—A Hot Air Blower ................................................................ 287
Chapter 12. Keypad Scanning ................................................................................................ 291
12.1 Programming Example for the Keypad ................................................................... 291
Chapter 13. Program Examples ............................................................................................. 307
13.1 Counting Events ...................................................................................................... 307
13.2 Look-Up Table......................................................................................................... 311
13.3 7-Segment Display .................................................................................................. 311
13.4 Numbers Larger than 255 ........................................................................................ 321
13.5 Long Time Intervals ................................................................................................ 327
13.6 One Hour Delay....................................................................................................... 330

w w w.ne w nespress.com
viii Contents

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


Chapter 14. PicBasic and PicBasic Pro Programming........................................................... 335
14.1 PicBasic Language .................................................................................................. 335
14.2 PicBasic Pro Language ............................................................................................ 357
14.3 Liquid Crystal Display (LCD) Interface and Commands ....................................... 369
14.4 Interrupts ................................................................................................................. 380
14.5 Recommended PicBasic Pro Program Structure ..................................................... 381
14.6 Using Stepping Motors ............................................................................................ 381
14.7 Using Servomotors .................................................................................................. 384
Chapter 15. Simple PIC Projects ........................................................................................... 387
15.1 Project #1—Flashing an LED ................................................................................. 387
15.2 Project #2—Scrolling LEDs .................................................................................... 391
15.3 Project #3—Driving a 7-Segment LED Display ..................................................... 397
Chapter 16. Moving On with the 16F876 .............................................................................. 405
16.1 Project #4—Accessing Port A I/O .......................................................................... 405
16.2 Project #5—Analog-to-Digital Conversion ............................................................. 412
16.3 Project #6—Driving a Servomotor .......................................................................... 421
Chapter 17. Communication .................................................................................................. 429
17.1 Project #7—Driving an LCD Module ..................................................................... 429
17.2 Project #8—Serial Communication......................................................................... 439
17.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 ...................................................... 465
18.1 The Compiler Package ............................................................................................ 465
18.2 BASIC and Its Essentials......................................................................................... 467
18.3 Development Boards ............................................................................................... 470
18.4 Programming Style .................................................................................................. 473
18.5 Building the Circuits and Standard Assumptions.................................................... 475
18.6 Pins, Ports and Input/Output ................................................................................... 476
18.7 Pseudo-Code and Planning the Program ................................................................. 485
18.8 Inside the Compiler ................................................................................................. 487
References ........................................................................................................................ 491
Chapter 19. The Basics—Output ........................................................................................... 493
19.1 Pin Architectures ..................................................................................................... 494
19.2 LED Indicators ........................................................................................................ 498
19.3 Switching Inductive Loads ...................................................................................... 503

www.n ewn es p res s .c om


Contents ix

19.4 Low Side Switching ................................................................................................ 506


19.5 Isolated Switching ................................................................................................... 524
19.6 Fast Switching—Sound from a PIC ........................................................................ 533
References ........................................................................................................................ 536
Chapter 20. The Basics—Digital Input.................................................................................. 539
20.1 Introduction ............................................................................................................. 539
20.2 Switch Bounce and Sealing Current ........................................................................ 548
20.3 Hardware Debouncing ............................................................................................. 549
20.4 Software Debouncing .............................................................................................. 551
20.5 Isolated Switching ................................................................................................... 555
20.6 Reading a Keypad.................................................................................................... 557
Reference .......................................................................................................................... 562
Chapter 21. Introductory Stepper Motors .............................................................................. 563
21.1 Stepper Motor Basics .............................................................................................. 563
21.2 Programs .................................................................................................................. 586
References ........................................................................................................................ 613
Chapter 22. Digital Temperature Sensors and Real-Time Clocks ......................................... 615
22.1 DS18B20 Temperature Sensor ................................................................................ 615
22.2 Reading Multiple Sensors on the Same Bus ........................................................... 628
22.3 DS1302 Real-Time Clock ....................................................................................... 633
22.4 Combination Date, Time and Temperature ............................................................. 647
22.5 Ideas for Modifications to Programs and Circuits ................................................... 653
References ........................................................................................................................ 656
Chapter 23. Infrared Remote Controls ................................................................................... 657
23.1 Common Encoding Standards ................................................................................. 659
23.2 IR Receiver .............................................................................................................. 661
23.3 Characterizing Wide/Narrow Pulse Intervals .......................................................... 664
23.4 Decoding a REC-80 Controller .............................................................................. 680
23.5 Ideas for Modifications to Programs and Circuits .................................................. 693
References ........................................................................................................................ 694

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


Chapter 24. Getting Started.................................................................................................... 697
24.1 The Plan ................................................................................................................... 697
24.2 Checklist .................................................................................................................. 697
24.3 Coding ..................................................................................................................... 698
24.4 Review ..................................................................................................................... 707

w w w.ne w nespress.com
x Contents

Books ................................................................................................................................ 710


Links ................................................................................................................................. 710
Chapter 25. Programming Loops ........................................................................................... 711
25.1 The Plan ................................................................................................................... 711
25.2 Checklist .................................................................................................................. 711
25.3 Coding ..................................................................................................................... 712
25.4 Using the Logic Analyzer........................................................................................ 719
25.5 Review ..................................................................................................................... 720
Books ................................................................................................................................ 723
Links ................................................................................................................................. 723
Chapter 26. More Pattern Work, More Loops ....................................................................... 725
26.1 The Plan ................................................................................................................... 725
26.2 Checklist .................................................................................................................. 725
26.3 Coding ..................................................................................................................... 725
26.4 Testing with the Logic Analyzer ............................................................................. 732
26.5 Using the Explorer16 Demonstration Board ........................................................... 734
26.6 Review ..................................................................................................................... 734
Books ................................................................................................................................ 736
Links ................................................................................................................................. 736
Chapter 27. NUMB3RS ......................................................................................................... 737
27.1 The Plan ................................................................................................................... 737
27.2 Checklist .................................................................................................................. 737
27.3 Coding ..................................................................................................................... 737
27.4 Notes for C Experts ................................................................................................. 742
27.5 Measuring Performance .......................................................................................... 743
27.6 Review ..................................................................................................................... 746
Links ................................................................................................................................. 749
Chapter 28. Interrupts ............................................................................................................ 751
28.1 The Plan ................................................................................................................... 751
28.2 Checklist .................................................................................................................. 751
28.3 Coding ..................................................................................................................... 751
28.4 Managing Multiple Interrupts ................................................................................. 764
28.5 Review ..................................................................................................................... 765
Books ................................................................................................................................ 768
Links ................................................................................................................................. 768
Chapter 29. Taking a Look Under the Hood .......................................................................... 769
29.1 The Plan ................................................................................................................... 769
29.2 Checklist .................................................................................................................. 769

www.n ewn es p res s .c om


Contents xi

29.3 Coding ..................................................................................................................... 769


29.4 Review ..................................................................................................................... 783
Books ................................................................................................................................ 785
Links ................................................................................................................................. 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

w w w.ne w nespress.com
For more information about this title, click here

CONTENTS

Introduction xi
Acknowledgments xxi

Chapter 1 Embedded Microcontrollers 1


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

Chapter 2 The Microchip PIC Microcontroller 43


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

Chapter 3 Software Development Tools 63


Tools Overview 65
High Level Languages 83
Microchip MPLAB IDE 103

Chapter 4 Programming PIC Microcontrollers 155


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

Chapter 5 Emulators and Debuggers 207


MPLAB ICE-2000 210
MPLAB REAL ICE 213

v
vi CONTENTS

MPLAB ICD 2 Debugger 214


The Emu-II 219
Other Emulators 241

Chapter 6 The Microchip PIC MCU Processor Architecture 243


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

Chapter 7 Using the PIC MCU Instruction Set 293


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

Chapter 8 Assembly-Language Software Techniques 373


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

Chapter 9 Basic Operating Features 445


Power Input and Decoupling 446
Configuration Fuses 451
OPTION Register 470
TMR0 478
Interrupt Operation 483
The Right PIC Microcontroller to Learn On 485
CONTENTS vii

Chapter 10 Macro Development 489


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

Chapter 11 Building and Linking 519


Creating Linked Applications 519

Chapter 12 Bootloaders 527


Bootloader Requirements 528
Mid-Range Bootloaders 530
PIC18 Bootloaders 535

Chapter 13 Real-Time Operating Systems 537


Low-End and Mid-Range RTOSs 541
PIC18 RTOS Design 542

Chapter 14 Debugging Your Applications 565


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

Chapter 15 PIC Microcontroller Application Design


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

Chapter 16 PIC MCU Optional Hardware Features 617


Mid-Range Built-in EEPROM/Flash Access 618
TMR1 624
TMR2 626
Compare/Capture/PWM (CCP) Module 628
Serial I/O 633
Analog I/O 649
Parallel Slave Port (PSP) 657
In-Circuit Serial Programming (ICSP) 659
viii CONTENTS

Chapter 17 PIC MCU Input and Output Device Interfacing 661


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

Chapter 18 Motor Control 711


Dc Motors 711
Stepper Motors 724
R/C Servo Control 733

Chapter 19 Practical PC Interfacing 739


PC Software Application Development Tools 740
Serial Port 742
Parallel Port 749

Chapter 20 PIC Microcontroller Application Basics 755


Jumping Around 755
Some Basic Functions 771
Analog Input/Output 798
I/O with Interrupts 810
Serial I/O 832

Chapter 21 Projects 853


Low-End Devices 853
Mid-Range Devices 878
PIC18 Devices 953

Appendix A Resources 965


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

Appendix B PIC Microcontroller Summary 977


Feature to Part Number Table 977
Instruction Sets 977
I/O Register Addresses 1016
Device Pinouts 1030
CONTENTS ix

Appendix C Useful Tables and Data 1061


Electrical Engineering Formulas 1063
Mathematical Formulas 1065
Mathematical Conversions 1066
ASCII 1067

Appendix D Miscellaneous Electronic Reference Information 1073


Basic Electronic Components and Their Symbols 1073
Test Equipment 1080

Appendix E Basic Programming Language 1089


PICBASIC 1091

Appendix F C Programming Language 1123


Common Library Functions 1130
PICC Library Functions 1133
Microchip C18 Library Functions 1138

Appendix G Reuse, Return, and Recycle 1149


Useful Snippets 1150
Mykemacs.inc 1160
Sixteen-Bit Numbers 1200

Glossary 1213
Index 1229
Contents

Acknowledgements ix
Preface to the third edition xi

1 Introduction 1
Some tips before starting 2
Binary, decimal and hexadecimal 2
An 8-bit system 5
Initial steps 5
Choosing your PIC microcontroller 6
Writing 10
Assembling 10
The file registers 10
A program template 13

2 Exploring the PIC5x series 20


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

3 The PIC12F50x series (8-pin PIC microcontrollers) 90


Differences from the PIC16F54 90
The STATUS register 90
The OSCCAL register 91
Inputs and outputs 92
The OPTION register 92
The TRIS register 93
The general purpose file registers 93
The MCLR 93
Configuration bits 93
Example project: ‘PIC dice’ 94
Random digression 95

4 Intermediate operations using the PIC12F675 100


The inner differences 101
The OPTION and WPU registers 102
The TRISIO register 103
Calibrating the internal oscillator 103
PCLATH: Higher bits of the program counter 104
Remaining differences 105
Interrupts 105
INTCON 106
The interrupt service routine 107
Interrupts during sleep 109
Maintaining the STATUS quo 109
New program template 110
Example project: ‘Quiz game controller’ 112
EEPROM 116
EECON1 116
Reading from the EEPROM 116
Writing to the EEPROM 117
Example project: ‘Telephone card chip’ 118
Further EEPROM examples: Music maker 122
Power monitor 122
Analogue to digital conversion 122
ADCON0 123
ANSEL: Analogue select register 124
A/D conversion interrupt 125
Example project: ‘Bath monitor’ 125
Comparator module 129
Voltage reference 130
Comparator interrupts 130
Comparator example: ‘Sun follower’ 131
Comparator example: Reading many buttons from one pin 132
Final project: Intelligent garden lights 134
Contents vii

5 Advanced operations and the future 138


Extra timers: TMR1 & … 138
Capture/Compare/PWM 139
USART: Serial communication 140
Programming tips 142
6 A PIC development environment 143
7 Sample programs 145
Program A LedOn – Turns an LED on 145
Program B PushButton (1.0) – If a push button is pressed,
turns on an LED 146
Program C PushButton (2.0) – Shorter version of PushButton 1.0 147
Program D Timing – LED states toggled every second, and buzzer
on every five seconds 148
Program E Traffic – Pedestrian traffic lights junction is simulated 150
Program F Counter (1.0) – Counts signals from a push button,
resets after 16 152
Program G Counter (2.0) – Stop reading button twice (otherwise,
as Counter 1.0) 154
Program H Counter (3.0) – Solves button bounce (otherwise, as
Counter 2.0) 156
Program I StopClock – A stop clock displaying tenths of
seconds to minutes 158
Program J LogicGates – Acts as the eight different gates 162
Program K Alarm – An alarm system which can be set or disabled 164
Program L BikeBuddy – A speedometer and mileometer for bikes 165
Program M PIC Dice – A pair of dice are simulated 171
Program N Quiz – Indicates which of three push buttons has been
pressed first 175
Program O Phonecard – To act like a phonecard which decrements
a file register 177
Program P TempSense – Displays whether temperature is too
hot, too cold or OK 181
Program Q 183
Appendix A Specifications of some Flash PIC microcontrollers 189
Appendix B Pin layouts of some Flash PIC microcontrollers 191
Appendix C Instructions glossary 192
Appendix D Number system conversion 195
Appendix E Bit assignments of various file registers 196
Appendix F If all else fails, read this 203
Appendix G Contacts and further reading 204
Appendix H PICKit™ 1 & BFMP Info 205
Appendix I Answers to the exercises 207
Appendix J Some BASIC commands in assembly 222
Index 223

S-ar putea să vă placă și