Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CHAPTER I INTRODUCTION TO
MICROCONTROLLERS
CHAPTER V MPLAB
Introduction
Introduction
History
Microcontrollers versus microprocessors
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
Memory unit
Central processing unit
Buses
Input-output unit
Serial communication
Timer unit
Watchdog
Analog to digital converter
Program
CHAPTER II MICROCONTROLLER
PIC16F84
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
Introduction
CISC, RISC
Applications
Clock/instruction cycle
Pipelining
Pin description
6.3 Samples
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
Introduction
Contents
Preface
1 Introduction
1.1
1.2
1.3
1.4
1.5
xi
1
1
2
5
6
7
7
8
9
11
11
12
12
13
14
15
17
20
21
21
22
23
24
25
27
27
28
32
vi
CONTENTS
37
42
44
44
49
52
52
57
57
58
59
59
59
60
60
61
63
63
63
65
65
66
67
71
75
76
77
78
78
78
79
79
79
79
79
80
80
80
81
82
82
83
83
CONTENTS
4.8
4.9
4.10
4.11
4.12
4.13
4.14
4.15
4.16
4.17
4.18
4.19
4.20
4.21
4.22
4.23
4.24
vii
83
83
84
86
86
89
89
91
91
92
94
95
95
96
96
96
97
98
99
101
101
103
103
104
104
105
105
107
107
109
111
116
117
119
119
120
121
121
123
129
129
131
131
136
viii
CONTENTS
137
137
138
138
139
139
140
140
140
140
143
145
154
154
156
162
166
166
169
171
171
174
177
178
178
179
181
183
184
187
187
189
192
194
201
205
208
211
213
214
215
217
CONTENTS
ix
219
222
230
233
233
134
137
240
243
243
243
245
246
249
250
253
254
260
263
263
267
268
269
11.1
11.2
11.3
11.4
11.5
269
270
273
274
278
Appendix A
Table of z-Transforms
283
Appendix B
MATLAB Tutorial
285
Index
307
Contents
Preface .................................................................................................................. ix
Whats 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
Contents
Dumping Linksys WCF12 Tuples ................................................................................................ 65
Dumping Netgear MA701 Tuples............................................................................................... 68
Dumping Zonet ZCF100 Tuples.................................................................................................. 70
Enabling the 802.11b CompactFlash NIC................................................................................... 74
The Value of Parsing the CIS...................................................................................................... 77
Full Throttle ............................................................................................................................... 77
Chapter 11: Processing 802.11b Frames with the AirDrop ............................ 167
AirDrop Frame Structure.......................................................................................................... 168
AirDrop-P Frame Reception...................................................................................................... 184
Chapter 13: Flying Cargo with UDP and the AirDrop..................................... 243
Running a UDP Application on the AirDrop-P........................................................................... 243
The EDTP Internet Test Panel and the Code Behind It ............................................................... 245
Exercising the AirDrop-P with the EDTP Internet Test Panel....................................................... 249
Notes ...................................................................................................................................... 274
Chapter 14: Flying Cargo with TCP/IP and the AirDrop.................................. 275
TCP and the AirDrop-P ............................................................................................................ 275
The TCP/IP Stacks Physical Layer .............................................................................................. 284
The TCP/IP Stacks Data Link Layer ........................................................................................... 284
vi
Contents
The TCP/IP Stacks Network Layer............................................................................................. 284
The TCP/IP Stacks Transport Layer ........................................................................................... 284
The TCP/IP Stacks Application Layer ........................................................................................ 285
TCP/IP The Big Ugly .............................................................................................................. 285
Youve Done It! ....................................................................................................................... 334
Chapter 16: A New Kid in Town Who Calls Himself ZigBee........................... 361
Zig What???............................................................................................................................ 361
Making ZigBee Talk ................................................................................................................. 363
The Microchip ZigBee Stack ..................................................................................................... 366
vii
Contents
Part 1 Microcontroller
1 PIC Hardware
1
3
Processor System
18
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
79
Switch Input
79
Switch Debouncing
81
84
Keypad Input
87
88
90
xii
Data Processing
101
Number Systems
101
Conversion
106
Variable Types
110
Arithmetic
112
121
Calculator
121
Pulse Output
128
Period Measurement
130
Analogue Interfacing
141
8-bit Conversion
141
10-bit Conversion
145
Amplifier Interfaces
149
Contents
Part 3
160
Instrumentation Amplifier
161
Current Loop
163
Comparators
165
Op-amp Selection
168
Analogue Output
168
Systems
177
8 Power Outputs
179
Current Drivers
179
183
185
Motor Interfacing
189
9 Serial Communication
201
USART
201
SPI
205
IC
10 Sensor Interfacing
210
223
Sensors
223
Sensor Types
228
Amplifier Design
236
Weather Station
238
xiii
Contents
xiv
11 System Design
249
Base System
249
Memory System
259
266
System Design
270
274
279
291
CONTENIDO
Prologo
1. Microcontroladores programables: La solucin esta en un chip
2: Microcontroladores de 8 bits:
3: Los sellos mgicos de parallax
4: Pbasic: El lenguaje mas fcil 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 diseo de
proyectos
11: Ejercicios de programacin en ensamblador
12: Los primeros diseos prcticos.
A: Gua Rpida del PIC16X84
B: Gua del usuario del SIM2000
C: Breve descripcin del Micro PIC Trainer
D: La familia PIC al completo
E: Contenido del CD
F: Proyectos con el PIC16F84 Bibliografa y direcciones de inters relacionadas
con los PIC ndice
Table of Contents
Preface
xv
The Atom
Isotopes and Ions
Static Electricity
Electrical Charge
1
2
3
4
1.3.1
1.3.2
1.3.3
1.3.4
4
4
5
5
Voltage
Current
Power
Ohm's Law
Resistors
Revisiting Ohm's Law
Resistors in Series and Parallel
Capacitors
Capacitors in Series and in Parallel
Inductors
Transformers
1.6 Semiconductors
1.6.1
1.6.2
1.6.3
1.6.4
Integrated Circuits
Semiconductor Electronics
P-Type and N-Type Silicon
The Diode
6
6
8
9
9
10
12
13
14
15
15
16
16
17
17
19
19
19
20
20
22
22
23
vi
Microcontroller Programming
23
24
24
24
25
25
26
27
2.4.1 Binary-to-ASCII-Decimal
2.4.2 Binary-to-Hexadecimal Conversion
2.4.3 Decimal-to-Binary Conversion
28
29
29
33
33
33
3.1.1 ASCII
3.1.2 EBCDIC and IBM
3.1.3 Unicode
34
36
36
Fixed-Point Representations
Floating-Point Representations
Standardized Floating-Point Representations
IEEE 754 Single Format
Encoding and Decoding Floating-Point Numbers
37
37
38
39
40
41
44
45
46
47
48
50
51
52
55
55
55
56
56
4.1.1
4.1.2
4.1.3
4.1.4
Logical
Logical
Logical
Logical
AND
OR
XOR
NOT
57
57
57
58
58
58
60
62
62
63
63
Contents
vii
64
64
65
67
67
69
70
71
72
72
73
73
75
76
77
77
78
79
81
81
83
84
85
5.4.1
5.4.2
5.4.3
5.4.4
5.4.5
5.4.6
5.4.7
5.4.8
Inverter Gates
The AND Gate
The NAND Gate
The OR Gate
The NOR Gate
Positive and Negative Logic
The XOR Gate
Schmitt Trigger Inverter
The RS Flip-flop
Clocked RS Flip-flop
The D Flip-flop
The Edge-triggered D Flip-flop
Preset and Clear Signals
D Flip-flop Waveform Action
Flip-flop Applications
6.2 Clocks
6.2.1 Clock Waveforms
6.2.2 The TTL Clock
6.2.3 The 555 Timer
86
87
87
88
88
89
90
91
93
93
95
95
96
96
98
99
100
101
102
103
103
104
105
106
viii
Microcontroller Programming
Frequency Dividers
The JK Flip-flop Counter
Ripple Counters
Decoding Gates
Synchronous Counters
Counter ICs
Shift Registers
Harvard Architecture
RISC CPU Design
Single-word Instructions
Instruction Format
Mid-Range Device Versions
106
107
107
107
108
110
110
112
113
115
115
118
118
118
118
120
121
122
122
124
125
129
129
130
131
131
132
134
134
135
135
138
138
139
139
139
141
142
142
143
143
144
145
145
145
147
148
149
Contents
ix
I/O Ports
Timer Modules
Capture-and-Compare Module
Master Synchronous Serial Port (MSSP) Module
USART Module
A/D Module
Installing MPLAB
Creating the Project
Project Build Options
Building the Project
9.3 Programmers
9.4 Engineering PIC Software
9.4.1 Using Program Comments
Program Header
Commented Banners
Commented Bitmaps
9.4.2 Defining Data Elements
The cblock Directive
9.4.3 Banking Techniques
The banksel Directive
Bank Selection Macros
Deprecated Banking Instructions
9.4.4 Processor and Configuration Controls
Configuration Bits
9.4.5 Naming Conventions
9.4.6 Errorlevel Directive
149
151
153
153
154
154
154
154
155
157
158
158
159
160
160
161
161
161
163
163
164
165
165
167
169
169
170
171
172
174
174
175
176
176
177
178
179
179
180
180
180
181
182
182
184
186
186
Microcontroller Programming
189
189
191
191
191
192
193
194
194
194
196
199
202
204
206
206
208
Chapter 11 - Interrupts
11.0 Interrupts on the 16F84
11.0.1 The Interrupt Control Register
11.0.2 The OPTION Register
211
211
211
212
213
214
214
215
217
217
217
218
218
218
219
220
221
222
223
224
225
227
229
229
232
235
241
241
241
Contents
xi
Timer0 Interrupt
Timer0 Prescaler
242
242
243
244
245
245
246
247
247
248
248
250
252
255
259
260
260
260
263
265
269
275
275
276
276
276
276
276
276
277
277
278
278
278
279
280
281
281
281
283
283
283
284
284
284
284
285
285
285
285
xii
Microcontroller Programming
Write data
Read data
13.2.2 A 16F84 8-bit Data Mode Circuit
Chapter 14 - Communications
14.0 PIC Communications Overview
14.1 Serial Data Transmission
14.1.1
14.1.2
14.1.3
14.1.4
285
286
286
287
287
289
290
290
291
291
292
292
293
293
293
295
295
296
298
299
301
302
302
304
306
306
316
327
339
339
340
340
342
342
343
344
344
345
345
346
347
349
350
350
351
351
352
352
354
360
Contents
xiii
361
364
366
366
367
368
368
369
371
375
376
376
379
380
381
381
384
386
389
SerialSnd Program
SerialRcv Program
Serial6465 Program
TTYUsart Program
SerComLCD Program
SerIntLCD Program
389
394
400
404
420
438
459
460
460
460
461
462
465
467
467
469
469
475
476
476
477
478
478
479
480
482
486
486
488
490
492
xiv
Microcontroller Programming
492
504
521
543
544
544
545
546
547
549
549
550
552
554
554
558
558
560
565
568
568
580
595
613
615
621
659
Index
795
Contents
1.
2.
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
3.4
3.5
3.6
3.7
3.8
3.9
3.10
3.11
3.12
3.13
3.14
4.
Bus Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.1 Address Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.2 Data Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3.3 Control Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Time Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.5.1 Timing Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Port Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Analog-to-Digital Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
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
Interrupt System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Choosing a Microcontroller for a Specic Design . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.11.1 System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Microcontroller Vendors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Cutting Edge Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
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
7.
viii
CONTENTS
7.3
7.4
7.5
Contents
Preface ................................................................................................................. ix
A Quick Look at the Microcontrollers .................................................................................. x
Atmels AVR ................................................................................................................. x
Microchips PIC ........................................................................................................... xii
Contents
Chapter 5: Using Microcontroller USARTs ............................................................. 47
Some Interrupt-Driven USART Code ................................................................................. 50
Applying What We Know about RS-232 to the Atmel AVR ............................................... 70
Coding the AVR RS-232 Routines ............................................................................... 73
vi
Contents
CS8900A-CQ Status and Control Registers ............................................................... 159
Did It Register? ........................................................................................................ 172
Chapter 13: Putting the Easy Ethernet AVR Online ........................................... 337
Chapter 14: Finale ................................................................................................. 347
Obtaining Easy Ethernet Devices ..................................................................................... 348
Contents
Introduction
ix
1
2
3
3
4
5
6
6
9
11
12
12
12
13
14
16
19
23
26
26
27
Introductory projects
29
LED_Flasher2
SOS
Code for SOS circuit
Flashing 8 LEDs
Chasing 8 LEDs
Traffic lights
More than 8 outputs
29
30
30
33
35
39
45
47
47
48
49
vi Contents
5
10
11
Using inputs
64
Switch flowchart
Program development
Scanning (using multiple inputs)
Switch scanning
Control application a hot air blower
66
67
73
73
77
82
The 16F84
16F84 memory map
The 16F818
82
87
88
Keypad scanning
93
94
Program examples
110
Counting events
Look up table
7-Segment display
Numbers larger than 255
Long time intervals
One hour delay
110
115
115
126
133
136
139
139
142
143
144
145
146
146
160
161
162
163
166
167
168
171
174
176
178
Contents
12
13
14
15
16
17
vii
186
189
199
200
Interrupts
207
Interrupt sources
Interrupt control register
Program using an interrupt
208
208
209
216
216
216
217
217
218
219
220
220
222
225
229
229
230
232
233
236
245
245
247
247
248
248
250
252
253
255
255
242
243
viii
18
19
Contents
Projects
257
257
266
272
282
285
287
287
289
292
Appendix
Appendix
Appendix
Appendix
Index
A Microcontroller data
B Electrical characteristics
C Decimal, binary and hexadecimal numbers
D Useful contacts
299
301
303
306
307
Contents
7
8
10
14
17
21
Parameters
Low-End Instruction Set
Mid-Range Instruction Set
PIC17Cxx Instruction Set
PIC18Cxx Instruction Set
Microchip Special Instruction Mnemonics
Parallax PICmicro MCU Instruction Set
21
22
31
40
63
104
111
Copyright 2001 The McGraw-Hill Companies. Click Here for Terms of Use.
vi
Contents
123
123
125
126
128
128
130
130
132
133
134
135
136
137
138
139
140
142
142
144
146
147
150
150
151
154
169
186
Contents
Chapter 7. Built-In Hardware Features
Configuration Registers
Oscillators
Sleep
Option Register
Input/Output Ports and TRIS Registers
Watchdog Timer
TMR0
Prescaler
TMR1
TMR2
Compare/Capture/PWM(CCP) Module
USART Module
SSP Module
SPI operation
I2C operation
Built-In ADC
Built-In Comparators
Parallel Slave Port
Built-In EEPROM Data Memory Access
EPROM Program Memory Access
Flash Program Memory Access
External Parallel Memory
vii
207
207
208
213
216
217
221
222
223
224
227
228
232
239
239
244
252
257
261
263
269
271
273
277
277
281
282
viii
Contents
282
284
287
291
292
293
293
296
334
338
345
347
357
363
365
370
371
373
374
374
379
379
381
385
394
398
402
405
406
Contents
Chapter 10. PC Interfaces
Memory Map
I/O Space Map
Interrupt Function by Number
ISA Bus
ISA pinouts
Interrupts
Keyboard and Mouse Ports
Connector specification
Keyboard operation with timing diagrams
Keyboard scan codes
Keyboard controller commands
BIOS interfaces
Keyboard commands
Serial Port
Connector pinouts
8250 block diagram
Serial port base addresses
8250 registers
Interrupts
Interrupt 14hRS-232 communications APIs
Parallel Port
Block diagram/connector
Base registers
Registers
Data output waveform
BIOS interfaces
ix
407
407
407
422
425
425
429
430
430
431
432
436
436
441
441
442
443
443
444
449
451
455
455
457
457
458
459
461
461
462
465
Contents
Time Delays
Negating the Contents of a Register
Incrementing/Decrementing w
Rotating a Byte in Place
Copy Bits from One Register to Another
Converting a Nybble to ASCII
Converting an ASCII Byte to a Hex Nybble
Using T0CKI as an Interrupt Source Pin
Dividing by Three
Sixteen-Bit Pulse Measurement with
5-Cycle Delay
Detect a Change in a Register
Test a Byte within a Range
Convert ASCII to Upper Case
Swap the Contents of w with a Register
Swap the Contents of Two Registers
Compare and Swap if Y X
Counting the Number of 1s in a Byte
Generating Parity for a Byte
Keeping a Variable within a Range
Swapping Bit Pairs
Bitwise Operations
Constant Multiplication
Constant Division
466
467
468
469
470
470
471
471
472
473
474
474
475
475
475
476
476
477
477
478
478
479
480
483
483
484
485
488
Contents
Comparisons with 16-Bit Variables
Multiplication
Division
xi
489
492
495
499
500
500
501
502
504
507
517
517
518
519
525
526
536
536
539
540
542
549
xii
Contents
Visual Basic
MSComm control
The C Language
Declarations
Statements
Operators
Directives
Backslash characters
Common C functions
PICmicro MCU enhancement functions
578
581
588
588
590
593
595
598
598
603
605
605
629
606
607
609
612
612
612
615
616
616
622
625
626
626
629
630
632
Contents
Digital Logic
Gates
Flip flops
Index
xiii
632
632
633
635
635
636
638
638
640
641
641
642
643
648
656
658
659
660
661
661
661
662
663
663
663
664
664
665
665
667
Contents
Introduction
Acknowledgements
Section 1
1
Section 2
2
xxi
xxv
1
3
3
3
4
4
5
6
7
8
9
9
10
10
11
11
12
13
14
15
15
15
17
18
20
22
22
23
25
2.1
25
25
vi
Contents
2.1.2 The 16F84A
2.1.3 A caution on upgrades
2.2 An architecture overview of the 16F84A
2.2.1 The Status register
2.3 A review of memory technologies
2.3.1 Static RAM (SRAM)
2.3.2 EPROM (Erasable Programmable Read-Only Memory)
2.3.3 EEPROM (Electrically Erasable Programmable Read-Only Memory)
2.3.4 Flash
2.4 The 16F84A memory
2.4.1 The 16F84A program memory
2.4.2 The 16F84A data and Special Function Register memory (RAM)
2.4.3 The Configuration Word
2.4.4 EEPROM
2.5 Some issues of timing
2.5.1 Clock oscillator and instruction cycle
2.5.2 Pipelining
2.6 Power-up and Reset
2.7 What others do the Atmel AT89C2051
2.8 Taking things further the 16F84A on-chip reset circuit
Summary
References
27
27
27
29
29
30
31
31
31
32
32
33
35
35
37
37
38
38
40
41
44
44
45
3.1
3.2
46
46
46
49
49
52
52
53
55
55
55
56
59
59
60
60
61
61
62
63
3.3
3.4
3.5
3.6
3.7
Contents
vii
Summary
References
64
64
65
4.1
66
66
67
68
69
69
70
71
71
71
72
72
73
73
76
76
76
77
77
77
79
80
81
81
81
82
82
83
86
87
88
88
89
5.1
89
89
91
92
92
94
5.2
viii
Contents
5.3
5.4
95
97
97
98
99
101
102
102
102
102
104
106
106
107
108
109
109
110
110
112
112
115
116
116
116
116
117
117
117
117
117
118
118
119
119
120
6.1
121
121
122
124
125
125
126
6.2
Contents
Stopping interrupts from wrecking your
program 1 context saving
6.2.4 Stopping interrupts from wrecking your program 2 critical
regions and masking
6.3 The main idea counters and timers
6.3.1 The digital counter reviewed
6.3.2 The counter as timer
6.3.3 The 16F84A Timer 0 module
6.4 Applying the 16F84A Timer 0, with examples using the electronic ping-pong
6.4.1 Object or event counting
6.4.2 Hardware-generated delays
6.5 The Watchdog Timer
6.6 Sleep mode
6.7 What others do
6.8 Taking things further interrupt latency
Summary
ix
6.2.3
Section 3
7
127
130
131
131
132
134
136
136
137
138
139
140
141
142
143
145
7.1
7.2
146
146
146
147
150
150
150
152
154
155
155
156
158
158
159
161
161
161
161
161
163
164
164
7.3
7.4
7.5
7.6
7.7
Contents
7.8
165
165
167
170
170
171
172
172
173
174
184
8.1
8.2
184
187
187
188
193
193
8.3
8.4
8.5
8.6
8.7
176
176
178
179
180
182
183
194
199
199
200
203
203
204
204
205
205
207
207
207
208
212
Contents
xi
8.8
212
212
214
215
215
217
218
220
220
222
223
223
225
9.1
9.2
225
226
226
226
228
231
232
232
234
235
235
235
237
237
237
239
241
244
245
248
249
249
252
252
252
255
258
260
261
262
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11
9.12
9.13
xii
10
Contents
Summary
References
262
262
263
263
265
265
266
266
267
267
268
268
270
271
273
275
275
275
275
276
277
277
281
283
286
286
286
290
292
293
293
293
295
295
295
298
299
300
302
302
Contents
11
xiii
303
303
303
303
304
11.1
304
305
306
308
308
309
310
311
312
312
313
317
318
319
319
319
319
320
321
321
322
323
324
324
326
326
327
327
329
329
329
331
331
331
332
332
332
xiv
Contents
Section 4
12
333
335
12.1 The main idea the PIC 18 Series and the 18FXX2
12.2 The 18F2X2 block diagram and Status register
12.3 The 18 Series instruction set
12.3.1 Instructions which are unchanged
12.3.2 Instructions which have been upgraded
12.3.3 New, variant, instructions
12.3.4 New instructions
12.4 Data memory and Special Function Registers
12.4.1 The data memory map
12.4.2 Access RAM
12.4.3 Indirect addressing and accessing tables in data memory
12.5 Program memory
12.5.1 The program memory map
12.5.2 The Program Counter
12.5.3 Upgrading from the 16 Series and computed goto instructions
12.5.4 The Configuration registers
12.6 The Stacks
12.6.1 Automatic Stack operations
12.6.2 Programmer access to the Stack
12.6.3 The Fast Register Stack
12.7 The interrupts
12.7.1 An interrupt structure overview
12.7.2 The interrupt sources, their enabling and prioritisation
12.7.3 Overall interrupt prioritisation enabling
12.7.4 Global enabling
12.7.5 Other aspects of the interrupt logic
12.7.6 The Interrupt registers
12.7.7 Context saving with interrupts
12.8 Power supply and reset
12.8.1 Power supply
12.8.2 Power-up and Reset
12.9 The oscillator sources
12.9.1 LP, XT, HS and RC oscillator modes
12.9.2 EC, ECIO and RCIO oscillator modes
12.9.3 HS + PLL oscillator mode
12.9.4 Clock source switching
12.10 Introductory programming with the 18F242
12.10.1 Using the MPLAB IDE for the 18 Series
12.10.2 The Fibonacci program
Summary
References
336
337
340
344
344
345
345
345
345
347
347
347
349
349
349
350
352
352
352
352
353
353
353
354
354
355
355
356
358
358
358
360
362
363
363
363
364
364
365
367
367
Contents
13
14
xv
368
368
369
369
369
371
371
371
371
373
373
373
376
376
376
376
377
378
378
379
379
380
380
380
382
383
383
384
384
385
385
Introducing C
386
387
387
387
388
388
390
391
392
392
393
xvi
15
Contents
14.2.9 The C preprocessor and its directives
14.2.10 Use of libraries, and the Standard Library
14.3 Compiling the C program
14.4 The MPLAB C18 compiler
14.4.1 Specification of radix
14.4.2 Arithmetic operations
14.5 A C18 tutorial
14.5.1 The Linker and Linker Scripts
14.5.2 Linking header and library files
14.5.3 Building the project
14.5.4 Project files
14.6 Simulating a C program
14.7 A second C example the Fibonacci program
14.7.1 Program preliminaries more on declaring variables
14.7.2 The dowhile construct
14.7.3 Labels and the goto keyword
14.7.4 Simulating the Fibonacci program
14.8 The MPLAB C18 libraries
14.8.1 Hardware peripheral functions
14.8.2 The software peripheral library
14.8.3 The general software library
14.8.4 The maths library
14.9 Further reading
Summary
References
394
394
394
395
396
396
396
396
397
397
398
400
401
402
403
403
403
403
404
404
405
406
406
407
407
409
409
409
411
411
412
412
413
413
414
414
415
415
415
416
417
420
Contents
16
17
xvii
421
421
422
423
423
423
427
428
429
429
431
431
432
432
433
433
434
435
437
437
439
440
440
442
442
443
444
444
445
446
447
447
448
448
449
449
450
451
452
453
453
xviii
18
Contents
17.6.2 Scope
17.6.3 Duration
17.6.4 Linkage
17.6.5 Working with 18 Series memory
17.6.6 Storage class examples
17.7 Start-up code: c018i.c
17.7.1 The C18 start-up files
17.7.2 The c018i.c structure
17.7.3 Simulating c018i.c
17.8 Structures, unions and bit-fields
17.9 Processor-specific header files
17.9.1 SFR definitions
17.9.2 Assembler utilities in the header file
17.10 Taking things further the MPLAB Linker and the .map file
17.10.1 What the Linker does
17.10.2 The Linker Script
17.10.3 The .map file
Summary
References
454
454
455
455
455
456
456
457
457
459
460
460
461
462
462
462
464
465
465
466
466
467
468
469
469
469
469
470
471
471
472
473
473
473
474
475
476
477
477
477
478
Contents
19
xix
478
479
479
479
480
480
480
481
482
482
482
483
483
484
485
485
487
488
488
489
489
490
491
492
494
494
496
496
497
499
499
500
505
505
505
506
507
509
509
510
510
xx
Contents
511
20
513
513
514
515
515
516
516
517
517
518
518
520
520
521
522
523
523
524
524
Appendix 1
Appendix 2
Appendix 3
Appendix 4
Appendix 5
Appendix 6
Index
527
528
533
537
541
544
549
Contents
List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
VI
List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
XI
The Fundamentals
1.
Digital Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.
Logic Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
3.
41
Part II
The Software
4.
5.
6.
7.
8.
9.
Part III
VI
Contents
B.
C.
D.
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Indice
Cap tulo 1
Arquitectura de un PIC
Introducci n ......................................................5
Caracter sticas del PIC .....................................5
Caracter sticas Perif ricas................................5
Caracter sticas Especiales del
Microcontrolador ...............................................6
Patas del PIC....................................................6
Resumen del Dispositivo ..................................9
Ficha Did ctica ...............................................10
Lo que Debe Saber
Sobre los PICs................................................13
a) El Procesador es segmentado
tipo "pipe line" .................................................13
b) El Procesador es de tipo RISC ..................14
c) Poseen una arquitectura
tipo Harvard ....................................................14
d) Son de arquitectura ortogonal basada
en banco de registros .....................................15
Palabras de Configuraci n
e Identificaci n................................................18
Organizaci n de la Memoria...........................19
Organizaci n de la Memoria
de Programas .................................................19
Organizaci n de la Memoria
de Datos..........................................................19
Los Registros del PIC.....................................20
Archivo de Registro de
Uso General....................................................20
Registros de Funciones
Especiales (SFR)............................................21
Registro de Status ..........................................23
Registro OPTION_REG..................................25
Microcontroladores PIC
Indice
Cap tulo 2
Manejo de las Instrucciones
de un PIC
Introducci n ....................................................53
Instrucci n: SUMA ..........................................54
C digo de la instrucci n ADDWf ....................55
El Set de Instrucciones
del 16X84........................................................56
ADDLW...................................................59
ADDWF ..................................................60
ANDLW...................................................61
ANDWF ..................................................62
BCF ........................................................63
BSF ........................................................64
BTFSC ...................................................65
BTFSS....................................................67
CALL ......................................................68
CLRF......................................................69
CLRW.....................................................70
CLRWDT ................................................71
COMF.....................................................72
DECF .....................................................73
DECFSZ.................................................74
GOTO.....................................................75
INCF.......................................................76
INCFSZ ..................................................77
IORLW....................................................78
IORWF ...................................................79
MOVLW ..................................................80
MOVF.....................................................81
MOVWF .................................................82
NOP .......................................................83
RETFIE ..................................................83
RETLW ...................................................84
RETURN ................................................85
RLF ........................................................85
RRF........................................................86
SLEEP....................................................87
SUBLW...................................................88
SUBWF ..................................................90
SWAPF...................................................92
XORLW ..................................................93
XORWF ..................................................94
Palabras de Configuraci n
e Identificaci n...............................................96
Cap tulo 3
Aprendiendo a Programar
Circuito de un Entrenador...............................97
Encendido Intermitente
de un Led......................................................100
Secuenciador de 4 Canales .........................108
Generador de Se ales de Audio...................112
Los Formatos de los Archivos.......................115
Secuenciador de 8 Canales..........................115
Compilaci n de un Programa .......................118
Carga del Programa en un PIC ....................121
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
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Whats on the CD-ROM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
Chapter 1: What Are Intelligent Sensors, and
Why Should I Care about Them? . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Conventional Sensors Arent Perfect . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 First Things FirstDigitizing the Sensor Signal. . . . . . . . . . . . . . . . . . 7
1.3 Next StepAdd Some Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 Finish Up with Quick and Reliable Communications . . . . . . . . . . . . 8
1.5 Put It All Together, and Youve Got an Intelligent Sensor . . . . . . . . . . 9
1.6 Why Dont 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 DSCs Data Processing Architecture . . . . . . . . . . . . . . . . 55
3.2 Interrupt Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
vii
viii Contents
Contents
ix
Contents
Introduction
History
Why use C?
PC based versus PICmicroMCU Based Program Development
Product Development
Terminology
Trying and Testing Code
C Coding Standards
Basics
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
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 PICmicroMCU 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
Table of Contents
1. Introduction
2. Microcontroller Overview
Table of Contents
23
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
55
iii
Table of Contents
6. C Program Structure
65
71
Table of Contents
83
Table of Contents
9. Control Structures
99
10. Functions
111
Table of Contents
121
11.1 Pointers..................................................................................................121
11.1.1 Declaring a Pointer ........................................................................................................121
11.1.2 Pointer Operators ..........................................................................................................122
11.1.3 Pointer Pitfalls ................................................................................................................123
Table of Contents
139
151
Table of Contents
14. Libraries
161
169
ix
Table of Contents
185
187
18. Glossary
189
19. Bibliography
197
20. Index
198
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
Preight checklist ................................................................................................. 4
The ight............................................................................................................... 4
Compiling and linking ................................................................................................................ 6
Building the rst project ............................................................................................................. 7
PORT initialization ..................................................................................................................... 9
Retesting PORTA...................................................................................................................... 10
Testing PORTB ......................................................................................................................... 10
Contents
Notes for PIC microcontroller experts............................................................... 27
Notes for C experts ............................................................................................ 27
Tips and tricks ..................................................................................................... 27
Exercises .............................................................................................................. 28
Books ................................................................................................................. 28
Links ................................................................................................................. 28
CHAPTER 3: MORE PATTERN WORK, MORE LOOPS ............................................... 29
Flight plan ........................................................................................................... 29
Preight checklist ............................................................................................... 30
The ight............................................................................................................. 30
Do Loops .................................................................................................................................. 30
Variable declarations ................................................................................................................ 31
for loops ................................................................................................................................. 31
More loop examples ................................................................................................................. 32
Arrays ....................................................................................................................................... 33
A new demo .............................................................................................................................. 34
Testing with the Logic Analyzer............................................................................................... 36
Using the Explorer16 demonstration board .............................................................................. 37
Contents
Math libraries............................................................................................................................ 51
Complex data types .................................................................................................................. 51
Exercises .............................................................................................................. 52
Books ................................................................................................................. 52
Links ................................................................................................................. 52
CHAPTER 5: INTERRUPTS ......................................................................................... 53
Flight plan ........................................................................................................... 53
Preight checklist ............................................................................................... 53
The ight............................................................................................................. 54
Nesting of interrupts ................................................................................................................. 57
Traps ......................................................................................................................................... 57
A template and an example for Timer1 interrupt ..................................................................... 58
A real example with Timer1 ..................................................................................................... 59
Testing the Timer1 interrupt ..................................................................................................... 61
The secondary oscillator ........................................................................................................... 63
The real-time clock calendar (RTCC) ...................................................................................... 64
Managing multiple interrupts ................................................................................................... 64
Contents
Books ................................................................................................................. 83
Links ................................................................................................................. 83
Contents
Books ............................................................................................................... 125
Links ............................................................................................................... 125
CHAPTER 9: GLASS BLISS ....................................................................................... 127
Flight plan ......................................................................................................... 128
Pre-ight checklist............................................................................................ 128
The ight........................................................................................................... 128
HD44780 controller compatibility ........................................................................................ 129
The Parallel Master Port ......................................................................................................... 131
Conguring the PMP for LCD module control ...................................................................... 132
A small library of functions to access an LCD display .......................................................... 133
Advanced LCD control ........................................................................................................... 136
Contents
Testing the PS/2 receive routines ........................................................................................... 172
The simulation ........................................................................................................................ 174
The Simulator Prole ............................................................................................................. 175
Another method Change Notication .................................................................................. 176
Evaluating cost ....................................................................................................................... 181
A third method I/O polling .................................................................................................. 181
Testing the I/O polling method ............................................................................................... 186
Cost and efciency of the solution ......................................................................................... 188
Completing the interface: adding a FIFO buffer .................................................................... 190
Completing the interface: performing key codes decoding .................................................... 194
Contents
CHAPTER 13: MASS STORAGE ............................................................................... 263
Flight plan ......................................................................................................... 263
The ight........................................................................................................... 264
The SD/MMC card physical interface .................................................................................... 264
Interfacing to the Explorer16 board........................................................................................ 265
Starting a new project ............................................................................................................. 266
Selecting the SPI mode of operation ...................................................................................... 267
Sending commands in SPI mode ........................................................................................... 267
Completing the SD/MMC card initialization ......................................................................... 270
Reading data from an SD/MMC card ..................................................................................... 271
Writing data to an SD/MMC card .......................................................................................... 274
Using the SD/MMC interface module .................................................................................... 276
xiii
Contents
Using the PIC24 OC modules in PWM mode ........................................................................ 332
Testing the PWM as a D/A converter ..................................................................................... 334
Producing analog waveforms ................................................................................................. 335
Reproducing voice messages ................................................................................................. 338
A media player ....................................................................................................................... 339
The WAVE le format ............................................................................................................ 339
The play() function ............................................................................................................ 341
The low level audio routines .................................................................................................. 347
Testing the WAVE le player.................................................................................................. 350
Optimizing the le I/O............................................................................................................ 353
LED Proling ......................................................................................................................... 353
Looking under the hood for more ........................................................................................... 356
xiv
Contents
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
#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 )NTRODUCTORY 3TEPPER -OTORS
3TEPPER -OTOR "ASICS
0ROGRAMS
2EFERENCES
#(!04%2 )NTERRUPTS AND 4IMERS IN -"ASIC
)NTERRUPTS AND 4IMERS/VERVIEW
)NTERRUPTS
4IMERS
#APTURE AND #OMPARE
2EFERENCES
#ONTENTS
#(!04%2 $IGITAL 4EMPERATURE 3ENSORS AND 2EAL
4IME #LOCKS
$3" 4EMPERATURE 3ENSOR
$3 2EAL
4IME #LOCK
#OMBINATION $ATE 4IME AND 4EMPERATURE
2EFERENCES
#(!04%2 !SSEMBLER
4HE "ASICS
/P#ODES
2EFERENCES
#(!04%2 )NTERRUPT (ANDLERS AND 4IMERS IN !SSEMBLER
)32!3- n -"ASICS 'ATEWAY TO !SSEMBLER )NTERRUPT 3ERVICE 2OUTINES
0ROGRAM %XAMPLES
2EFERENCES
#(!04%2 $4-&