Professional Documents
Culture Documents
Contenido Pic
Contenido Pic
CISC, RISC
Applications 6.3 Samples
Clock/instruction cycle
Pipelining ● Light Emitting Diodes
Pin description ● Keyboard
● Optocoupler
2.1 Clock generator - oscillator ❍ Optocouplering the input lines
Introduction
Preface xi
1 Introduction 1
1.1 The Idea of System Control 1
1.2 Computer in the Loop 2
1.3 Centralized and Distributed Control Systems 5
1.4 Scada Systems 6
1.5 Hardware Requirements for Computer Control 7
1.5.1 General Purpose Computers 7
1.5.2 Microcontrollers 8
1.6 Software Requirements for Computer Control 9
1.6.1 Polling 11
1.6.2 Using External Interrupts for Timing 11
1.6.3 Using Timer Interrupts 12
1.6.4 Ballast Coding 12
1.6.5 Using an External Real-Time Clock 13
1.7 Sensors Used in Computer Control 14
1.7.1 Temperature Sensors 15
1.7.2 Position Sensors 17
1.7.3 Velocity and Acceleration Sensors 20
1.7.4 Force Sensors 21
1.7.5 Pressure Sensors 21
1.7.6 Liquid Sensors 22
1.7.7 Air Flow Sensors 23
1.8 Exercises 24
Further Reading 25
2 System Modelling 27
2.1 Mechanical Systems 27
2.1.1 Translational Mechanical Systems 28
2.1.2 Rotational Mechanical Systems 32
vi CONTENTS
Index 307
Contents
Preface .................................................................................................................. ix
What’s on the CD-ROM?....................................................................................... x
Chapter 1: Why Are We Doing This? ................................................................... 1
Selecting a Suitable Microcontroller ............................................................................................. 2
Selecting a Suitable 802.11b Communications Device ................................................................. 3
802.11b Hardware Overview ....................................................................................................... 3
AirDrop Basics ............................................................................................................................. 4
Chapter 2: The AirDrop-P ..................................................................................... 9
The AirDrop-P Hardware.............................................................................................................. 9
Learn to Play Guitar and Become Famous .................................................................................. 15
Chapter 3: The AirDrop-A................................................................................... 17
The AirDrop-A Hardware ........................................................................................................... 17
Bowing Out............................................................................................................................... 23
Chapter 4: 802.11b CompactFlash Network Interface Cards ........................... 25
They Were Not Designed To Do This .......................................................................................... 25
The TEW-222CF ........................................................................................................................ 25
Never Ignore an Inquisitive Author with Hand Tools ................................................................... 26
Unwrapping the TEW-222CF..................................................................................................... 30
An Undercover Look at the Zonet ZCF1100 ............................................................................... 32
What’s Behind Door Number 4 .................................................................................................. 34
RF, Witchcraft, Pointy Hats, Ghouls, Goblins...Same Thing ......................................................... 35
Chapter 5: Talking With 802.11bCompactFlash NICs........................................ 37
Physically Connecting a Microcontroller to a CompactFlash Card ............................................... 38
Musical Overtones..................................................................................................................... 43
Chapter 6: Touring the Card Information Structure ......................................... 45
Talking in Tuples ........................................................................................................................ 46
First Steps with the AirDrop-P .................................................................................................... 48
Walking the Tuple Chain............................................................................................................ 50
CIS Reconnaissance ................................................................................................................... 59
v
Contents
vi
Contents
vii
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 37
Software Design 44
‘C’ Programming 47
3 Circuit Simulation 55
Basic Circuit 56
Software Debugging 63
Hardware Testing 65
Hardware Implementation 70
Program Downloading 73
xi
Contents
Part 2 Interfacing 77
xii
Contents
xiii
Contents
xiv
CONTENIDO
Prologo
1. Microcontroladores programables: La solución esta en un chip
2: Microcontroladores de 8 bits:
3: Los sellos mágicos de parallax
4: Pbasic: El lenguaje mas fácil del mundo
5: El primer contacto con el PIC16F84
6: En el interior del procesador
7: Los recursos fundamentales: temporizadores, puertas de e/s y eeprom de
datos
8: Interrupciones, reset y recursos auxiliares
9: Manejando el repertorio de instrucciones 10: Herramientas y diseño de
proyectos
11: Ejercicios de programación en ensamblador
12: Los primeros diseños prácticos.
A: Guía Rápida del PIC16X84
B: Guía del usuario del SIM2000
C: Breve descripción del Micro PIC Trainer
D: La familia PIC al completo
E: Contenido del CD
F: Proyectos con el PIC16F84 Bibliografía y direcciones de interés relacionadas
con los PIC Índice
Table of Contents
Preface xv
v
vi Microcontroller Programming
Index 795
v
Contents
1. Digital Design Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Binary Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 Representation of Integers and Floating Point Variables. . . . . . . . . . . . . . .2
1.2.2 Two’s Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.3 Floating Point Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.4 Basic Math Operations: Addition and Subtraction . . . . . . . . . . . . . . . . . . . 4
1.3 Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 Unicode and ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Gray Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Combinational and Sequential Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.1 Digital Design Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Digital Design Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.1 Programmable Gate Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.2 Microprocessors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.3 Digital Signal Processors (DSPs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.4 Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.5.5 Mixed Mode Processing Microcontroller with FPGA . . . . . . . . . . . . . . . 13
1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3. Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.1 So What Exactly is a Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.1.1 Microcontroller Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.1.2 Basic Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.1.3 RISC versus CISC Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2 Register Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
vi CONTENTS
3.3 Bus Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.1 Address Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.2 Data Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.3 Control Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.4 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5 Time Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.5.1 Timing Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.6 Port Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.7 Analog-to-Digital Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.8 Communication Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.8.1 Serial Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.8.2 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.8.3 Serial Communication Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.8.4 Handshake Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.8.5 RS-232 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.9 Interrupt System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.10 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.11 Choosing a Microcontroller for a Specific Design . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.11.1 System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.12 Microcontroller Vendors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.13 Cutting Edge Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4. Timing Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.1.1 Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.1.2 Period . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.1.3 Duty Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2 Timer System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.2.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.2.2 Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.3.1 Measuring External Timing Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.3.2 Counting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.3.3 Generating Timing Signals to Interface External Devices . . . . . . . . . . . . 59
4.3.4 Industrial Implementation Case Study (PWM) . . . . . . . . . . . . . . . . . . . . . 60
4.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
CONTENTS vii
5. Analog-to-Digital Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.1.1 Analog Signals Versus Digital Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.1.2 Sampling, Quantization, and Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.1.3 Resolution and Data Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.2 Analog-to-Digital Conversion Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
5.3 ADC Conversion Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.3.1 Successive-Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5.3.2 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.3.3 Counter-Based Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.3.4 Parallel Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.4.1 Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.4.2 Signal Conditioning for ATD Converters . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.4.3 Digital-to-Analog Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.4.4 Industrial Implementation: Digital Cameras . . . . . . . . . . . . . . . . . . . . . . . . 81
5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6. Networked Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.1 Background Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.1.1 Designing Computer Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.1.2 Types of Networks and Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.2 Microcontroller Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
6.2.1 Controller Area Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
6.2.2 BDLC Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.2.3 Customized Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.3.1 Automobiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.3.2 Mobile Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Preface ................................................................................................................. ix
A Quick Look at the Microcontrollers .................................................................................. x
Atmel’s AVR ................................................................................................................. x
Microchip’s PIC ........................................................................................................... xii
What’s on the CD-ROM? .................................................................................. xvi
v
Contents
vi
Contents
Chapter 10: UDP and the Easy Ethernet CS8900A .............................................. 221
A UDP Internet Test Panel ............................................................................................... 223
Chapter 13: Putting the Easy Ethernet AVR Online ........................................... 337
vii
Contents
Introduction ix
3 Introductory projects 29
LED_Flasher2 29
SOS 30
Code for SOS circuit 30
Flashing 8 LEDs 33
Chasing 8 LEDs 35
Traffic lights 39
More than 8 outputs 45
5 Using inputs 64
Switch flowchart 66
Program development 67
Scanning (using multiple inputs) 73
Switch scanning 73
Control application – a hot air blower 77
7 Keypad scanning 93
Programming example for the keypad 94
14 Interrupts 207
Interrupt sources 208
Interrupt control register 208
Program using an interrupt 209
18 Projects 257
Project 1 Electronic dice 257
Project 2 Reaction timer 266
Project 3 Burglar alarm 272
Fault finding 282
Development kits 285
Index 307
Contents
Low-End 8
Mid-Range 10
PIC17Cxx 14
PIC18Cxx 17
Parameters 21
Low-End Instruction Set 22
Mid-Range Instruction Set 31
PIC17Cxx Instruction Set 40
PIC18Cxx Instruction Set 63
Microchip Special Instruction Mnemonics 104
Parallax PICmicro® MCU Instruction Set 111
Copyright 2001 The McGraw-Hill Companies. Click Here for Terms of Use.
vi Contents
Power 277
Reset 281
Digital Logic Interfacing 282
viii Contents
Index 667
Contents
Introduction xxi
Acknowledgements xxv
Summary 64
References 64
Summary 262
References 262
14 Introducing C 386
14.1 The main idea – why C? 387
14.2 An introduction to C 387
14.2.1 A little history 387
14.2.2 A first program 388
14.2.3 Laying out the program – declarations, statements, comments and space 388
14.2.4 C keywords 390
14.2.5 The C function 391
14.2.6 Data type and storage 392
14.2.7 C operators 392
14.2.8 Control of program flow, and the while keyword 393
xvi Contents
Index 549
Contents
List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VI
List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XI
1. Digital Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2. Logic Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Appendices
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
El Registro INTCON .......................................27
Indice PCL y PCLATH ...............................................28
STACK (PILA) .................................................28
Cap tulo 1 Registros INDF y FSR ....................................29
Arquitectura de un PIC PORTS I/O......................................................30
Registros PORTA y TRISA .............................30
Introducci n ......................................................5 Registros PORTB y TRISB.............................32
Caracter sticas del PIC .....................................5 M dulo Temporizador TIMER0 .......................34
Caracter sticas Perif ricas................................5 Prescaler (Divisor de Frecuencia) ..................35
Caracter sticas Especiales del Interrupci n de Timer0....................................36
Microcontrolador ...............................................6 Memoria de Datos EEPROM..........................36
Patas del PIC....................................................6 Lectura de la Memoria de
Resumen del Dispositivo ..................................9 Datos de EEPROM.........................................38
Ficha Did ctica ...............................................10 Escritura de la Memoria de
Lo que Debe Saber Datos EEPROM..............................................39
Sobre los PICs................................................13 Verificaci n de Escritura .................................40
a) El Procesador es segmentado La CPU del PIC ..............................................42
tipo "pipe line" .................................................13 Bits de Configuraci n......................................43
b) El Procesador es de tipo RISC ..................14 Configuraciones del Oscilador........................43
c) Poseen una arquitectura Oscilador de Cristal o
tipo Harvard ....................................................14 Resonador de Cer mica.................................44
d) Son de arquitectura ortogonal basada Oscilador RC...................................................44
en banco de registros .....................................15 Reset...............................................................44
Palabras de Configuraci n Reset de Encendido .......................................47
e Identificaci n................................................18 Temporizador de Arranque del
Organizaci n de la Memoria...........................19 Oscilador (OST)..............................................48
Organizaci n de la Memoria Temporizador de Encendido
de Programas .................................................19 (PWRT) ...........................................................48
Organizaci n de la Memoria El Perro Guardi n (WDT) ...............................48
de Datos..........................................................19 Interrupciones del Micro .................................49
Los Registros del PIC.....................................20 Interrupci n Externa (INT) ..............................51
Archivo de Registro de Interrupci n por Desborde
Uso General....................................................20 del TMR0 ........................................................51
Registros de Funciones Interrupci n por Cambio de
Especiales (SFR)............................................21 Estado en las patas RB7 y RB4 .....................51
Registro de Status ..........................................23 Interrupci n por Finalizaci n de la Escritura
Registro OPTION_REG..................................25 en la EEPROM de datos ................................51
2 Microcontroladores PIC
Indice
Dedico este libro a todos los que trabajan por una Argentina mejor, a los que creen que
es posible cambiar las cosas desde el trabajo y el esfuerzo colectivo, a los que creen
que las respuestas est n en casa... en esta hermosa y bendita Am rica Latina.
Dedico este libro a quienes saben construir en paz...
Dedico este libro a quienes integran Editorial Quark, Imprenta Rosgal y Centro Japon s
de Informaci n Electr nica.
Horacio Daniel Vallejo
Microcontroladores PIC 3
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
What’s on the CD-ROM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
Chapter 1: What Are Intelligent Sensors, and
Why Should I Care about Them? . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Conventional Sensors Aren’t Perfect . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 First Things First—Digitizing the Sensor Signal. . . . . . . . . . . . . . . . . . 7
1.3 Next Step—Add Some Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 Finish Up with Quick and Reliable Communications . . . . . . . . . . . . 8
1.5 Put It All Together, and You’ve Got an Intelligent Sensor . . . . . . . . . . 9
1.6 Why Don’t We Make Everything Intelligent? . . . . . . . . . . . . . . . . . . 11
1.7 Real-world Examples of Intelligent Sensors . . . . . . . . . . . . . . . . . . . 13
1.8 Outline of the Remainder of the Book . . . . . . . . . . . . . . . . . . . . . . 16
Chapter 2: Intuitive Digital Signal Processing . . . . . . . . . . . . . 21
2.1 Foundational Concepts for Signal Processing . . . . . . . . . . . . . . . . . . 21
2.2 Issues Related to Signal Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.3 How to Analyze a Sensor Signal Application . . . . . . . . . . . . . . . . . . 47
2.4 A General Sensor Signal-processing Framework . . . . . . . . . . . . . . . . 48
2.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 3: Underneath the Hood of the dsPIC DSC. . . . . . . . . 53
3.1 The dsPIC DSC’s Data Processing Architecture . . . . . . . . . . . . . . . . 55
3.2 Interrupt Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
vii
viii Contents
1 C Fundamentals
Structure of C Programs
Components of a C Program
#pragma
main()
#include
printf Function
Variables
Constants
Comments
Functions
C Keywords
2 Variables
Data Types
Variable Declaration
Variable Assignment
Enumeration
typedef
Type Conversions
3 Functions
Functions
Function Prototypes
Using Function Arguments
Using Function to Return Values
Classic and Modern Function Declarations
4 Operators
Arithmetic
Relational
Logical
Bitwise
4
Increment and Decrement
Precedence of
6 Arrays / Strings
One Dimensional Arrays
Strings
Multidimensional Arrays
Initializing Arrays
Arrays of Strings
7 Pointers
Pointer Basics
Pointers and Arrays
Passing Pointer to Functions
8 Structures / Unions
Structure Basics
Pointers to Structures
Nested Structures
Union Basics
Pointers to Unions
9 PICmicro®MCU Specific C
Inputs and Outputs
Mixing C and Assembler
Advanced BIT Manipulation
Timers
A/D Conversion
Data Communications
I2C Communications
SPI Communications
PWM
LCD Driving
5
Interrupts
Include Libraries
Additional Information
6
Table of Contents
1. Introduction 1
1.1 Typographical Conventions....................................................................... 1
1.2 Explaining the Microcontroller ................................................................. 2
1.3 Book Contents ........................................................................................... 3
2. Microcontroller Overview 5
2.1 What is a Microcontroller?......................................................................... 5
2.2 The Microcontroller in a System............................................................... 7
2.3 Architecture ............................................................................................... 7
2.3.1 Von Neumann ......................................................................................................................8
2.3.2 Von Neumann Memory Map.............................................................................................8
2.3.3 Harvard ..................................................................................................................................9
2.3.4 Harvard Memory Map.......................................................................................................10
2.3.5 The Central Processing Unit ............................................................................................11
2.3.6 Central Processing Unit.....................................................................................................13
2.3.7 ROM.....................................................................................................................................14
2.3.8 RAM .....................................................................................................................................15
2.3.9 I/O Ports.............................................................................................................................16
2.3.10 Timer..................................................................................................................................17
2.3.11 Interrupt Circuitry............................................................................................................18
2.3.12 Buses ..................................................................................................................................19
2.4 Sample Microcontroller Configurations.................................................. 19
2.4.1 Motorola MC68HC705C8 ................................................................................................19
2.4.2 National Semiconductor COP8SAA7 ............................................................................20
2.4.3 Microchip PIC16C54.........................................................................................................20
2.4.4 Microchip PIC16C74.........................................................................................................21
i
Table of Contents
ii
Table of Contents
3.10 Devices................................................................................................... 41
3.10.1 Mask ROM........................................................................................................................41
3.10.2 Windowed Parts ...............................................................................................................41
3.10.3 OTP....................................................................................................................................41
4. Programming Fundamentals 43
4.1 What is a Program?.................................................................................. 43
4.2 Number Systems ..................................................................................... 43
4.3 Binary Information.................................................................................. 44
4.4 Memory Addressing ................................................................................ 46
4.5 Machine Language.................................................................................. 46
4.6 Assembly Language ................................................................................ 46
4.6.1 Assembler ............................................................................................................................47
4.7 Instruction Sets........................................................................................ 47
4.8 The Development of Programming Languages ..................................... 48
4.9 Compilers ................................................................................................ 50
4.9.1 The Preprocessor ...............................................................................................................50
4.9.2 The Compiler ......................................................................................................................50
4.9.3 The Linker ...........................................................................................................................50
4.10 Cross Development................................................................................ 51
4.10.1 Cross compiler..................................................................................................................51
4.10.2 Cross development tools ................................................................................................51
4.10.3 Embedded Development Cycle.....................................................................................52
iii
Table of Contents
6. C Program Structure 65
6.1 C Preprocessor Directives........................................................................65
6.2 Identifier Declaration ..............................................................................65
6.2.1 Identifiers in Memory ....................................................................................................... 66
6.2.2 Identifier names ................................................................................................................. 66
6.2.3 Variable Data Identifiers................................................................................................... 67
6.2.4 Constant Data Identifiers ................................................................................................. 67
6.2.5 Function Identifiers........................................................................................................... 68
6.3 Statements ...............................................................................................68
6.3.1 The Semicolon Statement Terminator ........................................................................... 69
6.3.2 Combining Statements in a Block................................................................................... 69
iv
Table of Contents
v
Table of Contents
9. Control Structures 99
9.1 Conditional Expressions..........................................................................99
9.2 Decision Structures................................................................................ 100
9.2.1 if and else Statements...................................................................................................... 100
9.2.2 Nested if statements........................................................................................................ 101
9.2.3 Matching else and if......................................................................................................... 102
9.2.4 switch and case................................................................................................................. 103
9.2.5 Execution within a switch .............................................................................................. 103
9.2.6 Fall-through execution.................................................................................................... 104
9.2.7 The default case ............................................................................................................... 105
9.2.8 The goto Statement ......................................................................................................... 105
9.2.9 Comparing goto and switch..case.................................................................................. 106
9.3 Looping Structures ................................................................................ 106
9.3.1 Control expression .......................................................................................................... 106
9.3.2 The while loop.................................................................................................................. 107
9.3.3 The do loop ...................................................................................................................... 107
9.3.4 The for loop...................................................................................................................... 108
9.3.5 How the for loop works ................................................................................................. 108
9.4 Exiting a Loop....................................................................................... 109
9.4.1 The break Statement ....................................................................................................... 109
9.4.2 The continue Statement.................................................................................................. 109
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
x
Contents
Preface................................................................................................................. xv
Introduction ...................................................................................................... xvii
Who should read this book? ...................................................................................................xvii
Structure of the book .............................................................................................................xviii
What this book is not ............................................................................................................... xix
Checklists ................................................................................................................................ xix
PART I
CHAPTER 1: THE FIRST FLIGHT................................................................................... 3
Flight plan ............................................................................................................. 3
Preflight checklist ................................................................................................. 4
The flight............................................................................................................... 4
Compiling and linking ................................................................................................................ 6
Building the first project ............................................................................................................. 7
PORT initialization ..................................................................................................................... 9
Retesting PORTA...................................................................................................................... 10
Testing PORTB ......................................................................................................................... 10
Post-flight briefing ............................................................................................. 12
Notes for assembly experts ............................................................................... 13
Notes for PIC MCU experts ................................................................................ 14
Notes for C experts ............................................................................................ 14
Tips and tricks ..................................................................................................... 14
Exercises .............................................................................................................. 15
Books ................................................................................................................. 15
Links ................................................................................................................. 15
vii
Contents
viii
Contents
Math libraries............................................................................................................................ 51
Complex data types .................................................................................................................. 51
Exercises .............................................................................................................. 52
Books ................................................................................................................. 52
Links ................................................................................................................. 52
ix
Contents
Books ................................................................................................................. 83
Links ................................................................................................................. 83
x
Contents
xi
Contents
xii
Contents
xiii
Contents
xiv
Contents
Section 1
Display controller experiments
Section 2
Interfacing with keypads
Section 3
Experimenting with serial eeproms
Section 4
Experimenting with analog to digital converters
Section 5
Experimenting with digital to analog converters
Section 6
Experimenting with remote control
Section 7
Temperature measurement experiments
Section 8
Experimenting with robotics
Section 9
Experimenting with audio control devices
Section 10
Programming techniques
Section 11
Powering up the PIC
#ONTENTS
0REFACE X
!CKNOWLEDGMENTS XII
7HATS ON THE #$
2/- XIII
#(!04%2 7HAT IS A 0)#
0)#S hv
(OW $O ) 4ELL 4HEM !PART
7HICH /NE 3HOULD ) 5SE
(OW $O ) 0ICK /NE
3O 7HICH /NE $O ) 2EALLY 7ANT TO 5SE
"ASIC -ICROS -"ASIC #OMPILER
2EFERENCES
#(!04%2 -"ASIC #OMPILER AND $EVELOPMENT "OARDS
4HE #OMPILER 0ACKAGE
"!3)# AND )TS %SSENTIALS
$EVELOPMENT "OARDS
0ROGRAMMING 3TYLE
"UILDING THE #IRCUITS AND 3TANDARD !SSUMPTIONS
0INS 0ORTS AND )NPUT/UTPUT
0SEUDO
#ODE AND 0LANNING THE 0ROGRAM
)NSIDE THE #OMPILER
2EFERENCES
#(!04%2 4HE "ASICS n /UTPUT
0IN !RCHITECTURES
,%$ )NDICATORS
3WITCHING )NDUCTIVE ,OADS
,OW 3IDE 3WITCHING
)SOLATED 3WITCHING
3PECIAL 0URPOSE 3WITCHING
&AST 3WITCHING3OUND FROM A 0)#
2EFERENCES
#(!04%2 4HE "ASICS n $IGITAL )NPUT
)NTRODUCTION
3WITCH "OUNCE AND 3EALING #URRENT
)SOLATED 3WITCHING
V
#ONTENTS
2EADING A +EYPAD
2EFERENCES
#(!04%2 ,#$ -ODULES
3ELECTING A $ISPLAY
6&$ $ISPLAYS
#ONNECTION TO 0)#
(ELLO 7ORLD
,#$ -ODULE -EMORY 3HIFTS AND ,INES
&ONT 3ELECTION
#USTOM #HARACTERS
2EFERENCES
#(!04%2 2EADING #OMPLEX )NPUT 3WITCHES
0IN 3AVING 4ECHNIQUES
2OTARY %NCODERS
2EADING A 2ELATIVE %NCODER
$UAL %NCODERS AND ,#$
2EFERENCES
#(!04%2 3EVEN
3EGMENT ,%$ $ISPLAYS
,%$ $ISPLAY 3ELECTION
#IRCUIT $ESIGN
2EFERENCES
#(!04%2 )NTRODUCTORY 3TEPPER -OTORS
3TEPPER -OTOR "ASICS
0ROGRAMS
2EFERENCES
#(!04%2 23
3ERIAL )NTERFACE
(OW TO #ONNECT TO 9OUR 0#
6OLTAGE ,EVELS IN 23
AND ,EVEL #ONVERSION
3TANDARD 0IN #ONNECTIONS
!SYNCHRONOUS 4RANSMISSION 3TART "ITS 3TOP "ITS AND "IT /RDER
-"ASICS 0ROCEDURES FOR 3ERIAL #OMMUNICATIONS
0ROGRAMS
2EFERENCES
#(!04%2 )NTERRUPTS AND 4IMERS IN -"ASIC
)NTERRUPTS AND 4IMERS/VERVIEW
)NTERRUPTS
4IMERS
#APTURE AND #OMPARE
2EFERENCES
#(!04%2 !NALOG
TO
$IGITAL #ONVERSION
)NTRODUCTION TO !NALOG
TO
$IGITAL #ONVERSION
2ESOLUTION AND !CCURACY
3ELF
#ONTAINED $6-
2EFERENCES
VI
#ONTENTS