Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 15.06.2025

Просмотров: 5253

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Sid Katzen

The Quintessential PIC

Microcontroller

SPIN Springer’s internal project number, if known

Engineering – Monograph (English)

November 8, 2000

Springer-Verlag

Berlin Heidelberg NewYork

London Paris Tokyo

Hong Kong Barcelona

Budapest

Contents

List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VI

List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XI

List of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XIV

Part I The Fundamentals

1.

Digital Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.

Logic Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

3.Stored Program Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

Part II The Software

4.

The PIC16F84 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

5.

The Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

105

6.

Subroutines and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

137

7.

Interrupt Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

171

8.

Assembly language .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

197

9.High-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Part III The Outside World

10. The Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

11. One Byte at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271


VI Contents

12. One Bit at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 13. Time is of the Essence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 14. Take the Rough with the Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

15.To Have and to Hold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

16.A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455

Appendices

A. 14-bit Core Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 B. Special Purpose Register Structure for the PIC16C74B . . . . . . . . . 477 C. C Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 D. Acronyms and Abbreviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

List of Figures

1.1The NOT operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.2The AND function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.3The inclusive-OR operation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.4The XOR operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.5 Detecting sign overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1 The 74LS00 quad 2-I/P NAND package. . . . . . . . . . . . . . . . . . . . . . . 18

2.2Output structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2.3 Open-collector bu ers driving a party line. . . . . . . . . . . . . . . . . . . 20 2.4 Sharing a bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.5The 74LS138 and ’139 MSI natural decoders. . . . . . . . . . . . . . . . . 21

2.6 The 74LS688 octal equality detector. . . . . . . . . . . . . . . . . . . . . . . . . 23

2.7Addition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.8Implementing a programmable adder/subtractor. . . . . . . . . . . . 25

2.9 The 74LS382 ALU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.10 A ROM-implemented 1-bit adder. . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.11 The 2764 Erasable PROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.12 Floating-gate MOSFET link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.13 The R S latch.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.14 Using a R S latch to debounce a switch. . . . . . . . . . . . . . . . . . . . . . . 30

2.15The D latch and flip flop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

2.16The 74LS74 dual D flip flop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.17 The 74LS377 octal D flip flop array. . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.18 The 74LS373 octal D latch array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

2.19An 8-bit ALU-accumulator processor. . . . . . . . . . . . . . . . . . . . . . . . 35

2.20The SISO shift register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

2.21 The T flip flop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.22 A modulo-16 ripple counter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.23 Generating timing waveforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.24 The 6264 8196 × 8 RAM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3.1 An elementary von Neumann computer. . . . . . . . . . . . . . . . . . . . . . 42 3.2 An elementary Harvard architecture computer. . . . . . . . . . . . . . . 44 3.3 Executing the 1st instruction whilst fetching down the 2nd. . . 45 3.4 Parallel fetch and execute streams. . . . . . . . . . . . . . . . . . . . . . . . . . . 50


VIII List of Figures

3.5 Programmer’s model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

3.6The indirect mechanism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

3.7 Circular shifts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.8The process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

3.9 Visualization of the task process. . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

3.10Division by repetitive subtracting. . . . . . . . . . . . . . . . . . . . . . . . . . . 68

3.11Double-precision shifting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.12 A 7-bit pseudo-random number generator. . . . . . . . . . . . . . . . . . . 70

4.1 An example of a system based on a microcontroller. . . . . . . . . . 81 4.2 Architecture of the PIC16F84 microcontroller . . . . . . . . . . . . . . . 85 4.3 Showing how all of the PC are altered when writing to PCL.. . . 86 4.4 Internal clock sequencing waveforms. . . . . . . . . . . . . . . . . . . . . . . . 87 4.5 The PIC16F84 Status register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

4.6Data store memory map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

5.1 General 14-bit core Status register. . . . . . . . . . . . . . . . . . . . . . . . . . . 109

5.2The indirect mechanism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

5.3 The ith section of the compare-update sequence. . . . . . . . . . . . . 112 5.4 Generating a 13-bit Program-store address. . . . . . . . . . . . . . . . . . . 114

6.1 Modular hardware implementing a PC. . . . . . . . . . . . . . . . . . . . . . . 138

6.2Subroutine calling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

6.3Using the hardware stack hold return addresses. . . . . . . . . . . . . 141

6.4 Nested subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 6.5 System view of K × 100 ms delay subroutine. . . . . . . . . . . . . . . . . 145 6.6 The 7-segment display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 6.7 System diagram for the byte multiplication subroutine. . . . . . . 150

6.8The stack frame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

6.9 Finding the square root of an integer. . . . . . . . . . . . . . . . . . . . . . . . 162

7.1Detecting and measuring an external event. . . . . . . . . . . . . . . . . . 172

7.2Responding to an interrupt request.. . . . . . . . . . . . . . . . . . . . . . . . . 175

7.3 The flag:mask pair. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 7.4 The PIC 16F84’s interrupt logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 7.5 Oven safety hardware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7.6 Echo sounding hardware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

8.1 Conversion from assembly-level source to machine code. . . . . 198 8.2 Absolute assembly-level code translation. . . . . . . . . . . . . . . . . . . . 202 8.3 Relocatable assembly-level code translation. . . . . . . . . . . . . . . . . . 211

8.4Linking three source files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

8.5Code building and testing tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

8.6 MPLAB window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

8.7MPLAB screen shot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222


List of Figures IX

9.1 Conversion from high-level source code to machine code. . . . . 233 9.2 Onion skin view of the steps leading to executable code. . . . . . 234 9.3 Simulating our example program in MPLAB. . . . . . . . . . . . . . . . . . 242 9.4 The active-low die patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

