Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

The Quintessential Pic Microcontroller
Nội dung xem thử
Mô tả chi tiết
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.1 The NOT operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2 The AND function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3 The inclusive-OR operation.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 The XOR operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 Detecting sign overflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1 The 74LS00 quad 2-I/P NAND package. . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Output structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 Open-collector buffers driving a party line. . . . . . . . . . . . . . . . . . . 20
2.4 Sharing a bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5 The 74LS138 and ’139 MSI natural decoders. . . . . . . . . . . . . . . . . 21
2.6 The 74LS688 octal equality detector. . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7 Addition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.8 Implementing 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 RS latch to debounce a switch. . . . . . . . . . . . . . . . . . . . . . . 30
2.15 The D latch and flip flop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.16 The 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.19 An 8-bit ALU-accumulator processor. . . . . . . . . . . . . . . . . . . . . . . . 35
2.20 The 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.6 The indirect mechanism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.7 Circular shifts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.8 The process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.9 Visualization of the taskprocess. . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.10 Division by repetitive subtracting. . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.11 Double-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 clocksequencing waveforms. . . . . . . . . . . . . . . . . . . . . . . . 87
4.5 The PIC16F84 Status register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.6 Data store memory map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.1 General 14-bit core Status register. . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.2 The 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.2 Subroutine calling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
6.3 Using the hardware stackhold 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.8 The stackframe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
6.9 Finding the square root of an integer. . . . . . . . . . . . . . . . . . . . . . . . 162
7.1 Detecting and measuring an external event. . . . . . . . . . . . . . . . . . 172
7.2 Responding to an interrupt request.. . . . . . . . . . . . . . . . . . . . . . . . . 175
7.3 The flag:maskpair. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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.4 Linking three source files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
8.5 Code building and testing tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
8.6 MPLAB window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
8.7 MPLAB 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.6 Manually resetting the PIC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
10.7 The sequence of events leading to startup on power-up. . . . . . 264
10.8 Brown-out reset. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
10.9 An alternative brown-out circuit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
11.1 The mid-range PIC 16CXX series Parallel Ports A and B. . . . . . . 272
11.2 A simplified typical I/O port line. . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
11.3 Reading 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 weakpull-up option. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
11.8 Interfacing to a keypad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
11.9 The Port B change feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
11.10 A multi-zone intruder alarm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
11.11 Source 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 sinkcurrent. . . . . . . . . . . . . . . . 304
12.1 The smart card. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
12.2 Serial interface to a 3-digit 7-segment display. . . . . . . . . . . . . . . . 307
12.3 Logic functional diagram of the 74HCT595 octal shift register. 309
12.4 Serially interfacing to a DAC0800 digital to analog converter. 310
12.5 Serially interfacing to the multi-zone intruder alarm. . . . . . . . . 311
12.6 The MAX549A SPI dual 8-bit DAC. . . . . . . . . . . . . . . . . . . . . . . . . . . 314
12.7 SPI waveforms for the MAX549A. . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
12.8 Multiple MAX549As on the one SPI circuit. . . . . . . . . . . . . . . . . . . 316
12.9 The basic Serial Synchronous Port. . . . . . . . . . . . . . . . . . . . . . . . . . . 317
12.10 The SSP CONtrol and STATus registers. . . . . . . . . . . . . . . . . . . . . . 318
12.11 SSP SPI-mode master waveforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
12.12 A multidrop SPI communications network. . . . . . . . . . . . . . . . . . . 322
12.13 Data transfer on the I
2C bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
12.14 Sharing the SCL and SDA bus lines. . . . . . . . . . . . . . . . . . . . . . . . . . . 326
X List of Figures
12.15 A I
2C packet transmission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
12.16 The MAXIM MAX518 I
2C dual digital to analog converter. . . . . 328
12.17 Minimum timing relationships for the Fast I
2C 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.22 The 24XXX series of I2C serial EEPROMs. . . . . . . . . . . . . . . . . . . . . 352
12.23 EEPROM Read and Write waveforms. . . . . . . . . . . . . . . . . . . . . . . . . 355
12.24 Interfacing the DS1820 1-Wire digital thermometer. . . . . . . . . . 356
12.25 A 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.6 Simplified view of the A/D converter. . . . . . . . . . . . . . . . . . . . . . . . 400
14.7 The successive approximation process. . . . . . . . . . . . . . . . . . . . . . 402
14.8 The 8-bit 8-channel analog to digital conversion module. . . . . 404
14.9 Configuring the analog inputs for Port A and Port E.. . . . . . . . . . 405
14.10 Interrupt 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.14 Buffered data acquisition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
14.15 A level-shifting resistor network. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
14.16 ECG detection strategy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
14.17 A controllable external voltage circuit. . . . . . . . . . . . . . . . . . . . . . . 429
14.18 Pinning for the PIC16C71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
15.1 The PIC16F8X Data EEPROM module. . . . . . . . . . . . . . . . . . . . . . . . . 433
15.2 The PIC16F8X EECON1 register. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
List of Figures XI
15.3 The first 32 bytes of EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
15.4 The PIC16F87X flash and Data EEPROM storage system. . . . . . . 440
15.5 The PIC16F87X EEPROM Control register 1. . . . . . . . . . . . . . . . . . . 441
15.6 View of the flash Program module. . . . . . . . . . . . . . . . . . . . . . . . . . . 445
15.7 Configuration word for the PIC16F87X devices. . . . . . . . . . . . . . . 445
15.8 Watchdog timer period versus temperature. . . . . . . . . . . . . . . . . . 448
16.1 The annunciator hardware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
16.2 The modular software structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
16.3 The Main process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
16.4 Programming the PIC from MPLAB. . . . . . . . . . . . . . . . . . . . . . . . . . . 472
16.5 The Microchip PICSTART Plus programmer. . . . . . . . . . . . . . . . . . 473
List of Tables
1.1 7-bit ASCII characters. ...................................... 5
1.2 Some common bit groupings. ............................... 6
1.3 Different ways of representing the quantities decimal 0…20. . 7
3.1 Our BASIC computer’s instruction set. . . . . . . . . . . . . . . . . . . . . . . 53
5.1 Move instructions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
5.2 Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.3 Logic instructions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.4 Program Counter instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.1 Subroutine and interrupt handling instructions. . . . . . . . . . . . . . 139
6.2 The 7-segment lookup table showing byte[N] being extracted. 149
8.1 The listing file root.lst. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
8.2 The absolute 8-bit Intel format object-code file root.hex. . . . 206
8.3 The error file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
8.4 Part of Microchip’s file p16f84.inc. . . . . . . . . . . . . . . . . . . . . . . . . 209
8.5 The pic16f84.lkr linker command file. . . . . . . . . . . . . . . . . . . . . 212
8.6 The output linker map file rms.asm. . . . . . . . . . . . . . . . . . . . . . . . . 218
8.7 The resulting absolute object file rms.hex. . . . . . . . . . . . . . . . . . . 219
9.1 Resulting assembly-level CCS compiler output after linking.. . 240
10.1 PIC16F83/4 Special-Purpose Register file reset summary. . . . . 263
10.2 Power-up reset and sleep timeouts. . . . . . . . . . . . . . . . . . . . . . . . . 265
10.3 Reset conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
11.1 Summary of mid-range PIC parallel I/O provision. . . . . . . . . . . . 272
11.2 Energization pattern for the eight field directions. . . . . . . . . . . . 294
12.1 The SSP Mode bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
14.1 Quantization parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
14.2 ADC clocking frequency versus device crystal frequency. . . . . 401
14.3 Configuring the ADC port pins in the PIC16C73/74 devices. . . 405
List of Programs
3.1 Clearing a blockof files the linear way. . . . . . . . . . . . . . . . . . . . . . . 56
3.2 Clearing a blockof files using a repeating loop. . . . . . . . . . . . . . . 57
3.3 Simple single-precision addition of two byte variables. . . . . . . . 64
3.4 A more accurate single-precision addition. . . . . . . . . . . . . . . . . . . 64
3.5 The double-precision add program. . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.6 Dividing by ten. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.7 Multiplying by nine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.8 A 7-bit pseudo-random number generator. . . . . . . . . . . . . . . . . . . 71
4.1 Incrementing a packed BCD byte. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
4.2 Adding two packed BCD numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.1 Finding the maximum temperature the linear way. . . . . . . . . . . . 111
5.2 Finding the maximum temperature using a loop structure. . . . 113
5.3 Division by repetitive subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . 118
5.4 Shifting to find the highest set bit. . . . . . . . . . . . . . . . . . . . . . . . . . . 124
5.5 Triple-precision shifting to find the number of set bits. . . . . . . 125
5.6 Multiplying by three. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.7 Double-precision decrement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
5.8 Bi-quinary error detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
5.9 Binary to 2-digit BCD conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.10 Average daily temperature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
5.11 multiplication by ten. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.1 A 100 ms delay subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.2 A K × 100 ms delay subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.3 An alternative K × 100 ms delay subroutine. . . . . . . . . . . . . . . . . 147
6.4 The software 7-segment decoder. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
6.5 The byte multiplication subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . 152
6.6 Implementing a byte multiply using a stackmodel. . . . . . . . . . . 157
6.7 Dividing by three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.8 Coding a 208 µs delay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.9 A 1-second delay program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.10 Binary to 3-digit BCD conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.11 Coding the square root subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . 163
6.12 Using a software stackto pass parameters. . . . . . . . . . . . . . . . . . . 166
6.13 The software 7-segment decoder revisited. . . . . . . . . . . . . . . . . . . 166
XVI List of Programs
7.1 Background program for the pea canning packer. . . . . . . . . . . . . 181
7.2 Event counting foreground software. . . . . . . . . . . . . . . . . . . . . . . . . 183
7.3 Oven safety.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
7.4 Saving and restoring the context for the PIC16C74 processor. 191
7.5 Coding the real-time clockISR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
7.6 Incrementing a packed-BCD byte with maximum value of 99.. 194
8.1 Absolute assembly-level code for our square-root module. . . . 200
8.2 The main relocatable source file main.asm. . . . . . . . . . . . . . . . . . . 214
8.3 The relocatable source file sqr.asm. . . . . . . . . . . . . . . . . . . . . . . . . 215
8.4 The relocatable source file root2.asm. . . . . . . . . . . . . . . . . . . . . . . 216
9.1 A simple function coded in C.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
9.2 Coding the square root function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
9.3 Linearizing a K-type thermocouple. . . . . . . . . . . . . . . . . . . . . . . . . . 246
9.4 Generating the root-mean square value of two variables. . . . . . 247
11.1 Scanning the keypad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
11.2 Noise filtered keypad scanning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
11.3 Interacting with the intruder hardware. . . . . . . . . . . . . . . . . . . . . . 288
11.4 A digital comparator with hysteresis. . . . . . . . . . . . . . . . . . . . . . . . 292
11.5 Driving a stepper motor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
11.6 Coding the keypad device driver in C. . . . . . . . . . . . . . . . . . . . . . . . 297
11.7 Displaying the decimal equivalent of a binary byte. . . . . . . . . . . 301
11.8 Displaying a 3-digit decimal number on a scanning readout. . 302
12.1 Displaying the decimal equivalent of a binary byte. . . . . . . . . . . 308
12.2 Input serial byte subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
12.3 Interacting with the MAX549A dual-channel SPI DAC. . . . . . . . . 315
12.4 Using the SSP for SPI data input and output. . . . . . . . . . . . . . . . . . 320
12.5 Interfacing to the MAX549A in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
12.6 A crystal frequency-independent short delay macro. . . . . . . . . . 331
12.7 Low-level I
2C subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
12.8 Interacting with the MAX518 dual-channel I
2C DAC. . . . . . . . . . 334
12.9 Interfacing to the MAX518 in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
12.10 A baud-rate delay macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
12.11 Asynchronous formatted input and output subroutines. . . . . . 340
12.12 The USART-based I/O subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . 345
12.13 Updating Program 11.4’s trip value. . . . . . . . . . . . . . . . . . . . . . . . . . 350
12.14 Reading in a byte using the I
2C protocol. . . . . . . . . . . . . . . . . . . . . 351
12.15 Incrementing the non-volatile odometer count. . . . . . . . . . . . . . . 354
12.16 Reading and writing on a 1-Wire system. . . . . . . . . . . . . . . . . . . . . 358
13.1 The bean counter Interrupt Service Routine. . . . . . . . . . . . . . . . . . 368
13.2 Measuring the ECG waveform period to a resolution of 1 ms. . 370
13.3 Generating a 15 minute data logger timebase. . . . . . . . . . . . . . . . 374
13.4 Capturing the instant of time an ECG R-point occurs. . . . . . . . . 378
13.5 Pulse-Width Modulation using Timer 0. . . . . . . . . . . . . . . . . . . . . . . 384
13.6 Tachometer software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
List of Programs XVII
13.7 Measuring the duration of a pulse. . . . . . . . . . . . . . . . . . . . . . . . . . . 388
14.1 Taking a reading from channel n. . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
14.2 Interrupt-driven subroutine to read channel n. . . . . . . . . . . . . . . 410
14.3 The ISR for our interrupt-driven ADC software. . . . . . . . . . . . . . . 411
14.4 Digitizing Channel 1 of a PIC16C71 device. . . . . . . . . . . . . . . . . . . 412
14.5 A digital/analog comparator with hysteresis. . . . . . . . . . . . . . . . . 414
14.6 Buffered interrupt-driven data acquisition. . . . . . . . . . . . . . . . . . . 421
14.7 Sleep conversion in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
14.8 ECG peakpicking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
14.9 An implementation of the ECG peakpicker in C. . . . . . . . . . . . . . 427
15.1 Retrieving a byte from the EEPROM Data module. . . . . . . . . . . . . 434
15.2 Putting a byte into the EEPROM Data module.. . . . . . . . . . . . . . . . 436
15.3 Incrementing the non-volatile odometer count in Data EEPROM. 437
15.4 Reading a word from the flash Program store. . . . . . . . . . . . . . . . 442
15.5 Writing to flash Program memory. . . . . . . . . . . . . . . . . . . . . . . . . . . 443
15.6 Squaring an integer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
15.7 C-based coding for the odometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
15.8 The Sauna Power-up reset sequence and ISR. . . . . . . . . . . . . . . . . 450
15.9 Reading a new period count. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
15.10 Updating the Sauna EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
16.1 The timebase software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
16.2 The data display function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
16.3 The initialization code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
16.4 The Diagnostic process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
16.5 The Set-time process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
16.6 The Main process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471