1 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 Chapter Summary .. 15. Section Memory and Memory Organization . 15. Section The PIC16F877 . 16. Section PROGRAMMING the PIC 17. Chapter 3 Simple PIC Hardware & Software ( Hello World ) .. 20. Section Chapter Summary .. 20. Section A Simple Example system 20. Section Summary of Instructions and Concepts . 25. Chapter 4 The PIC Instruction Set (Part I) .. 27. Section Chapter Summary . 27. Section The PIC16F877 Instruction Set 27. Section Summary of Instructions and Concepts . 33. Chapter 5 The PIC Instruction Set (Part II) . 34. Section Chapter Summary .. 34. Section Introduction 34. Section Keypad and Display Interface 35.
2 Section The STATUS Register and Flag Bits . 39. Section The Keypad Software . 40. Section The LED Display Software 43. Section Improved Display and Indirect Addressing 46. Section Odds & Ends .. 50. Section Using KEY_SCAN and DISPLAY Together . 54. Section A Last Look at the Advanced Security system .. 56. Section Summary of Instructions and Concepts 57. Chapter 6 Fundamental ESP Techniques . 59. Section Chapter Summary 59. Section Introduction . 59. Section Software Readability 59. Section Software Maintainability . 60. 2. Chapter 6. Section Software Fundamentals .. 60. Section The Background Routine 61. Section The Watch-Dog Timer 61. Section Event-Driven Software .. 62. Section Interrupts . 65. Section Slow Inputs and Outputs . 65. Section Software Time Measurement 66. Section Hashing . 67. Section Waveform Encoding . 68. Section Waveform Decoding 71. Section RAM, ROM, and Time Tradeoffs 75. Section ROM States .. 75. Section Limitations of C/C++ 76. Chapter 7 Advanced ESP.
3 78. Section Chapter Summary 78. Section Introduction . 78. Section Sine Wave Generation .78. Section Dual-Tone-Multi-Frequency (DTMF) Signaling 81. Section Pulse-Width Modulation .82. Section ADPCM Data Compression 84. Section Test Functions and system Ideas 87. Chapter 8 PIC Peripherals and Interrupts 91. Section Chapter Summary ..91. Section Overview of the PIC Peripherals 91. Section Input/Output Ports ..93. Section Port A .. 93. Section Port B, Port C, Port D .. 95. Section Port E .. 95. Section Interrupts . 95. Section ADC and Analog MUX .. 98. Section Watch-Dog Timer .. 102. Section Timer 0 103. Section Timer 1 .. 105. Section Timer 2 .. 106. Section Capture Mode 107. Section Compare Mode 109. Section Pulse-Width Modulation (PWM) 111. Section Parallel Slave Port 114. 3. Chapter 8. Section EEPROM Data Memory 116. Section FLASH Program Memory . 117. Section FLASH Code & Data EEPROM Protection . 118. Section The CONFIGURATION Word 119. Section Sleep Modes & Reset Modes 120.
4 Chapter 9 PIC Peripherals, Serial Communications Ports 122. Section Chapter Summary 122. Section Introduction .122. Section USART (Overview) 122. Section USART (Asynchronous Mode, Full-Duplex) ..123. Section USART (Synchronous, Master Mode) 127. Section USART (Synchronous, Slave Mode) .. 128. Section Serial Peripheral Interface (Master Mode) . 128. Section Serial Peripheral Interface (Slave Mode) 132. Section I2C system Overview .134. Section I2C Slave Mode 137. Section I2C Master Mode . 139. Chapter 10 DSP Fundamentals 147. Section Chapter Summary . 147. Section Introduction .. 147. Section An Example: A Low-Pass Filter .. 148. Section An Example: A High-Pass Filter . 148. Section DSP Filters in General 149. Section Aliasing and the Nyquist Sampling Theorem 149. Section DSP Cookbook I: A Simple LPF/HPF . 151. Section DSP Cookbook II: A Simple BPF 152. Section DSP Cookbook III: A Median Filter 153. Section DSP Example I: Standard DTMF Decoding .154. Section DSP Example II: Alternative DTMF Decoding.
5 155. Section 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.
6 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 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.
7 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.
8 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 Frequency (of the PIC).
9 Freq = Frequency 7. FSK = Frequency Shift Keying HPF = High Pass Filter Hz = Hertz IIC or I2C = Inter-Integrated Circuit INT = Interrupt I/O = Input/Output ISR = Interrupt Service Routine kHz = Kilohertz LED = Light Emitting Diode LPF = Low Pass Filter Max = Maximum MHz = Megahertz Min = Minimum ms = Milliseconds MSSP = Master Synchronous Serial Port OSC = Oscillator PC = Personal Computer or Program Counter PIC = Peripheral Interface Controller PISO = Parallel-In, Serial-Out (Shift Register). PLL = Phase-Locked Loop POR = Power-On Reset PROM = Programmable Read Only Memory PSP = Parallel Slave Port PWM = Pulse Width Modulation Q = Flip-Flop, Counter, or Shift Register Output State (Data Out). RAM = Random Access Memory (A Read/Write Memory). RC = Resistor/Capacitor (Time Constant or Circuit). RF = Radio Frequency RFI = Radio Frequency Interference ROM = Read Only Memory Sin or sin = Sine SIPO = Serial-In, Parallel-Out (Shift Register). SPI = Serial Peripheral Interface sqrt = Square Root SR = Sampling Rate 8.
10 UC = Microcontroller uP = Microprocessor USART = Universal Synchronous/Asynchronous Receiver/Transmitter WDT = Watch-Dog Timer XTAL = Quartz Crystal (Sets Oscillator Frequncy). 9. Trademarks IBM and IBM-PC are registered trademarks of International Business Machines, Inc. PIC, PIC16F87X, PIC16F877, MPLAB, MPASM, In-Circuit Debugger, In- Circuit Serial Programmer are registered trademarks of Microchip Technology, Inc. CTI and CTI Speech Compressor are registered trademarks of Compressed Time, Inc. Touch Tone, Unix, C, and C++ are registered trademarks of AT&T, Inc. Linux is a registered trademark of the Free Software Foundation. I2C, IIC, and Inter-Integrated Circuit are registered trademarks of Philips Corp. The PIC Instruction Set, Assembly Keywords, and Mnemonics are Copyrighted by Microchip Technology, Inc. Maxim and MAX690 CPA are registered trademarks of Maxim Corporation. Borland, Turbo, Turbo C++ are registered trademarks of Inprise, Inc. 10. Chapter 1: Introduction to ESP and the PIC.