10.1 Pinout for a variety of PIC family members. . . . . . . . . . . . . . . . . . 254 10.2 Typical supply current versus clocking frequency. . . . . . . . . . . . 256 10.3 Equivalent output circuit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 10.4 Typical oscillator configurations.. . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 10.5 Configuration word for the PIC16F83/4. . . . . . . . . . . . . . . . . . . . . . 261

10.6Manually resetting the PIC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

10.7The sequence of events leading to startup on power-up. . . . . . 264

10.8Brown-out reset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

10.9 An alternative brown-out circuit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

11.1The mid-range PIC 16CXX series Parallel Ports A and B. . . . . . . 272

11.2A simplified typical I/O port line. . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

11.3Reading and writing to a port bit set to input or output. . . . . . 275

11.4 Sinking and sourcing current. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 11.5 Port A I/O pin driver structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 11.6 Interfacing switches to a port line. . . . . . . . . . . . . . . . . . . . . . . . . . . 280 11.7 Port B’s weak pull-up option. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 11.8 Interfacing to a keypad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 11.9 The Port B change feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

11.10A multi-zone intruder alarm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

11.11Source current against voltage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

11.12 The stepper motor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 11.13 Using port expansion to drive three 7-segment displays. . . . . . 298 11.14 Scanning a 3-digit 7-segment array. . . . . . . . . . . . . . . . . . . . . . . . . . 299 11.15 Low-level output voltage against sink current. . . . . . . . . . . . . . . . 304

12.1 The smart card. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 12.2 Serial interface to a 3-digit 7-segment display. . . . . . . . . . . . . . . . 307

12.3Logic functional diagram of the 74HCT595 octal shift register. 309

12.4Serially interfacing to a DAC0800 digital to analog converter. 310

12.5Serially interfacing to the multi-zone intruder alarm. . . . . . . . . 311

12.6The MAX549A SPI dual 8-bit DAC. . . . . . . . . . . . . . . . . . . . . . . . . . . 314

12.7SPI waveforms for the MAX549A. . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

12.8Multiple MAX549As on the one SPI circuit. . . . . . . . . . . . . . . . . . . 316

12.9 The basic Serial Synchronous Port. . . . . . . . . . . . . . . . . . . . . . . . . . . 317

12.10The SSP CONtrol and STATus registers. . . . . . . . . . . . . . . . . . . . . . 318

12.11SSP SPI-mode master waveforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

12.12 A multidrop SPI communications network. . . . . . . . . . . . . . . . . . . 322 12.13 Data transfer on the I2C bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

12.14 Sharing the SCL and SDA bus lines. . . . . . . . . . . . . . . . . . . . . . . . . . . 326


XList of Figures

12.15 A I2C packet transmission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 12.16 The MAXIM MAX518 I2C dual digital to analog converter. . . . . 328 12.17 Minimum timing relationships for the Fast I2C mode. . . . . . . . . 329 12.18 Transmitting the string "PIC" in the asynchronous mode . . . . . 336 12.19 The PIC USART configured for asynchronous communication. 342 12.20 Some signalling configurations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 12.21 Communicating with a PC via an RS-232 link. . . . . . . . . . . . . . . . . 349

12.22The 24XXX series of I2C serial EEPROMs. . . . . . . . . . . . . . . . . . . . . 352

12.23EEPROM Read and Write waveforms. . . . . . . . . . . . . . . . . . . . . . . . . 355

12.24Interfacing the DS1820 1-Wire digital thermometer. . . . . . . . . . 356

12.25A LCD display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

13.1 The integral PIC Watchdog timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 13.2 The Option register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 13.3 Simplified equivalent circuit for Timer 0. . . . . . . . . . . . . . . . . . . . . 365 13.4 Counting cans of beans on a conveyer belt. . . . . . . . . . . . . . . . . . . 366 13.5 Functional equivalent circuit for Timer 1 . . . . . . . . . . . . . . . . . . . . 372 13.6 The CCP1 module set to Compare mode. . . . . . . . . . . . . . . . . . . . . 375 13.7 Capturing the time of an event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 13.8 A simplified equivalent circuit for Timer 2. . . . . . . . . . . . . . . . . . . 379 13.9 Pulse width modulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 13.10 Timer 2 and the PWM CCP mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 13.11 An event manifesting itself as a pulse duration. . . . . . . . . . . . . . 387

14.1 Analog world – digital processing. . . . . . . . . . . . . . . . . . . . . . . . . . . 391 14.2 The quantizing process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 14.3 The analog–digital process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 14.4 Illustrating aliasing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 14.5 Initializing the 8-4-2-1 capacitor network. . . . . . . . . . . . . . . . . . . . 398

14.6Simplified view of the A/D converter. . . . . . . . . . . . . . . . . . . . . . . . 400

14.7The successive approximation process. . . . . . . . . . . . . . . . . . . . . . 402

14.8The 8-bit 8-channel analog to digital conversion module. . . . . 404

14.9Configuring the analog inputs for Port A and Port E.. . . . . . . . . . 405

14.10Interrupt control for the ADC module. . . . . . . . . . . . . . . . . . . . . . . 408

14.11 R-2R digital-to-analog conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 14.12 The Maxim MAX506 quad 8-bit D/A converter. . . . . . . . . . . . . . . 418 14.13 Generating a continuous sawtooth using a MAX506 DAC. . . . . 419

14.14Bu ered data acquisition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420

14.15A level-shifting resistor network.. . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

14.16 ECG detection strategy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

14.17A controllable external voltage circuit. . . . . . . . . . . . . . . . . . . . . . . 429

14.18Pinning for the PIC16C71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

15.1 The PIC16F8X Data EEPROM module. . . . . . . . . . . . . . . . . . . . . . . . . 433 15.2 The PIC16F8X EECON1 register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434