The Quintessential PIC Microcontroller (S. Katzen, 2000).Pdf
Total Page:16
File Type:pdf, Size:1020Kb
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 R S 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 × 8RAM.................................... 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. .................