Embedded Systems Programming with the Microchip PIC16F877 Microcontroller
Total Page:16
File Type:pdf, Size:1020Kb
EMBEDDED SYSTEMS PROGRAMMING WITH THE PIC16F877 Second Edition By Timothy D. Green Copyright 2008 by Timothy D. Green All Rights Reserved. Table of Contents Preface …………………………………………………………………. 5 List of Figures …………………………………………………………. 6 Abbreviations and Acronyms …………………………………………. 7 Trademarks ……………………………………………………………. 10 Chapter 1 Introduction to ESP and the PIC …………………………. 11 Chapter 2 Microcontrollers and the PIC16F877 ……………………. 15 Section 2.0 Chapter Summary ……………………………….. 15 Section 2.1 Memory and Memory Organization ……………. 15 Section 2.2 The PIC16F877 …………………………………. 16 Section 2.3 Programming the PIC …………………………… 17 Chapter 3 Simple PIC Hardware & Software (“Hello World”) …….. 20 Section 3.0 Chapter Summary ……………………………….. 20 Section 3.1 A Simple Example System ……………………… 20 Section 3.2 Summary of Instructions and Concepts …………. 25 Chapter 4 The PIC Instruction Set (Part I) ………………………….. 27 Section 4.0 Chapter Summary ………………………………. 27 Section 4.1 The PIC16F877 Instruction Set ………………… 27 Section 4.2 Summary of Instructions and Concepts …………. 33 Chapter 5 The PIC Instruction Set (Part II) …………………………. 34 Section 5.0 Chapter Summary ……………………………….. 34 Section 5.1 Introduction ……………………………………… 34 Section 5.2 Keypad and Display Interface …………………… 35 Section 5.3 The STATUS Register and Flag Bits ……………. 39 Section 5.4 The Keypad Software ……………………………. 40 Section 5.5 The LED Display Software ……………………… 43 Section 5.6 Improved Display and Indirect Addressing ……… 46 Section 5.7 Odds & Ends …………………………………….. 50 Section 5.8 Using KEY_SCAN and DISPLAY Together ……. 54 Section 5.9 A Last Look at the Advanced Security System ….. 56 Section 5.10 Summary of Instructions and Concepts ………… 57 Chapter 6 Fundamental ESP Techniques ……………………………. 59 Section 6.0 Chapter Summary ………………………………… 59 Section 6.1 Introduction ………………………………………. 59 Section 6.2 Software Readability …………………………… 59 Section 6.3 Software Maintainability …………………………. 60 2 Chapter 6 Section 6.4 Software Fundamentals ………………………….. 60 Section 6.5 The Background Routine ………………………… 61 Section 6.6 The Watch-Dog Timer …………………………… 61 Section 6.7 Event-Driven Software …………………………... 62 Section 6.8 Interrupts …………………………………………. 65 Section 6.9 Slow Inputs and Outputs …………………………. 65 Section 6.10 Software Time Measurement …………………… 66 Section 6.11 Hashing …………………………………………. 67 Section 6.12 Waveform Encoding ……………………………. 68 Section 6.13 Waveform Decoding …………………………… 71 Section 6.14 RAM, ROM, and Time Tradeoffs ……………… 75 Section 6.15 ROM States ……………………………………... 75 Section 6.16 Limitations of C/C++ …………………………… 76 Chapter 7 Advanced ESP ……………………………………………. 78 Section 7.0 Chapter Summary ………………………………… 78 Section 7.1 Introduction ………………………………………. 78 Section 7.2 Sine Wave Generation …………………………….78 Section 7.3 Dual-Tone-Multi-Frequency (DTMF) Signaling …81 Section 7.4 Pulse-Width Modulation ………………………….82 Section 7.5 ADPCM Data Compression ………………………84 Section 7.6 Test Functions and System Ideas …………………87 Chapter 8 PIC Peripherals and Interrupts ……………………………91 Section 8.0 Chapter Summary ………………………………...91 Section 8.1 Overview of the PIC Peripherals …………………91 Section 8.2 Input/Output Ports ………………………………..93 Section 8.2.1 Port A ………………………………………….. 93 Section 8.2.2 Port B, Port C, Port D ………………………….. 95 Section 8.2.3 Port E ………………………………………….. 95 Section 8.3 Interrupts …………………………………………. 95 Section 8.4 ADC and Analog MUX ………………………….. 98 Section 8.5 Watch-Dog Timer ………………………………... 102 Section 8.6 Timer 0 …………………………………………… 103 Section 8.7 Timer 1 ………………………………………….. 105 Section 8.8 Timer 2 ………………………………………….. 106 Section 8.9 Capture Mode …………………………………… 107 Section 8.10 Compare Mode ………………………………… 109 Section 8.11 Pulse-Width Modulation (PWM) ……………… 111 Section 8.12 Parallel Slave Port ……………………………… 114 3 Chapter 8 Section 8.13 EEPROM Data Memory ………………………… 116 Section 8.14 FLASH Program Memory ………………………. 117 Section 8.15 FLASH Code & Data EEPROM Protection ……. 118 Section 8.16 The CONFIGURATION Word ………………… 119 Section 8.17 Sleep Modes & Reset Modes …………………… 120 Chapter 9 PIC Peripherals, Serial Communications Ports ……………122 Section 9.0 Chapter Summary …………………………………122 Section 9.1 Introduction ……………………………………….122 Section 9.2 USART (Overview) ………………………………122 Section 9.2.1 USART (Asynchronous Mode, Full-Duplex) …..123 Section 9.2.2 USART (Synchronous, Master Mode) ………… 127 Section 9.2.3 USART (Synchronous, Slave Mode) ………….. 128 Section 9.3 Serial Peripheral Interface (Master Mode) ………. 128 Section 9.4 Serial Peripheral Interface (Slave Mode) …………132 Section 9.5 I2C System Overview …………………………….134 Section 9.5.1 I2C Slave Mode …………………………………137 Section 9.5.2 I2C Master Mode ………………………………. 139 Chapter 10 DSP Fundamentals ……………………………………… 147 Section 10.0 Chapter Summary ………………………………. 147 Section 10.1 Introduction …………………………………….. 147 Section 10.2 An Example: A Low-Pass Filter ……………….. 148 Section 10.3 An Example: A High-Pass Filter ………………. 148 Section 10.4 DSP Filters in General ………………………… 149 Section 10.5 Aliasing and the Nyquist Sampling Theorem ……149 Section 10.6 DSP Cookbook I: A Simple LPF/HPF …………. 151 Section 10.7 DSP Cookbook II: A Simple BPF ……………… 152 Section 10.8 DSP Cookbook III: A Median Filter …………… 153 Section 10.9 DSP Example I: Standard DTMF Decoding …….154 Section 10.10 DSP Example II: Alternative DTMF Decoding .. 155 Section 10.11 DSP Application: Speech Compression ………… 159 Appendix A The PIC16F877 Instruction Set ………………………….. 163 Appendix B Useful C++ Programs for PIC ASM Applications ………. 180 Appendix C Special Function Registers (RAM Addresses & Bits) ……185 Appendix D PIC16F877 Register File Map ……………………………191 Appendix E PIC16F877 Pin Function Map ……………………………192 Appendix F Save/Restore Registers on Interrupt ……………………… 193 References ………………………………………………………………. 195 4 Preface This book is intended for use by Junior-level undergraduates, Senior-level undergraduates, and Graduate students in electrical engineering as well as practicing electrical engineers and hobbyists and seeks to provide a gentle introduction to embedded systems programming with the Microchip PIC16F877 microcontroller. After introducing the PIC16F877 and its programming, this book covers the fundamental techniques and advanced level techniques of embedded systems programming in a general sense. The general sense ESP techniques can be applied to any microcontroller. There is also an introduction to the fundamentals of digital signal processing (DSP) using the PIC16F877. I would like to thank Dr. Dan Simon of the Cleveland State University Electrical Engineering Department for his kind and valuable help and suggestions in the preparations for this book. I would also like to thank John R. Owerko and James R. Jackson, both of A.R.F Products, Inc., for their expertise in the security systems market. I owe them both a great debt for my knowledge of security systems and for expanding my knowledge of the techniques of embedded systems programming in general. Special thanks also go to Sister Renee Oliver who proofread the manuscript and offered many helpful suggestions. Thanks go to my friends Damian Poirier, Jim Strieter, Greg Glazer, Zarif Bastawros, Brian McGeever, Ted Seman, and Jim Chesebrough who offered many helpful suggestions. Any errors that remain in the text are mine and I will correct them in the next edition. Timothy D. Green November 2005 Cleveland, Ohio 5 List of Figures 2-1 uP Internal View Block Diagram 2-2 PIC16F877 Internal Block Diagram 3-1 Simple Hardware View (Ports Only) 3-2 Basic Hardware System Example 4-1 A Simple Security System 5-1 Twelve-Key Matrix Keypad 5-2 PIC Matrix Keypad Interface Circuit 5-3 Seven Segment LED Digit Display in Common Cathode and Common Anode Forms 5-4 Single LED Digit Drives for Common Cathode/Anode Forms 5-5a Multiplexed LED Digit Drives for Common Cathode Form 5-5b Multiplexed LED Digit Drives for Common Anode Form 5-6 Illustration of Indirect RAM Addressing 5-7 Diagram of RLF and RRF Instructions 6-1 “All Digital” Watch-Dog Timer Circuit 6-2 Event-Driven Push-Button Switch DeBouncing 6-3 Manchester Code Waveform 6-4 Decoding of Manchester Waveform 7-1 Gaussian Probability Density Function and a Set of Sampled Values 8-1 ADCON1 “Analog vs Digital” Selection Codes 8-2 PIC16F877 Interrupt Tree 9-1 Master Mode SPI Mode Timing 9-2 Serial-Out/Serial-In with the 74HC164 and 74HC165 9-3 Serial-Out/Serial-In with Gated Clock to Inhibit Serial Out 9-4 SPI Mode Timing (Slave Mode, CKE = 0) 9-5 SPI Mode Timing (Slave Mode, CKE = 1) 10-1 Example of Aliasing When Sampling an Analog Signal 10-2 “Slow-to-Fast” Mode Speech Compression Process 10-3 “Fast-to-Slow” Mode Speech Compression Process Appendix A Figure: Diagram of RLF and RRF Instructions 6 Abbreviations and Acronyms ABS = Absolute Value ACCUM = Accumulator ADC = Analog-to-Digital Converter ADPCM = Adaptive Differential Pulse Code Modulation ALU = Arithmetic Logic Unit Arccos = Arc-Cosine ASCII = American Standard Code for Information Interchange ATM = Automatic Teller Machine BOR = Brown Out Reset BPF = Band Pass Filter CK = Clock Cos = Cosine CPU = Central Processing Unit D = Data DAC = Digital-to-Analog Converter dB = Decibels DC = Direct Current DDS = Direct Digital Synthesis DIP = Dual Inline Package DPSK = Differential Phase Shift Keying DSP = Digital Signal Processing DTMF = Dual Tone Multi-Frequency EEPROM = Electrically Erasable Programmable Read Only Memory EMC = Electro-Magnetic Compatibility EMI = Electro-Magnetic Interference EPROM = Erasable Programmable Read Only Memory ESP = Embedded Systems Programming Fmax = Maximum Frequency Fosc = Oscillator