Contenido Pic

download Contenido Pic

of 112

Transcript of Contenido Pic

  • 7/28/2019 Contenido Pic

    1/112

    C book

    PTER I INTRODUCTION TO

    OCONTROLLERS

    ductionoryocontrollers versus microprocessors

    Memory unitCentral processing unitBusesnput-output uniterial communication

    Timer unitWatchdogAnalog to digital converter

    rogram

    PTER II MICROCONTROLLER

    F84

    ductionC, RISCicationsk/instruction cycleining

    description

    Clock generator - oscillatorResetCentral processing unit

    ortsMemory organizationnterruptsree timer TMR0EPROM Data memory

    PTER III INSTRUCTION SET

    duction

    uction set in PIC16Cxx microcontrollerly

    Transferhmetic and logicperationscting the program flowuction execution periodd list

    PTER IV ASSEMBLY LANGUAGE

    GRAMMING

    duction

    CHAPTER V MPLAB

    Introduction

    5.1 Installing the MPLAB program package5.2 Introduction to MPLAB5.3 Choosing the development mode5.4 Designing a project5.5 Designing new assembler file5.6 Writing a program5.7 MPSIM simulator5.8 Toolbar

    CHAPTER VI THE SAMPLES

    Introduction

    6.1 The microcontroller power supply6.2 Macros used in programs

    Macros WAIT, WAITX Macro PRINT

    6.3 Samples

    Light Emitting Diodes Keyboard Optocoupler

    Optocouplering the input lines Optocouplering the output lines

    Relays Generating a sound

    Shift registers Input shift register Output shift register

    7-segment Displays (multiplexing) LCD display 12-bit AD converter Serial communication

    APPENDIX A INSTRUCTION SET

    APPENDIX B NUMERIC SYSTEMS

    Introduction

    B.1 Decimal numeric systemB.2 Binary numeric systemB.3 Hexadecimal numeric system

    APPENDIX C GLOSSARY

  • 7/28/2019 Contenido Pic

    2/112

    Contents

    Preface xi

    1 Introduction 1

    1.1 The Idea of System Control 11.2 Computer in the Loop 21.3 Centralized and Distributed Control Systems 51.4 Scada Systems 61.5 Hardware Requirements for Computer Control 7

    1.5.1 General Purpose Computers 71.5.2 Microcontrollers 8

    1.6 Software Requirements for Computer Control 91.6.1 Polling 111.6.2 Using External Interrupts for Timing 11

    1.6.3 Using Timer Interrupts 121.6.4 Ballast Coding 121.6.5 Using an External Real-Time Clock 13

    1.7 Sensors Used in Computer Control 141.7.1 Temperature Sensors 151.7.2 Position Sensors 171.7.3 Velocity and Acceleration Sensors 201.7.4 Force Sensors 211.7.5 Pressure Sensors 211.7.6 Liquid Sensors 221.7.7 Air Flow Sensors 23

    1.8 Exercises 24Further Reading 25

    2 System Modelling 27

    2.1 Mechanical Systems 272.1.1 Translational Mechanical Systems 282.1.2 Rotational Mechanical Systems 32

  • 7/28/2019 Contenido Pic

    3/112

    vi CONTENTS

    2.2 Electrical Systems 372.3 Electromechanical Systems 422.4 Fluid Systems 44

    2.4.1 Hydraulic Systems 442.5 Thermal Systems 492.6 Exercises 52

    Further Reading 52

    3 The PIC Microcontroller 57

    3.1 The PIC Microcontroller Family 573.1.1 The 10FXXX Family 583.1.2 The 12CXXX/PIC12FXXX Family 593.1.3 The 16C5X Family 593.1.4 The 16CXXX Family 593.1.5 The 17CXXX Family 603.1.6 The PIC18CXXX Family 60

    3.2 Minimum PIC Conguration 613.2.1 External Oscillator 633.2.2 Crystal Operation 633.2.3 Resonator Operation 633.2.4 RC Operation 653.2.5 Internal Clock 65

    3.3 Some Popular PIC Microcontrollers 663.3.1 PIC16F84 Microcontroller 673.3.2 PIC16F877 Microcontroller 71

    3.4 Exercises 75Further Reading 76

    4 Programming PIC Microcontrollers in C 77

    4.1 PICC Lite Variable Types 784.1.1 Bit 784.1.2 Unsigned Char 784.1.3 Signed Char 794.1.4 Unsigned Int 794.1.5 Signed Int 794.1.6 Long 794.1.7 Unsigned Long 794.1.8 Float 804.1.9 Double 80

    4.2 Variables 804.3 Comments in Programs 814.4 Storing Variables in the Program Memory 824.5 Static Variables 824.6 Volatile Variables 834.7 Persistent Variables 83

  • 7/28/2019 Contenido Pic

    4/112

    CONTENTS vii

    4.8 Absolute Address Variables 834.9 Bank1 Qualier 83

    4.10 Arrays 844.11 ASCII Constants 864.12 Arithmetic and Logic Operators 864.13 Number Bases 894.14 Structures 894.15 Program Flow Control 91

    4.15.1 IfElse Statement 914.15.2 SwitchCase Statement 924.15.3 For Statement 944.15.4 While Statement 954.15.5 Do Statement 954.15.6 Break Statement 964.15.7 Continue Statement 96

    4.16 Functions in C 964.16.1 User Functions 974.16.2 Built-in Functions 98

    4.17 Pointers in C 994.18 Pre-processor Commands 101

    4.18.1 #dene 1014.18.2 #include 1034.18.3 #asm and #endasm 103

    4.19 Accessing the EEPROM Memory 1044.20 Interupts in C Programs 1044.21 Delays in C Programs 1054.22 Structure of a C Program 105

    4.23 PIC Microcontroller InputOutput Interface 1074.23.1 Connecting an LED 1074.23.2 Connecting a Push-Button Switch 1094.23.3 Connecting an LCD 111

    4.24 Exercises 116Further Reading 117

    5 Microcontroller Project Development 119

    5.1 Hardware and Software Requirements 1195.2 Program Development Tools 120

    5.2.1 Flow Charts 1215.2.2 Structure Charts 1215.2.3 Pseudocode 123

    5.3 Exercise 129Further Reading 129

    6 Sampled Data Systems and the z-Transform 131

    6.1 The Sampling Process 1316.2 The z-Transform 136

  • 7/28/2019 Contenido Pic

    5/112

    viii CONTENTS

    6.2.1 Unit Step Function 1376.2.2 Unit Ramp Function 1376.2.3 Exponential Function 1386.2.4 General Exponential Function 1386.2.5 Sine Function 1396.2.6 Cosine Function 1396.2.7 Discrete Impulse Function 1406.2.8 Delayed Discrete Impulse Function 1406.2.9 Tables of z-Transforms 140

    6.2.10 The z-Transform of a Function Expressed as a Laplace Transform 1406.2.11 Properties of z-Transforms 1436.2.12 Inverse z-Transforms 145

    6.3 Pulse Transfer Function and Manipulation of Block Diagrams 1546.3.1 Open-Loop Systems 1546.3.2 Open-Loop Time Response 1566.3.3 Closed-Loop Systems 1626.3.4 Closed-Loop Time Response 166

    6.4 Exercises 166Further Reading 169

    7 System Time Response Characteristics 171

    7.1 Time Response Comparison 1717.2 Time Domain Specications 1747.3 Mapping the s-Plane into the z-Plane 1777.4 Damping Ratio and Undamped Natural Frequency in the z-Plane 178

    7.4.1 Damping Ratio 1787.4.2 Undamped Natural Frequency 179

    7.5 Damping Ratio and Undamped Natural Frequency Using Formulae 1817.6 Exercises 183

    Further Reading 184

    8 System Stability 187

    8.1 Factorizing the Characteristic Equation 1878.2 Jurys Stability Test 1898.3 RouthHurwitz Criterion 1928.4 Root Locus 1948.5 Nyquist Criterion 2018.6 Bode Diagrams 2058.7 Exercises 208

    Further Reading 211

    9 Discrete Controller Design 213

    9.1 Digital Controllers 2149.1.1 Dead-Beat Controller 2159.1.2 Dahlin Controller 217

  • 7/28/2019 Contenido Pic

    6/112

    CONTENTS ix

    9.1.3 Pole-Placement Control Analytical 2199.1.4 Pole-Placement Control Graphical 222

    9.2 PID Controller 2309.2.1 Saturation and Integral Wind-Up 2339.2.2 Derivative Kick 2339.2.3 PID Tuning 134

    9.3 Exercises 137Further Reading 240

    10 Controller Realization 243

    10.1 Direct Structure 24310.1.1 Direct Canonical Structure 24310.1.2 Direct Noncanonical Structure 245

    10.2 Cascade Realization 246

    10.3 Parallel Realization 24910.4 PID Controller Implementations 25010.5 Microcontroller Implementations 253

    10.5.1 Implementing Second-Order Modules 25410.5.2 Implementing First-Order Modules 26010.5.3 Implementing Higher-Order Modules 263

    10.6 Choice of Sampling Interval 26310.7 Exercises 267

    Further Reading 268

    11 Liquid Level Digital Control System: a Case Study 269

    11.1 The System Schematic 269

    11.2 System Model 27011.3 Identication of the System 27311.4 Designing a Controller 27411.5 Conclusions 278

    Appendix A Table of z-Transforms 283

    Appendix B MATLAB Tutorial 285

    Index 307

  • 7/28/2019 Contenido Pic

    7/112

    v

    Contents

    Preface .................................................................................................................. ix

    Whats on the CD-ROM?....................................................................................... x

    Chapter 1: Why Are We Doing This? ................................................................... 1

    Selecting a Suitable Microcontroller .............................................................................................2Selecting a Suitable 802.11b Communications Device ................................................................. 3802.11b Hardware Overview....................................................................................................... 3AirDrop Basics ............................................................................................................................. 4

    Chapter 2: The AirDrop-P .....................................................................................9The AirDrop-P Hardware.............................................................................................................. 9Learn to Play Guitar and Become Famous ..................................................................................15

    Chapter 3: The AirDrop-A................................................................................... 17 The AirDrop-A Hardware ........................................................................................................... 17Bowing Out............................................................................................................................... 23

    Chapter 4: 802.11b CompactFlash Network Interface Cards ........................... 25 They Were Not Designed To Do This .......................................................................................... 25The TEW-222CF ........................................................................................................................ 25Never Ignore an Inquisitive Author with Hand Tools ................................................................... 26Unwrapping the TEW-222CF..................................................................................................... 30An Undercover Look at the Zonet ZCF1100 ............................................................................... 32Whats Behind Door Number 4 .................................................................................................. 34RF, Witchcraft, Pointy Hats, Ghouls, Goblins...Same Thing ......................................................... 35

    Chapter 5: Talking With 802.11bCompactFlash NICs ........................................ 37 Physically Connecting a Microcontroller to a CompactFlash Card ...............................................38Musical Overtones..................................................................................................................... 43

    Chapter 6: Touring the Card Information Structure .........................................45

    Talking in Tuples ........................................................................................................................ 46First Steps with the AirDrop-P....................................................................................................48Walking the Tuple Chain............................................................................................................ 50CIS Reconnaissance ................................................................................................................... 59

  • 7/28/2019 Contenido Pic

    8/112

    Contents

    vi

    Dumping Linksys WCF12 Tuples ................................................................................................ 65Dumping Netgear MA701 Tuples............................................................................................... 68Dumping Zonet ZCF100 Tuples.................................................................................................. 70Enabling the 802.11b CompactFlash NIC................................................................................... 74The Value of Parsing the CIS...................................................................................................... 77Full Throttle............................................................................................................................... 77

    Chapter 7: Learning to Talk to 802.11b CompactFlash NICs ............................ 79What the 802.11b NIC Does for Us ........................................................................................... 79The 802.11b CompactFlash NIC I/O Drivers................................................................................ 83

    Chapter 8: Setting Up An AirDrop Wireless Network ...................................... 93Setting Up the AP...................................................................................................................... 94Somethings in the Air ............................................................................................................... 95Guitars and Hollywood............................................................................................................ 104

    Chapter 9: AirDrop Driver Basics ..................................................................... 105 BAP......................................................................................................................................... 105FID .......................................................................................................................................... 106RID.......................................................................................................................................... 107Reading a RID.......................................................................................................................... 118Stringing Up the SSID .............................................................................................................. 125Good RIDdance ....................................................................................................................... 128Retrieving the MAC Address.................................................................................................... 130Status Check ........................................................................................................................... 134

    Chapter 10: Putting an AirDrop on a Wireless LAN........................................ 137 Bogie Number 1 Allocating Transmit Buffers ......................................................................... 137Bogie Number 2 Enabling the MAC...................................................................................... 147

    Authenticating the AirDrop Wireless LAN Station..................................................................... 158Associating with the AIRDROP_NETWORK AP.......................................................................... 160

    Chapter 11: Processing 802.11b Frames with the AirDrop ............................ 167 AirDrop Frame Structure.......................................................................................................... 168AirDrop-P Frame Reception...................................................................................................... 184

    Chapter 12: PINGING the AirDrop.................................................................... 209Examining the IP Header.......................................................................................................... 231

    Chapter 13: Flying Cargo with UDP and the AirDrop..................................... 243Running a UDP Application on the AirDrop-P........................................................................... 243The EDTP Internet Test Panel and the Code Behind It ............................................................... 245Exercising the AirDrop-P with the EDTP Internet Test Panel....................................................... 249Notes ...................................................................................................................................... 274

    Chapter 14: Flying Cargo with TCP/IP and the AirDrop.................................. 275 TCP and the AirDrop-P ............................................................................................................ 275The TCP/IP Stacks Physical Layer.............................................................................................. 284The TCP/IP Stacks Data Link Layer ........................................................................................... 284

  • 7/28/2019 Contenido Pic

    9/112

    Contents

    vii

    The TCP/IP Stacks Network Layer............................................................................................. 284The TCP/IP Stacks Transport Layer ........................................................................................... 284The TCP/IP Stacks Application Layer ........................................................................................ 285TCP/IP The Big Ugly .............................................................................................................. 285Youve Done It! ....................................................................................................................... 334

    Chapter 15: WEP and the AirDrop ................................................................... 335 Incorporating WEP into the AirDrop 802.11b Driver.................................................................335The New Experimental AirDrop Hardware................................................................................ 345An Experimental AVR AirDrop Variant...................................................................................... 345The Experimental AirDrop Firmware......................................................................................... 348Coding a Simple 802.11b Web Server .....................................................................................355The AirDrop SRAM .................................................................................................................. 358

    Chapter 16: A New Kid in Town Who Calls Himself ZigBee........................... 361Zig What???............................................................................................................................ 361Making ZigBee Talk ................................................................................................................. 363The Microchip ZigBee Stack..................................................................................................... 366

    Chapter 17: Parting Frames .............................................................................. 371Numeric Notation.................................................................................................................... 373Source Code Presentation........................................................................................................ 373

    Conventions ...................................................................................................... 373Sub Snippets ........................................................................................................................... 374Netasyst Sniffer Capture Text Presentation ............................................................................... 375Mini Sniffs............................................................................................................................... 375

    Index ..................................................................................................................377

  • 7/28/2019 Contenido Pic

    10/112

    Contents

    Part 1 Microcontroller 1

    1 PIC Hardware 3

    Processor System 4

    PIC 16F877 Architecture 8

    PIC Instruction Set 18

    Special Function Registers 25

    2 PIC Software 35

    Assembly Language 37Software Design 44

    C Programming 47

    3 Circuit Simulation 55

    Basic Circuit 56

    Software Debugging 63

    Hardware Testing 65Hardware Implementation 70

    Program Downloading 73

    xi

  • 7/28/2019 Contenido Pic

    11/112

    Contents

    xi i

    Part 2 Interfacing 77

    4 Input & Output 79

    Switch Input 79

    Switch Debouncing 81

    Timer and Interrupts 84

    Keypad Input 87

    7-Segment LED Display 88

    Liquid Crystal Display 90

    5 Data Processing 101

    Number Systems 101

    Conversion 106

    Variable Types 110

    Arithmetic 112

    6 Calculate, Compare & Capture 121

    Calculator 121

    Pulse Output 128

    Period Measurement 130

    7 Analogue Interfacing 141

    8-bit Conversion 141

    10-bit Conversion 145

    Amplifier Interfaces 149

  • 7/28/2019 Contenido Pic

    12/112

    Contents

    xiii

    Transient & Frequency Response 160

    Instrumentation Amplifier 161

    Current Loop 163

    Comparators 165

    Op-amp Selection 168

    Analogue Output 168

    Part 3 Systems 177

    8 Power Outputs 179

    Current Drivers 179

    Relays & Motors 183

    Power Output Interfacing 185

    Motor Interfacing 189

    9 Serial Communication 201

    USART 201

    SPI 205

    I2C 210

    10 Sensor Interfacing 223

    Sensors 223Sensor Types 228

    Amplifier Design 236

    Weather Station 238

  • 7/28/2019 Contenido Pic

    13/112

    11 System Design 249

    Base System 249

    Memory System 259

    Other PIC Chips 266

    System Design 270

    Other MCU Families 274

    Answers to Assessment Questions 279

    Index & Abbreviations 291

    Contents

    xi v

  • 7/28/2019 Contenido Pic

    14/112

  • 7/28/2019 Contenido Pic

    15/112

  • 7/28/2019 Contenido Pic

    16/112

    CONTENIDO

    Prologo

    1. Microcontroladores programables: La solucin esta en un chip2: Microcontroladores de 8 bits:3: Los sellos mgicos de parallax4: Pbasic: El lenguaje mas fcil del mundo5: El primer contacto con el PIC16F846: En el interior del procesador7: Los recursos fundamentales: temporizadores, puertas de e/s y eeprom dedatos8: Interrupciones, reset y recursos auxiliares9: Manejando el repertorio de instrucciones 10: Herramientas y diseo deproyectos11: Ejercicios de programacin en ensamblador12: Los primeros diseos prcticos.A: Gua Rpida del PIC16X84B: Gua del usuario del SIM2000C: Breve descripcin del Micro PIC TrainerD: La familia PIC al completoE: Contenido del CDF: Proyectos con el PIC16F84 Bibliografa y direcciones de inters relacionadascon los PIC ndice

  • 7/28/2019 Contenido Pic

    17/112

    Table of Contents

    Preface xv

    Chapter 1 - Basic Electronics 11.0 The Atom 11.1 Isotopes and Ions 21.2 Static Electricity 31.3 Electrical Charge 4

    1.3.1 Voltage 41.3.2 Current 41.3.3 Power 51.3.4 Ohm's Law 5

    1.4 Electrical Circuits 61.4.1 Types of Circuits 6

    1.5 Circuit Elements 81.5.1 Resistors 91.5.2 Revisiting Ohm's Law 91.5.3 Resistors in Series and Parallel 101.5.4 Capacitors 121.5.5 Capacitors in Series and in Parallel 131.5.6 Inductors 141.5.7 Transformers 15

    1.6 Semiconductors 151.6.1 Integrated Circuits 161.6.2 Semiconductor Electronics 161.6.3 P-Type and N-Type Silicon 171.6.4 The Diode 17

    Chapter 2 - Number Systems 192.0 Counting 192.0.1 The Tally System 192.0.2 Roman Numerals 20

    2.1 The Origins of the Decimal System 202.1.1 Number Systems for Digital-Electronics 222.1.2 Positional Characteristics 222.1.3 Radix or Base of a Number System 23

    v

  • 7/28/2019 Contenido Pic

    18/112

    2.2 Types of Numbers 232.2.1 Whole Numbers 242.2.2 Signed Numbers 242.2.3 Rational, Irrational, and Imaginary Numbers 24

    2.3 Radix Representations 252.3.1 Decimal versus Binary Numbers 252.3.2 Hexadecimal and Octal 26

    2.4 Number System Conversions 272.4.1 Binary-to-ASCII-Decimal 282.4.2 Binary-to-Hexadecimal Conversion 292.4.3 Decimal-to-Binary Conversion 29

    Chapter 3 - Data Types and Data Storage 333.0 Electronic-Digital Machines 333.1 Character Representations 33

    3.1.1 ASCII 343.1.2 EBCDIC and IBM 363.1.3 Unicode 36

    3.2 Storage and Encoding of Integers 373.2.1 Signed and Unsigned Representations 373.2.2 Word Size 383.2.3 Byte Ordering 393.2.4 Sign-Magnitude Representation 403.2.5 Radix Complement Representation 41

    3.3 Encoding of Fractional Numbers 443.3.1 Fixed-Point Representations 453.3.2 Floating-Point Representations 463.3.3 Standardized Floating-Point Representations 473.3.4 IEEE 754 Single Format 483.3.5 Encoding and Decoding Floating-Point Numbers 50

    3.4 Binary-Coded Decimals (BCD) 513.4.1 Floating-Point BCD 52

    Chapter 4 - Digital Logic, Arithmetic, and Conversions 554.0 Microcontroller Logic and Arithmetic 55

    4.0.1 CPU Flags 554.0.2 Word Size 56

    4.1 Logical Instructions 564.1.1 Logical AND 574.1.2 Logical OR 574.1.3 Logical XOR 574.1.4 Logical NOT 58

    4.2 Microcontroller Arithmetic 584.2.1 Unsigned and Twos Complement Arithmetic 584.2.2 Operations on Decimal Numbers 60

    4.3 Bit Manipulations and Auxiliary Operations 624.3.1 Bit Shift and Rotate 624.3.2 Comparison Operations 634.3.3 Other Support Operations 63

    vi Mi crocontroller Programming

  • 7/28/2019 Contenido Pic

    19/112

    4.4 Unsigned Binary Arithmetic 644.4.1 Multi-byte Unsigned Addition 644.4.2 Unsigned Multiplication 654.4.3 Unsigned Division 67

    4.5 Signed Binary Arithmetic 674.5.1 Overflow Detection in Signed Arithmetic 694.5.2 Sign Extension Operations 704.5.3 Multi-byte Signed Operations 71

    4.6 Data Format Conversions 724.6.1 BCD Digits to ASCII Decimal 724.6.2 Unsigned Binary to ASCII Decimal Digits 734.6.3 ASCII Decimal String to Unsigned Binary 734.6.4 Unsigned Binary to ASCII Hexadecimal Digits 754.6.6 Signed Numerical Conversions 76

    Chapter 5 - Circuits and Logic Gates 775.0 Digital Circuits 775.1 The Diode Revisited 78

    5.1.1 The Light-Emitting Diode (LED) 795.2 The Transistor 81

    5.2.1 Bipolar Transistor 815.2.2 MOS Transistor 83

    5.3 Logic Gates 845.4 Transistor-Transistor Logic 85

    5.4.1 Inverter Gates 865.4.2 The AND Gate 875.4.3 The NAND Gate 875.4.4 The OR Gate 88

    5.4.5 The NOR Gate 885.4.6 Positive and Negative Logic 895.4.7 The XOR Gate 905.4.8 Schmitt Trigger Inverter 91

    5.5 Other TTL Logic Families 935.6 CMOS Logic Gates 93

    Chapter 6 - Circuit Components 956.0 Power Supplies 956.1 Clocked Logic and Flip-flops 96

    6.1.1 The RS Flip-flop 966.1.2 Clocked RS Flip-flop 986.1.3 The D Flip-flop 996.1.4 The Edge-triggered D Flip-flop 1006.1.5 Preset and Clear Signals 1016.1.6 D Flip-flop Waveform Action 1026.1.7 Flip-flop Applications 103

    6.2 Clocks 1036.2.1 Clock Waveforms 1046.2.2 The TTL Clock 1056.2.3 The 555 Timer 106

    Contents vii

  • 7/28/2019 Contenido Pic

    20/112

    6.2.4 Microcontroller Clocks 1066.3 Frequency Dividers and Counters 107

    6.3.1 Frequency Dividers 1076.3.2 The JK Flip-flop Counter 1076.3.3 Ripple Counters 1086.3.4 Decoding Gates 1106.3.5 Synchronous Counters 1106.3.6 Counter ICs 1126.3.7 Shift Registers 113

    6.4 Multiplexers and Demultiplexers 1156.4.1 Multiplexers 1156.4.2 Demultiplexers 1186.4.3 Multiplexer and Demultiplexer ICs 118

    6.5 Input Devices 1186.5.1 Switches 1186.5.2 Switch Contact Bounce 120

    6.5.3 Keypads 1216.6 Output Devices 1226.6.1 Seven-segment LED 1226.6.2 Liquid Crystal Displays 1246.6.3 LCD Technologies 125

    Chapter 7 - The Microchip PIC 1297.0 The PICMicro Microcontroller 129

    7.0.1 Programming the PIC 130PIC Programmers 131Development Boards 131

    7.0.2 Prototyping the PIC Circuit 1327.1 PIC Architecture 134

    7.1.1 Baseline PIC Family 134PIC10 Devices 135PIC12 Devices 135PIC14 Devices 138

    7.1.2 Mid-range PIC Family 138PIC16 Devices 139

    7.1.3 High-Performance PIC Family 139PIC18 Devices 139

    Chapter 8 - Mid-range PIC Architecture 1418.0 Processor Architecture and Design 142

    8.0.1 Harvard Architecture 1428.0.2 RISC CPU Design 1438.0.3 Single-word Instructions 1438.0.4 Instruction Format 1448.0.5 Mid-Range Device Versions 145

    8.1 The Mid-range Core Features 1458.1.1 Oscillator 1458.1.2 System Reset 1478.1.3 Interrupts 148

    8.2 Mid-Range CPU and Instruction Set 149

    viii Mi crocontroller Programming

  • 7/28/2019 Contenido Pic

    21/112

    8.2.1 Mid-Range Instruction Set 1498.2.2 STATUS and OPTION Registers 151

    8.3 EEPROM Data Storage 1538.3.1 EEPROM in Mid-Range PICs 153

    8.4 Data Memory Organization 1548.4.1 The w Register 1548.4.2 The Data Registers 154

    Memory Banks 154 The SFRs 155 The GP Rs 157

    8.4.3 Indirect Addressing 1588.5 Mid-range I/O and P eripheral Modules 158

    8.5.1 I/O Ports 1598.5.2 Timer Modules 1608.5.3 Capture-and-Compare Module 1608.5.4 Master Synchronous Serial Port (MSSP) Module 1618.5.5 USART Module 1618.5.6 A/D Module 161

    Chapter 9 - PIC Programming: Tools and Techniques 1639.0 Microchips MPLAB 163

    9.0.1 Embedded Systems 1649.1 Integrated Development Environment 165

    9.1.1 Installing MPLAB 1659.1.2 Creating the Project 1679.1.3 Project Build Options 1699.1.4 Building the Project 169

    9.2 Simulators and Debuggers 1709.2.1 MPLAB SIM 1719.2.2 MPLAB Hardware Debuggers 1729.2.3 A Quick-and-DirtyDebugger 174

    9.3 Programmers 1749.4 Engineering PIC Software 175

    9.4.1 Using Program Comments 176Program Header 176Commented Banners 177Commented Bitmaps 178

    9.4.2 Defining Data Elements 179 The cblock Directive 179

    9.4.3 Banking Techniques 180 The banksel Directive 180

    Bank Selection Macros 180Deprecated Banking Instructions 181

    9.4.4 P rocessor and Configuration Controls 182Configuration Bits 182

    9.4.5 Naming Conventions 1849.4.6 Errorlevel Directive 186

    9.5 Pseudo Instructions 186

    Contents ix

  • 7/28/2019 Contenido Pic

    22/112

    Chapter 10 - Programming Essentials: Input and Output 18910.0 16F84A Programming Template 189

    10.1 Introducing the 16F84A 19110.1.1 Template Circuit for 16F84A 19110.1.2 Power Supplies 191

    Voltage Regulator 19210.1.3 Comparisons in PIC Programming 193

    The Infamous PIC Carry Flag 19410.2 Simple Circuits and Programs 194

    10.2.1 A Single LED Circuit 194LED Flasher Program 196

    10.2.2 LED/Pushbutton Circuit 19910.2.3 Multiple LED Circuit 202

    10.3 Programming the Seven-segment LED 20410.4 A Demonstration Board 206

    10.4.1 PCB Images for Demo Board 20610.4.2 TestDemo1 Program 208

    Chapter 11 - Interrupts 21111.0 Interrupts on the 16F84 211

    11.0.1 The Interrupt Control Register 21111.0.2 The OPTION Register 212

    11.1 Interrupt Sources 21311.1.1 Port-B External Interrupt 21411.1.2 Timer0 Interrupt 21411.1.3 Port-B Line Change Interrupt 215

    Multiple External Interrupts 21711.1.4 EEPROM Data Write Interrupt 217

    11.2 Interrupt Handlers 21711.2.1 Context Saving Operations 218

    Saving w and STATUS Registers 21811.3 Interrupt Programming 218

    11.3.1 Programming the External Interrupt 219RB0 Interrupt Initialization 220RB0 Interrupt Service Routine 221

    11.3.2 Wakeup from SLEEP Using the RB0 Interrupt 222The SleepDemo Program 223

    11.3.3 Port-B Bits 4-7 Status Change Interrupt 224RB4-7 Interrupt Initialization 225RB4-7 Change Interrupt Service Routine 227

    11.4 Sample Programs 22911.4.1 The RB0Int Program 22911.4.2 The SleepDemo Program 23211.4.3 The RB4to7Int Program 235

    Chapter 12 - Timers and Counters 24112.0 The 16F84 Timer0 Module 241

    12.0.1 Timer0 Operation 241

    x Mi crocontroller Programming

  • 7/28/2019 Contenido Pic

    23/112

    Timer0 Interrupt 242 Timer0 Prescaler 242

    12.1 Delays Using Timer0 24312.1.1 Long Delay Loops 244

    How Accurate the Delay? 245 The Black-Ammerman Method 245

    12.2 Timer0 as a Counter 24612.3 Timer0 Programming 247

    12.3.1 Programming a Counter 247A Timer/Counter Test Circuit 248 The Tmr0Counter Program 248

    12.3.2 Timer0 as a Simple Delay Timer 25012.3.3 Measured Time Lapse 252

    Interrupt-driven Timer 25512.4 The Watchdog Timer 259

    12.4.1 Watchdog Timer Programming 260

    12.5 Sample Programs 26012.5.1 The Tmr0Counter program 26012.5.2 The Timer0 Program 26312.5.3 The LapseTimer Program 26512.5.4 The LapseTmrInt Program 269

    Chapter 13 - LCD Interfacing and Programming 27513.0 LCD Features and Architecture 275

    13.0.1 LCD Functions and Components 276Internal Registers 276Busy Flag 276Address Counter 276Display Data RAM (DDRAM) 276Character Generator ROM (CGROM) 276Character Generator RAM (CGRAM) 277 Timing Generation Circuit 277Liquid Crystal Display Driver Circuit 278Cursor/Blink Control Circuit 278

    13.0.2 Connectivity and Pin-Out 27813.1 Interfacing with the HD44780 279

    13.1.1 Busy Flag or Timed Delay Options 28013.1.2 Contrast Control 28113.1.3 Display Backlight 28113.1.4 Display Memory Mapping 281

    13.2 HD44780 Instruction Set 28313.2.1 Instruction Set Overview 283

    Clearing the Display 283Return home 284Entry mode set 284Display and Cursor ON/OFF 284Cursor/display shift 284Function set 285Set CGRAM address 285Set DDRAM address 285Read busy flag and Address register 285

    Contents xi

  • 7/28/2019 Contenido Pic

    24/112

    Write data 285Read data 286

    13.2.2 A 16F84 8-bit Data Mode Circuit 286

    13.3 LCD Programming 28713.3.1 Defining Constants and Variables 287

    Using MPLAB Data Directives 28913.3.2 LCD Initialization 290

    Function Set Command 290Display Off 291Display and Cursor On 291Set Entry Mode 292Cursor and Display Shift 292Clear Display 293

    13.3.3 Auxiliary Operations 293Time Delay Routine 293Pulsing the E Line 295Reading the Busy Flag 295Bit Merging Operations 296

    13.3.4 Text Data Storage and Display 298Generating and Storing a Text String 299Displaying the Text String 301

    13.3.5 Data Compression Techniques 3024-bit Data Transfer Mode 302Master/Slave Systems 304

    13.4 Sample Programs 30613.4.1 LCDTest1 30613.4.2 LCDTest2 Program 31613.4.3 LCDTest3 Program 327

    Chapter 14 - Communications 33914.0 PIC Communications Overview 33914.1 Serial Data Transmission 340

    14.1.1 Asynchronous Serial Transmission 34014.1.2 Synchronous Serial Transmission 34214.1.3 PIC Serial Communications 34214.1.4 The RS-232-C Standard 343

    Essential Concepts 344The Serial Bit Stream 344Parity Testing 345Connectors and Wiring 345The Null Modem 346The Null Modem Cable 347

    14.1.5 The EIA-485 Standard 349

    EIA-485 in PIC-based Systems 35014.2 Parallel Data Transmission 350

    14.2.1 PIC Parallel Slave Port (PSP) 35114.3 PIC Free-style Serial Programming 351

    14.3.1 PIC-to-PIC Serial Communications 352PIC-to-PIC Serial Communications Circuits 352PIC-to-PIC Serial Communications Programs 354

    14.3.2 Program Using Shift Register ICs 360

    xii Mi crocontroller Programming

  • 7/28/2019 Contenido Pic

    25/112

    The 74HC165 Parallel-to-Serial Shift Register 36174HC164 S erial-to-Parallel Shift R egister 364

    14.4 P IC P rotocol-based Serial P rogramming 36614.4.1 RS-232-C Communications on the 16F 84 366

    The RS-232-C Transceiver IC 367PIC to PC Communications 368An RS-232-C TTY Board 368A 16F84A UART Emulation 369An LCD Scrolling Routine 371

    14.4.2 RS-232-C Communications on the 16F87x 375 The 16F87x USART Module 376 The USART Baud Rate Generator 37616F87x USART Asynchronous Transmitter 37916F87x USART Asynchronous Receiver 380PIC-to-PC RS-232-C Communications C ircuit 38116F877 PIC Initialization Code 381USART Receive and Transmit Routines 384 The USART Receive Interrupt 386

    14.5 Sample Programs 38914.5.1 SerialSnd Program 38914.5.2 SerialRcv Program 39414.5.3 Serial6465 Program 40014.5.4 TTYUsart Program 40414.5.5 SerComLCD Program 42014.5.6 SerIntLCD Program 438

    Chapter 15 - Data EEPROM Programming 45915.0 PIC Internal EEPROM Memory 460

    15.0.1 EEPROM Programming on the 16F84 460

    Reading E EP ROM Data Memory on the 16F84 46016F84 EEPROM Data Memory Write 46116F84 E EPROM Demonstration Program 462

    15.0.2 EEPROM Programming on the 16F87x 465Reading EEPROM Data Memory on the 16F87x 467Writing to EEPROM Data Memory in the 16F87x 467GFR Access Issue in the 16F87x 469

    15.0.3 16F87x E EPROM Circuit and Program 46915.1 EEPROM Devices and Interfaces 475

    15.1.1 The I2C Serial Interface 47615.1.2 I2C Communications 47615.1.3 EEP ROM Communications Conditions 47715.1.4 EEPROM Write Operation 47815.1.5 EEPROM Read Operation 47815.1.6 I2C EEPROM Devices 47915.1.7 P IC Master Synchronous Serial Port (MSSP) 480

    MSSP in Master Mode 48215.1.8 I2C Serial EEPROM Programming on the 16F877 486

    IC2 Initialization Procedure 486I2C Write Byte Procedure 488I2C Read Byte Procedure 490

    15.2 Sample Programs 492

    Contents xiii

  • 7/28/2019 Contenido Pic

    26/112

    15.2.1 EECounter Program 49215.2.2 Ser2EEP Program 50415.2.3 I2CEEP Program 521

    Chapter 16 - Analog to Digital and Realtime Clocks 54316.0 A/D Converters 544

    16.0.1 Converter Resolution 54416.0.2 ADC Implementation 545

    16.1 A/D Integrated Circuits 54616.1.1 ADC0331 Sample Circuit and Program 547

    16.2 PIC On-Board A/D Hardware 54916.2.1 A/D Module on the 16F87x 549

    The ADCON0 Register 550The ADCON1 Register 552

    SLEEP Mode Operation 55416.2.2 A/D Module Sample Circuit and Program 554

    16.3 Realtime Clocks 55816.3.1 The NJU6355 Realtime Clock 55816.3.2 RTC Demonstration Circuit and Program 560

    BCD Conversion Procedures 565

    16.4 Sample Programs 56816.4.1 ADF84 Program 56816.4.2 A2DinLCD Program 58016.4.3 RTC2LCD Program 595

    Appendix A - Resistor Color Codes 613

    Appendix B - Building Your Own Circuit Boards 615

    Appendix C - Mid-range Instruction Set 621

    Appendix D - Supplementary Programs 659

    Index 795

    xiv Mi crocontroller Programming

  • 7/28/2019 Contenido Pic

    27/112

    v

    Contents1. Digital Design Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Binary Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    1.2.1 Representation of Integers and Floating Point Variables. . . . . . . . . . . . . . .21.2.2 Twos Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2.3 Floating Point Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    1.2.4 Basic Math Operations: Addition and Subtraction . . . . . . . . . . . . . . . . . . . 41.3 Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    1.3.1 Unicode and ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3.2 Gray Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    1.4 Combinational and Sequential Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.4.1 Digital Design Building Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7

    1.5 Digital Design Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.5.1 Programmable Gate Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.5.2 Microprocessors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.5.3 Digital Signal Processors (DSPs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    1.5.4 Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.5.5 Mixed Mode Processing Microcontroller with FPGA . . . . . . . . . . . . . . . 131.6 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    2. The Design and Development Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.1 The Design Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.2 Implementation and Testing Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.2.1 Software Development Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252.3 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

    3. Microcontroller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.1 So What Exactly is a Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    3.1.1 Microcontroller Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.1.2 Basic Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.1.3 RISC versus CISC Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    3.2 Register Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

  • 7/28/2019 Contenido Pic

    28/112

    vi CONTENTS

    3.3 Bus Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    3.3.1 Address Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3.2 Data Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3.3 Control Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    3.4 Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.5 Time Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    3.5.1 Timing Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.6 Port Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.7 Analog-to-Digital Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.8 Communication Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    3.8.1 Serial Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    3.8.2 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.8.3 Serial Communication Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.8.4 Handshake Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.8.5 RS-232 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    3.9 Interrupt System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.10 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443.11 Choosing a Microcontroller for a Specic Design. . . . . . . . . . . . . . . . . . . . . . . . . . .45

    3.11.1 System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.12 Microcontroller Vendors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.13 Cutting Edge Technology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.14 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    4. Timing Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    4.1.1 Frequency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.1.2 Period . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524.1.3 Duty Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    4.2 Timer System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534.2.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534.2.2 Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    4.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.3.1 Measuring External Timing Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.3.2 Counting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.3.3 Generating Timing Signals to Interface External Devices . . . . . . . . . . . . 594.3.4 Industrial Implementation Case Study (PWM) . . . . . . . . . . . . . . . . . . . . . 60

    4.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

  • 7/28/2019 Contenido Pic

    29/112

    CONTENTS vii

    5. Analog-to-Digital Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

    5.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655.1.1 Analog Signals Versus Digital Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .665.1.2 Sampling, Quantization, and Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675.1.3 Resolution and Data Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    5.2 Analog-to-Digital Conversion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745.3 ADC Conversion Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76

    5.3.1 Successive-Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765.3.2 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.3.3 Counter-Based Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785.3.4 Parallel Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

    5.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.4.1 Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.4.2 Signal Conditioning for ATD Converters . . . . . . . . . . . . . . . . . . . . . . . . . . 795.4.3 Digital-to-Analog Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.4.4 Industrial Implementation: Digital Cameras . . . . . . . . . . . . . . . . . . . . . . . .81

    5.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    6. Networked Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

    6.1.1 Designing Computer Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866.1.2 Types of Networks and Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86

    6.2 Microcontroller Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876.2.1 Controller Area Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876.2.2 BDLC Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886.2.3 Customized Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    6.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896.3.1 Automobiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906.3.2 Mobile Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

    6.4 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

    7. Operating Parameters and Interfacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957.1 Operating Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957.2 Input Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

    7.2.1 Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977.2.2 Switch Debouncing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007.2.3 Keypads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007.2.4 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

  • 7/28/2019 Contenido Pic

    30/112

    viii CONTENTS

    7.3 Output Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

    7.3.1 Light-Emitting Diodes (LEDs). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1027.3.2 Liquid Crystal Display (LCD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037.3.3 DC Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057.3.4 AC Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

    7.4 Application: DC Motor Speed and Direction Control . . . . . . . . . . . . . . . . . . . . . 1067.4.1 Motor Operating Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

    7.5 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

  • 7/28/2019 Contenido Pic

    31/112

    Contents

    v

    Preface.................................................................................................................ix

    A Quick Look at theMicrocontrollers.................................................................................. x

    Atmels AVR................................................................................................................. xMicrochips PIC ...........................................................................................................xii

    Whats on the CD-ROM? ..................................................................................xvi

    Chapter 1: The Essence of M icrocontroller NetworkingRS-232......................... 1

    Some History...................................................................................................................... 3RS-232 Standard Operating Procedure................................................................................ 5RS-232 VoltageConversion Considerations......................................................................... 8

    Chapter 2: Implementing RS-232 with a Microcontroller ....................................11

    Basic RS-232 Hardware .................................................................................................... 11Building a SimpleMicrocontroller RS-232 Transceiver........................................................ 14

    RS-232 InterfaceHardware ........................................................................................ 15A Microcontroller DCEDevice .................................................................................... 16Microchips PICkit 1 FLASH Starter Kit ........................................................................ 16Writing Some SimpleRS-232 Firmware ...................................................................... 20A Bit of RS-232 Transmit Code................................................................................... 27Some RS-232 Receive Code ....................................................................................... 32

    Chapter 3: Writing RS-232 Microcontroller Routines in BASIC............................ 37

    BASIC RS-232 .................................................................................................................. 37

    Chapter 4: Building Some RS-232 Communications Hardware ...........................43

    A Few More BASIC RS-232 Instructions ............................................................................ 43

  • 7/28/2019 Contenido Pic

    32/112

    vi

    Contents

    Chapter 5: Using Microcontroller USARTs ............................................................. 47

    Some Interrupt-Driven USART Code ................................................................................. 50

    Applying What WeKnow about RS-232 to the Atmel AVR............................................... 70Coding theAVRRS-232Routines............................................................................... 73

    Chapter 6: I 2CThe Other Serial Protocol ............................................................. 81

    Whyuse IC? ................................................................................................................... 83 The IC bus ...................................................................................................................... 83

    IC ACKS and NAKS .................................................................................................. 86More on Arbitration and Clock Synchronization ......................................................... 87IC Addressing ........................................................................................................... 91Some IC Firmware .................................................................................................... 91 The AVR Master IC Code .......................................................................................... 92 The AVR IC Master-Receiver Mode Code .................................................................. 97

    The PIC IC Slave-Transmitter Mode Code .................................................................. 99 The AVR-to-PIC IC Communications Ball ................................................................. 105

    Chapter 7: Ethernet ............................................................................................... 121

    What is Ethernet? .......................................................................................................... 121 The CS8900A-CQ .......................................................................................................... 122

    CS8900A-CQ Reset Overview .................................................................................. 123CS8900A-CQ Media InterfaceOverview .................................................................. 123CS8900A-CQ Transmit Process Overview ................................................................. 123CS8900A-CQ Receive Process Overview ................................................................... 124CS8900A-CQ External Storage Overview ................................................................. 125CS8900A-CQ StatusIndicators................................................................................. 126 The CS8900A-CQ MAC Engine................................................................................ 126

    Easy Ethernet CS8900A Hardware.................................................................................. 130 The PIC16F877 Microcontroller ................................................................................ 130 The Microchip PIC18F452 ........................................................................................ 131

    The CS8900A-CQ Ethernet Engine ................................................................................. 131Powering theCS8900A-CQ ............................................................................................ 132 The CS8900A-CQ Ethernet Magnetics............................................................................ 132Designing in theEasy Ethernet CS8900As PIC16F877 Microcontroller............................. 135 The ICSP (In-Circuit Serial Programming) Interface .......................................................... 136Developing the Easy Ethernet CS8900A Firmware .......................................................... 139Setting up thePIC16F877 Microcontroller....................................................................... 141Carving up thePIC16F877s MemoryResources.............................................................. 143

    Function Prototypes ................................................................................................. 143

    Defining theVariables .............................................................................................. 144 The Easy Ethernet CS8900A Macros............................................................................... 151Defining theCS8900A-CQ PacketPage Register Set ........................................................ 156

    CS8900A-CQ BusInterface Registers ....................................................................... 158Product Identification Code...................................................................................... 158

  • 7/28/2019 Contenido Pic

    33/112

    vi i

    Contents

    CS8900A-CQ Statusand Control Registers............................................................... 159Did It Register? ........................................................................................................ 172

    Chapter 8: Writing the CS8900A-CQ Firmware ..................................................173

    The First Step ................................................................................................................. 174Reset theCS8900A-CQ .................................................................................................. 175Load theCS8900A-CQ Basic Parameters........................................................................ 176Load theCS8900A-CQ Individual Address Register Set ................................................... 178Enable the CS8900A-CQ Transmitter and Receiver.......................................................... 179 The Main Service Loop ................................................................................................... 180A Frame Under theMicroscope...................................................................................... 182 The Art of ARP............................................................................................................... 189

    Chapter 9: PINGing the Easy Ethernet CS8900A ................................................. 203

    Chapter 10: UDP and the Easy Ethernet CS8900A ..............................................221

    A UDP Internet Test Panel............................................................................................... 223

    Chapter 11: TCP and the Easy Ethernet CS8900A............................................... 239

    The Physical Layer .......................................................................................................... 241 The DataLink Layer........................................................................................................ 241 The Network Layer ......................................................................................................... 242 The Transport Layer ........................................................................................................ 242 The Application Layer ..................................................................................................... 242Coding TCP/IP for theEasy Ethernet CS8900A ............................................................... 244

    Chapter 12: Lets Do It Again ................................................................................ 293Easy Ethernet Whacked??? What the? ....................................................................... 293 The Realtek RTL8019AS ................................................................................................. 294 The Easy Ethernet W Hardware ...................................................................................... 302 The Easy Ethernet W Firmware....................................................................................... 304Initializing theRealtek RTL8019AS.................................................................................. 307Online with theEasy Ethernet W .................................................................................... 324Sending a Frame using theEasy Ethernet W ................................................................... 327 Toolsfor Work and Play.................................................................................................. 331

    Chapter 13: Putting the Easy Ethernet AVR Online ...........................................337

    Chapter 14: Finale .................................................................................................347Obtaining Easy Ethernet Devices..................................................................................... 348

    About the Author ..................................................................................................349Index .......................................................................................................................351

  • 7/28/2019 Contenido Pic

    34/112

    Contents

    Introduction ix

    1 Introduction to the PIC microcontroller 1The aim of the book 1Program memory 2Microcontroller clock 3The microcontroller system 3Types of microcontroller 4Microcontroller specification 5Using the microcontroller 61 Microcontroller hardware 62 Programming the microcontroller 9

    2 Programming the 16F84 microcontroller 11Microcontroller inputs and output (I/O) 12Timing with the microcontroller 12Programming the microcontroller 12Entering data 13The header for the 16F84 14Program example 16Saving and assembling the code 19

    PICSTART PLUS programmer 23Programming flowchart 26Problem: flashing two LEDs 26Solution to problem, flashing two LEDs 27

    3 Introductory projects 29LED_Flasher2 29SOS 30Code for SOS circuit 30Flashing 8 LEDs 33Chasing 8 LEDs 35Traffic lights 39More than 8 outputs 45

    4 Headers, porting code which micro? 47Factors affecting the choice of the microcontroller 47Choosing the microcontroller 48Headers 49

  • 7/28/2019 Contenido Pic

    35/112

    5 Using inputs 64Switch flowchart 66Program development 67Scanning (using multiple inputs) 73Switch scanning 73Control application a hot air blower 77

    6 Understanding the headers 82The 16F84 8216F84 memory map 87The 16F818 88

    7 Keypad scanning 93

    Programming example for the keypad 94

    8 Program examples 110Counting events 110Look up table 1157-Segment display 115Numbers larger than 255 126Long time intervals 133One hour delay 136

    9 The 16C54 microcontroller 139Header for the 16C54 13916C54 memory map 142

    10 Alpha numeric displays 143Display pin identification 144Configuring the display 145Writing to the display 146Program example 146Program operation 160Display configuration 161Writing to the display 162Displaying a number 163

    11 Analogue to digital conversion 166Making an A/D reading 167

    Configuring the A/D device 168Analogue header for the 16F818 171A/D conversion example, a temperature sensitive

    switch 174Program code 176Another example a voltage indicator 178

    vi Contents

  • 7/28/2019 Contenido Pic

    36/112

    12 Radio transmitters and receivers 186Measuring the received pulse width 189

    13 EEPROM data memory 199Example using the EEPROM 200

    14 Interrupts 207Interrupt sources 208Interrupt control register 208Program using an interrupt 209

    15 The 12 series 8 pin microcontroller 216

    Pin diagram of the 12C508/509 216Pin diagram of the 12F629 and 12F675 216Features of these 12 series 217The memory map of the 12C508 217Oscillator calibration 218I/O PORT, GPIO 219Delays with the 12 series 220Header for 12C508/9 220Program application for 12C508 222Program application using the 12F629/675 225

    16 The 16F87X Microcontroller 22916F87X family specification 229

    The 16F872 microcontroller 23016F87X memory map 232The 16F872 header 23316F872 application a greenhouse control 236Programming the 16F872 microcontroller

    using PICSTART PLUS 242Reconfiguring the 16F872 header 243

    17 The 16F62X Microcontroller 24516F62X oscillator modes 24516F62X and 16F84 Pinouts 24716F62X port configuration 24716F62X memory map 248

    The 16F62X headers 248HEAD62RC.ASM 250A 16F627 application flashing an LED on and off 252The 16F627 LED flasher code 253Configuration settings for the 16F627 255Other features of the 16F62X 255

    Contents vii

  • 7/28/2019 Contenido Pic

    37/112

  • 7/28/2019 Contenido Pic

    38/112

    Contents

    Chapter 1. Conventions Used in This Book 1

    Chapter 2. PICmicro MCU Part NumberFeature Comparison 3

    Feature to Part Number Table 3

    Chapter 3. Device Pinouts 7

    Low-End 8Mid-Range 10PIC17Cxx 14PIC18Cxx 17

    Chapter 4. PICmicro MCU Instruction Sets 21

    Parameters 21Low-End Instruction Set 22Mid-Range Instruction Set 31PIC17Cxx Instruction Set 40PIC18Cxx Instruction Set 63Microchip Special Instruction Mnemonics 104Parallax PICmicro MCU Instruction Set 111

    v

    Copyright 2001 The McGraw-Hill Companies. Click Here for Terms of Use.

  • 7/28/2019 Contenido Pic

    39/112

    Chapter 5. PICmicro MCU ProcessorArchitectures 123

    The PICmicro MCUs Arithmetic Logic Unit 123Low-End PICmicro MCUs 125

    Register access 126STATUS register 128Program counter 128

    Mid-Range PICmicro MCUs 130Register access 130STATUS register 132Program counter 133Interrupt operation 134Interrupt handler skeleton 135

    PIC17Cxx 136Register access 137STATUS register 138Program counter 139Interrupt operation 140Interrupt handler skeleton 142

    PIC18Cxx 142Register access 144

    STATUS register 146Program counter 147Interrupt operation 150Interrupt handler skeleton 150

    Chapter 6. PICmicro MCU Register Mappings 151

    Low-End PICmicro MCUs 151Mid-Range PICmicro MCUs 154PIC17Cxx 169PIC18Cxx 186

    vi Contents

  • 7/28/2019 Contenido Pic

    40/112

    Chapter 7. Built-In Hardware Features 207

    Configuration Registers 207Oscillators 208Sleep 213Option Register 216

    Input/Output Ports and TRIS Registers 217Watchdog Timer 221TMR0 222Prescaler 223TMR1 224TMR2 227Compare/Capture/PWM(CCP) Module 228USART Module 232SSP Module 239

    SPI operation 239I2C operation 244

    Built-In ADC 252Built-In Comparators 257Parallel Slave Port 261Built-In EEPROM Data Memory Access 263EPROM Program Memory Access 269Flash Program Memory Access 271External Parallel Memory 273

    Chapter 8. PICmicro MCUHardware Interfacing 277

    Power 277Reset 281Digital Logic Interfacing 282

    Contents vii

  • 7/28/2019 Contenido Pic

    41/112

    Parallel Bus Device Interfacing 282Button Interfacing 284Switch Matrix Keypad/Keyboard Interfacing 287Combining Input and Output 291Simulated Open Collector/Open Drain I/O 292LEDs 293

    Multisegment LED displays 293LCD Interfaces 296I2C Bit Banging Master Interface 334RS-232 Interfaces 338RS-485/RS-422 345Asynchronous Serial I/O Software Routines 347Dallas Semiconductor One-Wire Interface 357Reading Potentiometer Using Parallel I/O Pins 363Motor Drivers 365R/C Servo Control 370Audio Output 371AC Power Control 373Hall-Effect Sensors 374Sony Infrared TV Remote Control 374

    Chapter 9. PICmicro MCU Programming 379

    Hex File Format 379Low-End PICmicro MCU Programming 381Mid-Range Serial Programming 385PIC17Cxx Programming 394PIC17Cxx ICSP Programming 398PIC18Cxx Programming 402Microchip ICSP Programming Connector 405Third Party/Downloadable Programmers 406

    viii Contents

  • 7/28/2019 Contenido Pic

    42/112

    Chapter 10. PC Interfaces 407

    Memory Map 407I/O Space Map 407Interrupt Function by Number 422ISA Bus 425

    ISA pinouts 425Interrupts 429

    Keyboard and Mouse Ports 430Connector specification 430Keyboard operation with timing diagrams 431Keyboard scan codes 432Keyboard controller commands 436BIOS interfaces 436Keyboard commands 441

    Serial Port 441Connector pinouts 4428250 block diagram 443Serial port base addresses 4438250 registers 444Interrupts 449Interrupt 14hRS-232 communications APIs 451

    Parallel Port 455Block diagram/connector 455Base registers 457Registers 457Data output waveform 458BIOS interfaces 459

    Chapter 11. Useful Code Snippets 461

    Jumping Outside the Current Page 461Tables 462Conditional Branching 465

    Contents ix

  • 7/28/2019 Contenido Pic

    43/112

    Time Delays 466Negating the Contents of a Register 467Incrementing/Decrementing w 468Rotating a Byte in Place 469Copy Bits from One Register to Another 470Converting a Nybble to ASCII 470

    Converting an ASCII Byte to a Hex Nybble 471Using T0CKI as an Interrupt Source Pin 471Dividing by Three 472Sixteen-Bit Pulse Measurement with

    5-Cycle Delay 473Detect a Change in a Register 474Test a Byte within a Range 474Convert ASCII to Upper Case 475Swap the Contents of w with a Register 475Swap the Contents of Two Registers 475Compare and Swap if Y X 476Counting the Number of 1s in a Byte 476Generating Parity for a Byte 477Keeping a Variable within a Range 477

    Swapping Bit Pairs 478Bitwise Operations 478Constant Multiplication 479Constant Division 480

    Chapter 12. 16-Bit Numbers 483

    Defining 16 Bit Numbers 483Increments and Decrements 484Addition/Subtraction 485Bitwise Operations on Constants and Variables 488

    x Contents

  • 7/28/2019 Contenido Pic

    44/112

    Comparisons with 16-Bit Variables 489Multiplication 492Division 495

    Chapter 13. PICmicro MCU

    Operations Tables 499I/O Pin Current Capabilities 500RC Oscillator Component Values 500LP Oscillator Operating Characteristics 501XT Oscillator Operating Characteristics 502HS Oscillator Operating Characteristics 504

    Chapter 14. PICmicro MCU ApplicationDebugging Checklist 507

    Chapter 15. PICmicro MCU ApplicationSoftware Development Tools 517

    Microsoft Compatible Editor Ctrl KeyCombinations 517

    MPSIM.INI 518MPLAB 519

    Stimulus (.STI) files 525MPLAB assembler directives 526

    Standard Declaration and Include (.inc) Files 536LinkingLinked Applications 536Application Code Template 539The BASIC Language 540Microsoft BASIC Enhancements 542PicBasic 549

    Contents xi

  • 7/28/2019 Contenido Pic

    45/112

    Visual Basic 578MSComm control 581

    The C Language 588Declarations 588Statements 590Operators 593Directives 595Backslash characters 598Common C functions 598PICmicro MCU enhancement functions 603

    Chapter 16. Constants and Data Tables 605

    Mathematical and Physical Constants 605ASCII 606

    ASCII control characters 607ANSI display control sequences 609IBM PC extended ASCII characters 612Windows ASCII characters 612

    EBCDIC 612Audio Notes 615

    Touch-Tone Telephone Frequencies 616Modem AT Commands 616Modem registers 622

    Morse Code 625Phonetic Alphabets 626Ten Radio Codes 626

    Chapter 17. Miscellaneous Electronics 629

    Resistor Color Coding 629Electromagnetic Spectrum 630

    Radar bands 632

    xii Contents

  • 7/28/2019 Contenido Pic

    46/112

    Digital Logic 632Gates 632Flip flops 633

    Chapter 18. Formulas 635

    DC Electronics Formulas 635

    AC Electronics Formulas 636Mathematical Formulas 638Boolean Arithmetic 638Conversions 640

    Chapter 19. Resources 641

    Microchip 641PICmicro MCU Books 642Useful Books 643PICList Internet List Server 648Recommended PICmicro MCU Web Sites 656Periodicals 658Useful Web Sites 659Hardware FAQs 660

    Part Suppliers 661Digi-Key 661AP Circuits 661Wirz Electronics 662Tower Hobbies 663Jameco 663JDR 663Newark 664Marshall Industries 664Mouser Electronics 665Mondo-tronics Robotics Store 665

    Index 667

    Contents xiii

  • 7/28/2019 Contenido Pic

    47/112

  • 7/28/2019 Contenido Pic

    48/112

  • 7/28/2019 Contenido Pic

    49/112

    Contents vii

    Summary 64References 64

    4 Starting to program an introduction to Assembler 65

    4.1 The main idea what programs do and how we develop them 664.1.1 The problem of programming and the Assembler compromise 664.1.2 The process of writing in Assembler 674.1.3 The program development process 68

    4.2 The PIC 16 Series instruction set, with a little more on the ALU 694.2.1 More on the PIC 16 Series ALU 694.2.2 The PIC 16 Series instruction set an introduction 70

    4.3 Assemblers and Assembler format 714.3.1 Introducing Assemblers and the Microchip MPASM TM Assembler 714.3.2 Assembler format 714.3.3 Assembler directives 724.3.4 Number representation 72

    4.4 Creating simple programs 734.4.1 A simple data transfer program 73

    4.5 Adopting a development environment 764.5.1 Introducing MPLAB 764.5.2 The elements of MPLAB 764.5.3 The MPLAB le structure 77

    4.6 An introductory MPLAB tutorial 774.6.1 Creating a project 774.6.2 Entering source code 794.6.3 Assembling the project 80

    4.7 An introduction to simulation 814.7.1 Getting started 814.7.2 Generating port inputs 814.7.3 Viewing microcontroller features 824.7.4 Resetting and running the program 82

    4.8 Downloading the program to a microcontroller 834.9 What others do a brief comparison of CISC and RISC instruction sets 864.10 Taking things further the 16 Series instruction set format 87Summary 88References 88

    5 Building Assembler programs 89

    5.1 The main idea building structured programs 895.1.1 Flow diagrams 895.1.2 State diagrams 91

    5.2 Flow control branching and subroutines 925.2.1 Conditional branching and working with bits 925.2.2 Subroutines and the Stack 94

  • 7/28/2019 Contenido Pic

    50/112

    viii Contents

    5.3 Generating time delays and intervals 955.4 Dealing with data 97

    5.4.1 Indirect addressing and the File Select Register 97

    5.4.2 Look-up tables 985.4.3 Example program with delays and look-up table 99

    5.5 Introducing logical instructions 1015.6 Introducing arithmetic instructions and the Carry ag 102

    5.6.1 Using add instructions 1025.6.2 Using subtract instructions 1025.6.3 An arithmetic program example 1025.6.4 Using indirect addressing to save the Fibonacci series 104

    5.7 Taming Assembler complexity 1065.7.1 Include Files 1065.7.2 Macros 1075.7.3 MPLAB special instructions 108

    5.8 More use of the MPLAB simulator 1095.8.1 Breakpoints 1095.8.2 Stopwatch 1105.8.3 Trace 110

    5.9 The ping-pong program 1125.9.1 A structure for the ping-pong program 1125.9.2 Exploring the ping-pong program code 115

    5.10 Simulating the ping-pong program tutorial 1165.10.1 Setting up input stimulus 1165.10.2 Setting up the Watch window 1165.10.3 Single stepping 1165.10.4 Animate 117

    5.10.5 Run 1175.10.6 Breakpoints 1175.10.7 Stopwatch 1175.10.8 Trace 1175.10.9 Debugging the full program 118

    5.11 What others do graphical simulators 118Summary 119References 119

    6 Working with time: interrupts, counters and timers 120

    6.1 The main idea interrupts 121

    6.1.1 Interrupt structures 1216.1.2 The 16F84A interrupt structure 1226.1.3 The CPU response to an interrupt 124

    6.2 Working with interrupts 1256.2.1 Programming with a single interrupt 1256.2.2 Moving to multiple interrupts identifying the source 126

  • 7/28/2019 Contenido Pic

    51/112

    Contents ix

    6.2.3 Stopping interrupts from wrecking yourprogram 1 context saving 127

    6.2.4 Stopping interrupts from wrecking your program 2 critical

    regions and masking 1306.3 The main idea counters and timers 131

    6.3.1 The digital counter reviewed 1316.3.2 The counter as timer 1326.3.3 The 16F84A Timer 0 module 134

    6.4 Applying the 16F84A Timer 0, with examples using the electronic ping-pong 1366.4.1 Object or event counting 1366.4.2 Hardware-generated delays 137

    6.5 The Watchdog Timer 1386.6 Sleep mode 1396.7 What others do 1406.8 Taking things further interrupt latency 141

    Summary 142

    Section 3 Larger Systems and the PIC 16F873A 143

    7 Larger systems and the PIC 16F873A 145

    7.1 The main idea the PIC 16F87XA 1467.2 The 16F873A block diagram and CPU 146

    7.2.1 Overview of CPU and core 1467.2.2 Overview of memory 1477.2.3 Overview of peripherals 150

    7.3 16F873A memory and memory maps 1507.3.1 The 16F873A program memory 1507.3.2 The 16F873A data memory and Special Function Registers 1527.3.3 The Conguration Word 154

    7.4 Special memory operations 1557.4.1 Accessing EEPROM and program memory 1557.4.2 In-Circuit Serial Programming (ICSP TM ) 156

    7.5 The 16F873A interrupts 1587.5.1 The interrupt structure 1587.5.2 The interrupt registers 1597.5.3 Interrupt identication and context saving 161

    7.6 The 16F873A oscillator, reset and power supply 161

    7.6.1 The clock oscillator 1617.6.2 Reset and power supply 1617.7 The 16F873A parallel ports 161

    7.7.1 The 16F873A Port A 1637.7.2 The 16F873A Port B 1647.7.3 The 16F873A Port C 164

  • 7/28/2019 Contenido Pic

    52/112

    x Contents

    7.8 Test, commission and diagnostic tools 1657.8.1 The challenge of testing an embedded system 1657.8.2 Oscilloscopes and logic analysers 167

    7.8.3 In-circuit emulators 1707.8.4 On-chip debuggers 170

    7.9 The Microchip in-circuit debugger (ICD 2) 1717.10 Applying the 16F873A: the Derbot AGV 172

    7.10.1 Power supply, oscillator and reset 1727.10.2 Use of the parallel ports 1737.10.3 Assembling the hardware 174

    7.11 Downloading, testing and running a simpleprogram with ICD 2 1767.11.1 A rst Derbot program 1767.11.2 Applying the ICD 2 1787.11.3 Setting the conguration bits within the program 179

    7.12 Taking things further the 16F874A/16F877APorts D and E 180

    Summary 182References 183

    8 The human and physical interfaces 184

    8.1 The main idea the human interface 1848.2 From switches to keypads 187

    8.2.1 The keypad 1878.2.2 Design example: use of keypad in Derbot hand controller 188

    8.3 LED displays 1938.3.1 LED arrays: seven-segment displays 1938.3.2 Design example: the Derbot hand controller

    seven-segment display 1948.4 Liquid crystal displays 199

    8.4.1 The HD44780 LCD driver and its derivatives 1998.4.2 Design example: use of LCD display in Derbot hand controller 200

    8.5 The main idea interfacing to the physical world 2038.6 Some simple sensors 203

    8.6.1 The microswitch 2048.6.2 Light-dependent resistors 2048.6.3 Optical object sensing 205

    8.6.4 The opto-sensor applied as a shaft encoder 2058.6.5 Ultrasonic object sensor 2078.7 More on digital input 207

    8.7.1 16F873A input characteristics 2078.7.2 Ensuring legal logic levels, and input protection 2088.7.3 Switch debouncing 212

  • 7/28/2019 Contenido Pic

    53/112

    Contents xi

    8.8 Actuators: motors and servos 2128.8.1 DC and stepper motors 2128.8.2 Angular positioning: the servo 214

    8.9 Interfacing to actuators 2158.9.1 Simple DC switching 2158.9.2 Simple switching on the Derbot 2178.9.3 Reversible switching: the H-bridge 2188.9.4 Motor switching on the Derbot 220

    8.10 Building up the Derbot 2208.11 Applying sensors and actuators a blind navigation Derbot program 222Summary 223References 223

    9 Taking timing further 225

    9.1 The main ideas taking counting and timing further 2259.2 The 16F87XA Timer 0 and Timer 1 226

    9.2.1 Timer 0 2269.2.2 Timer 1 2269.2.3 Application of Timer 0 and Timer 1 as counters for Derbot odometry 2289.2.4 Using Timer 0 and Timer 1 to generate repetitive interrupts 231

    9.3 The 16F87XA Timer 2, comparator and PR2 register 2329.3.1 Timer 2 2329.3.2 The PR2 register, comparator and postscaler 234

    9.4 The capture/compare/PWM (CCP) modules 2359.4.1 A capture/compare/PWM overview 2359.4.2 Capture mode 235

    9.4.3 Compare mode 2379.5 Pulse width modulation 2379.5.1 The principle of PWM 2379.5.2 Generating PWM signals in hardware the 16F87XA PWM 2399.5.3 PWM applied in the Derbot for motor control 241

    9.6 Generating PWM in software 2449.6.1 An example of software-generated PWM 2459.6.2 Further Assembler directives for memory denition and branching 248

    9.7 PWM used for digital-to-analog conversion 2499.7.1 An example of PWM used for digital-to-analog conversion 249

    9.8 Frequency measurement 2529.8.1 The principle of frequency measurement 252

    9.8.2 Frequency (speed) measurement in the Derbot 2529.9 Speed control applied to the Derbot 2559.10 Where there is no timer 2589.11 Sleep mode 2609.12 Where do we go from here? 2619.13 Building up the Derbot 262

  • 7/28/2019 Contenido Pic

    54/112

    xii Contents

    Summary 262References 262

    10 Starting with serial 263

    10.1 The main idea introducing serial 26310.2 Simple serial links synchronous data communication 265

    10.2.1 Synchronous basics 26510.2.2 Implementing synchronous serial I/O in the microcontroller 26610.2.3 Microwire and SPI (Serial Peripheral Interface) 26610.2.4 Introducing multiple nodes 267

    10.3 The 16F87XA Master Synchronous Serial Port (MSSP) module in SPI mode 26710.3.1 Port overview 26810.3.2 Port conguration 26810.3.3 Setting the clock 270

    10.3.4 Managing data transfer 27110.4 A simple SPI example 27310.5 The limitations of Microwire and SPI, and of simple

    synchronous serial transfer 27510.6 Enhancing synchronous serial, and the Inter-Integrated Circuit bus 275

    10.6.1 Main I 2C features and physical interconnection 27510.6.2 The pull-up resistor 27510.6.3 I 2C signal characteristics 276

    10.7 The MSSP congured for I 2C 27710.7.1 The MSSP I 2C registers and their preliminary use 27710.7.2 The MSSP in I 2C Slave mode 28110.7.3 The MSSP in I 2C Master mode 283

    10.8 I2C applied in the Derbot AGV 28610.8.1 The Derbot hand controller as a serial node 286

    10.8.2 The AGV as an I 2C master 28610.8.3 The hand controller as an I 2C slave 29010.8.4 Evaluation of the Derbot I 2C programs 292

    10.9 Evaluation of synchronous serial data communicationand an introduction to asynchronous 29310.9.1 Asynchronous principles 29310.9.2 Synchronising serial data without an incoming clock 293

    10.10 The 16F87XA Addressable Universal S