MAKING PIC MICROCONTROLLER INSTRUMENTS AND CONTROLLERS HARPRIT SINGH SANDHU New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto
CONTENTS Preface xiu PART I The PIC 16F877A Chapter 1 An Introduction to the PIC 16F877A Microcontroller Unit The Microcontroller 7 Special Precautions and Notes of Interest 9 Data Sheets 9 Some Useable PICS 11 Chapter 2 Getting Started: The Hardware and Software Setup The Programmers 14 Loading the Software 15 Using the Software in the Windows Environment 15 Software Notes from MicroEngineering Labs 18 Chapter 3 Understanding Microchip Technology's PIC 16F877A: A Description of the MCU 16F877A Microcontroller's Core Features 22 Peripheral Features 23 Configuring and Controlling the Properties of the Ports 29 PORTA 29 PORTB 30 PORTC 31 PORTD 32 PORTE 33 TIMERS 33 Chapter 4 The Software, the Compilers, and the Editor The Basic Compiler Instruction Set 35 The PICBASIC PRO Compiler Instruction Set 37 PICBASIC PRO Compiler 42 Chapter 5 Controlling the Output and Reading the Input General 47 Programs That Create Output 48 Programs That Read the Inputs and Then Provide Output 48 Creating Outputs 48 13 21 35 47
viii CONTENTS Blink One LED 50 Blink Eight LEDs in Sequence 51 Dim and Brighten One LED 52 LCD Display 53 Controlling the Digital and Analog Settings 56 Writing Binary, Hex, and Decimal Values to the LCD 56 Reading a Potentiometer and Displaying the Results on the LED Bargraph 57 A Simple Beep 60 Advanced Exercise: Controlling an RC Servo from the Keyboard 63 Reading the Inputs 67 Read Keyboard and Display Key Number on the LCD 73 Read One Potentiometer and Display Its 8-Bit Value on LCD in Binary, Hex, and Decimal Notation, Also Impress the Binary Value on the Bargraph 74 Read All Three Potentiometers and Display Their Values on the LCD 76 Adding the Kind of Flexibility That Defines Computer Interfaces and Demonstrates the Ability to Make Sophisticated Real-Time Adjustments 78 Exercises 79 Chapter 6 Timers and Counters 83 General 83 Timers 84 TimerO 85 Timerl: The Second Timer 93 Timer2: The Third Timer 102 Counters 104 Exercises for Timers 112 Exercises for Counters 112 Chapter 7 Clocks, Memory, and Sockets 113 Sockets U3, U4, and U5: For Serial One-Wire Memory Devices 113 Which EEPROM Type Should You Use? 115 Socket U3 I2C SEEPROM 115 Socket U4 SPI SEEPROM 117 Socket U5 Microwire Devices 118 Socket U6 Real-Time Clocks 120 The LTC1298 12-Bit A-to-D Converter (Also Used in Socket U6) 124 Sockets U7 (and U8) 126 Chapter 8 Serial Communications: Sockets U9 and UIO 131 When and How Will I Know if It Is Working? 134 Using the RS485 Communications 137 Chapter 9 Using Liquid Crystal Displays: An Extended Information Resource 139 General 139 Using LCDs in Your Projects 142 Understanding the Hardware and Software Interaction 143 Talking to the LCD 144
CONTENTS ix The Hardware 144 Setting Out Our Design Intent 146 Liquid-Crystal Display Exercises 154 PART II The Projects 157 Chapter 10 Using Sensors (Transducers) 159 General 159 The Most Basic Question We Must Answer Is... 161 Types of Sensors 163 Two Interesting Resources You Will Want to Investigate 164 Chapter 11 Conditioning the Input Signal 165 General 165 Alternating Current Outline 166 Direct Current Outline 166 Simple Switches and Other Contacts 167 Circuitry for Conditioning dc Signals 169 Chapter 12 Conditioning the Output Signal 173 General 173 Chapter 13 An Introduction to the Eight Projects 177 The Web Site 177 The Eight Techniques 177 Notes 182 Chapter 14 The Universal Instrument: A Background Discussion 183 The Properties and Capabilities of a Universal Instrument 183 A Basic Temperature-Controlling Device 184 Notes 186 Chapter 15 Counting Pulses: A Programmable Tachometer 187 Project 1 187 Notes on Using Seven-Segment Displays 199 Chapter 16 Creating Accurate Intervals with Timers: The Metronomes 209 Project 2 209 TimerO 214 Timerl 224 Timer2 226 The Timer2 Program 229 The Watchdog Timer 230
Chapter 17 Understanding the Counters: Counting Marbles 233 Project 3 233 < Counting with an Escapement 240 Some Real-World Notes 243 Counting to a Register Using an Interrupt 244 Counting Directly into an Internal Counter 246 Using TimeM in Counter Mode 248 Special Notes for Timerl Usage 256 Chapter 18 A Dual Thermometer Instrument 259 Project 4 259 Chapter 19 An Artificial Horizon: A Table Surface That Stays Level 269 Project 5 269 Discussion 270 Setting Up the Hardware Connections 271 Building the Artificial Horizon Table 275 Gravity Sensor Exercises 277 Chapter 20 Building a Simple Eight-Button Touch Panel 279 Project 6 279 Chapter 21 Single Set Point Controller with Remote Inhibit Capability 293 Project 7 293 Chapter 22 Logging Data from a Solar Collector 301 Project 8 301 Microcontroller Hardware 304 Software 306 Chapter 23 Debugging 315 General 315 Debugging and Troubleshooting 315 First Problem That Must Be Fixed: The Microcontroller Crystal Must Oscillate 316 If the Chip Refuses to Run 318 Using the PBP Compiler Commands to Help Debug a Program 319 Commands That Can Provide Debug Output to a Serial Port 319 Dumb Terminal Programs 319 Solderless Breadboards 320 Debugging at the Practical Level 320 Configuring the 16F877A and Related Notes 324 Settings 326 Configuration 326 Options 327 Simple Checks 327
CONTENTS xi Some Programmer-Related Error Messages 328 Things I Have Noticed but Have Not Figured Out (and Other Mysteries) 328 Setting the Ports 329 Chapter 24 Some Real-World Projects You Can Build 331 Conclusion 335 Appendixes 337 Appendix A Setting Up a Compiler for One-Keystroke Operation 339 Appendix В Abbreviations Used in this Book and in the Datasheets 341 Appendix С Listings of PICBASIC PRO Programs on the Internet at melabs.com 345 Appendix D Notes on Designing a Simple Battery Monitor Instrument: Thinking about a Simple Problem Out Loud 347 Appendix E Using the Support Web Site to Help Make Instruments and Controllers 349 Index 351