
- •Table of Contents
- •Preface
- •Additional Material
- •Basic Electronics
- •1.0 The Atom
- •1.1 Isotopes and Ions
- •1.2 Static Electricity
- •1.3 Electrical Charge
- •1.4 Electrical Circuits
- •1.5 Circuit Elements
- •1.6 Semiconductors
- •Number Systems
- •2.0 Counting
- •2.1 The Origins of the Decimal System
- •2.2 Types of Numbers
- •2.3 Radix Representations
- •2.4 Number System Conversions
- •Data Types and Data Storage
- •3.0 Electronic-Digital Machines
- •3.1 Character Representations
- •3.2 Storage and Encoding of Integers
- •3.3 Encoding of Fractional Numbers
- •3.4 Binary-Coded Decimals (BCD)
- •Digital Logic, Arithmetic, and Conversions
- •4.0 Microcontroller Logic and Arithmetic
- •4.1 Logical Instructions
- •4.2 Microcontroller Arithmetic
- •4.3 Bit Manipulations and Auxiliary Operations
- •4.4 Unsigned Binary Arithmetic
- •4.5 Signed Binary Arithmetic
- •4.6 Data Format Conversions
- •Circuits and Logic Gates
- •5.0 Digital Circuits
- •5.1 The Diode Revisited
- •5.2 The Transistor
- •5.3 Logic Gates
- •5.4 Transistor-Transistor Logic
- •5.5 Other TTL Logic Families
- •5.6 CMOS Logic Gates
- •Circuit Components
- •6.0 Power Supplies
- •6.1 Clocked Logic and Flip-flops
- •6.2 Clocks
- •6.3 Frequency Dividers and Counters
- •6.4 Multiplexers and Demultiplexers
- •6.5 Input Devices
- •The Microchip PIC
- •7.0 The PICMicro Microcontroller
- •7.1 PIC Architecture
- •Mid-range PIC Architecture
- •8.0 Processor Architecture and Design
- •8.1 The Mid-range Core Features
- •8.2 Mid-Range CPU and Instruction Set
- •8.3 EEPROM Data Storage
- •8.4 Data Memory Organization
- •8.5 Mid-range I/O and Peripheral Modules
- •PIC Programming: Tools and Techniques
- •9.0 Microchip’s MPLAB
- •9.1 Integrated Development Environment
- •9.2 Simulators and Debuggers
- •9.3 Programmers
- •9.4 Engineering PIC Software
- •9.5 Pseudo Instructions
- •Programming Essentials: Input and Output
- •10.0 16F84A Programming Template
- •10.1 Introducing the 16F84A
- •10.2 Simple Circuits and Programs
- •10.3 Programming the Seven-segment LED
- •10.4 A Demonstration Board
- •Interrupts
- •11.0 Interrupts on the 16F84
- •11.1 Interrupt Sources
- •11.2 Interrupt Handlers
- •11.3 Interrupt Programming
- •11.4 Sample Programs
- •Timers and Counters
- •12.0 The 16F84 Timer0 Module
- •12.1 Delays Using Timer0
- •12.2 Timer0 as a Counter
- •12.3 Timer0 Programming
- •12.4 The Watchdog Timer
- •12.5 Sample Programs
- •LCD Interfacing and Programming
- •13.0 LCD Features and Architecture
- •13.1 Interfacing with the HD44780
- •13.2 HD44780 Instruction Set
- •13.3 LCD Programming
- •13.4 Sample Programs
- •Communications
- •14.0 PIC Communications Overview
- •14.1 Serial Data Transmission
- •14.2 Parallel Data Transmission
- •14.4 PIC Protocol-based Serial Programming
- •14.5 Sample Programs
- •Data EEPROM Programming
- •15.0 PIC Internal EEPROM Memory
- •15.1 EEPROM Devices and Interfaces
- •15.2 Sample Programs
- •Analog to Digital and Realtime Clocks
- •16.0 A/D Converters
- •16.1 A/D Integrated Circuits
- •16.2 PIC On-Board A/D Hardware
- •16.3 Realtime Clocks
- •16.4 Sample Programs
- •Index

Chapter 7
The Microchip PIC
A microcontroller is a type of microprocessor furnished in a single integrated circuit and needing a minimum of support chips. Its principal nature is self-sufficiency and low cost. It is not intended to be used as a computing device in the conventional sense; that is, a microcontroller is not designed to be a data processing machine, but rather an intelligent core for a specialized dedicated system.
Microcontrollers are embedded in many control, monitoring, and processing systems. Some are general-purpose devices but most microcontrollers are used in specialized systems such as washing machines, telephones, microwave ovens, automobiles, and weapons of many kinds. A microcontroller usually includes a central processor, input and output ports, memory for program and data storage, an internal clock, and one or more peripheral devices such as timers, counters, analog-to-digital converters, serial communication facilities, and watchdog circuits.
More than two dozen companies in the world manufacture and market microcontrollers. They range from 8- to 32-bit devices. Those at the low end are intended for very simple circuits and provide limited functions and program space, while those at the high end have many of the features associated with microprocessors. The most popular ones include several from Intel (such as the 8051), Zilog (derivatives of their famous Z-80 microprocessor), Motorola (such as the 68HC05), Atmel (the AVR), Parallax (the BASIC Stamp), and Microchip. Some of the latter ones are the main topic of this book.
7.0 The PICMicro Microcontroller
PIC is a family of microcontrollers made by Microchip Technology. The original one was the PIC1650 developed by General Instruments. This device was called PIC for “Programmable Intelligent Computer” although it is now associated with “Programmable Interface Controller.” Microchip does not use PIC as an acronym. Instead they prefer the brand name PICmicro. Popular wisdom relates that PIC is a registered brand in Germany and Microchip is unable to use it internationally.
129
130 |
Chapter 7 |
The original PIC was built to be used with General Instruments’ CP1600 processor, which had poor I/O performance. The PIC was designed to take over the I/O tasks for the CPU, thus improving performance. In 1985, the PIC was upgraded with EPROM to produce a programmable controller. Today, a huge variety of PICs are available with many different on-board peripherals and program memories ranging from a few hundred words to 32K.
PICs use an instruction set that varies in length from about 35 instructions for the low-end PICs to more than 70 for the high-end devices. The accumulator, which is known as the work register in PIC documentation, is part of many instructions since the PIC contains no other internal registers accessible to the programmer. The PICs are programmable in their native Assembly Language, which is straightforward and not difficult to learn. In addition, C language and BASIC compilers have been developed for the PIC. Open-source Pascal, JAL, and Forth compilers are also available for PIC programming.
One of the reasons for the success of the PIC is the support provided by Microchip. This includes a professional-quality development environment called MPLAB which can be downloaded free from the company’s website (). The MPLAB package includes an assembler, a linker, a debugger, and a simulator. Microchip also sells a low-cost in-circuit debugger called MPLAB ICD 2. Other development products intended for the professional market are available from Microchip. The Microchip website furnishes hundreds of free support documents, including data sheets, application notes, and sample code.
In addition to the documents and products in the Microchip website, the PIC microcontrollers have gained the support of many hobbyists, enthusiasts, and entrepreneurs who develop code and support products and publish their results on the Internet. This community of PIC users is a treasure trove of information and know-how easily accessible to the beginner and useful even to the professional. One such Internet resource is an open-source collection of PIC tools named GPUTILS, which is distributed under the GNU General Public License. GPUTILS includes an assembler and a linker. The software works on Linux, Mac OS, OS/2, and Windows. Another product named GPSIM is an Open Source simulator featuring PIC hardware modules.
7.0.1 Programming the PIC
Programming a PIC microcontroller requires the following tools and components:
1.An Assembler or high-level language compiler. The software package usually includes a debugger, simulator, and other support programs.
2.A computer (usually a PC) in which to run the development software.
3.A hardware device called a programmer that connects to the computer through the serial, parallel, or USB line. The PIC is inserted in the programmer and “blown” by downloading the executable code generated by the development system. The hardware programmer usually includes the support software.
4.A cable or connector for connecting the programmer to the computer.
5.A PIC microcontroller.

The Microchip PIC |
131 |
Figure 7-1 USB PIC Programmer by MicroPro
PIC Programmers
The development system (assembler or compiler) and the programmer driver are the software components. The computer, programmer, and connectors are the hardware elements. Figure 7-1 shows a commercial programmer that connects to the USB port of a PC. The one in the illustration is made by MicroPro.
Many other programmers are available on the market. Microchip offers several high-end models with in circuit serial programming (ICSP) and low voltage programming (LVP) capabilities. These devices allow the PIC to be programmed in the target circuit. Some PICs can write to their own program memory. This makes possible the use of so-called bootloaders, which are small resident programs that allow loading user software over the RS-232 or USB lines. Programmer/debugger combinations are also offered by Microchip and other vendors.
Development Boards
A development board is a demonstration circuit that usually contains an array of connected and connectable components. Their main purpose is as a learning and experimental tool. Like programmers, PIC development boards come in a wide range of prices and levels of complexity. Most boards target a specific PIC microcontroller or a PIC family of related devices. Lacking a development board the other option is to build the circuits oneself, a time-consuming but valuable experience. Figure 7-2 (in the following page) shows the LAB-X1 development board for the 16F87x PIC family.
The LAX-X1 board, as well as several other models, is a product of microEngineering Labs, Inc. Some of the sample programs developed for this book were tested on a LAB-X1 board. Development boards from Microchip and other vendors are also available.

132 |
Chapter 7 |
Figure 7-2 LAB-X1 Development Board
7.0.2 Prototyping the PIC Circuit
Very few of us are satisfied with writing a PIC program and assuming that it works correctly. Testing software is a simple matter if there happens to be a development board at hand, if the board is compatible with the PIC, and if it provides the hardware that we need to test. But often one of these elements is missing and it becomes necessary to build the circuit for which the program was designed. Here again, there are several options. These range from having the circuit built for us by a professional engineering firm, to using a breadboard to prototype the circuit ourselves.
Breadboarding a prototype circuit is one of the options. A breadboard is a reusable, solderless device that allows building a prototype circuit, usually for temporary use. Breadboards have strips down one or both sides that are used as power rails. One strip carries the circuit’s positive voltage and the other one is wired to the ground of the power supply. Wire jumper kits provide connectors of different lengths and colors for making the circuit connections on the breadboard. For complex circuits several breadboards can be easily interconnected. Figure 7-3 shows two interconnected breadboards used to test one of the programs developed for this book.

The Microchip PIC |
133 |
Figure 7-3 Circuits in Two Interconnected Breadboards
Once a circuit and the software have been tested, there are several available technologies for building a more permanent prototype. These include wire wrap, stripboards, and several other circuit board building tools and techniques, including prototyping boards specially designed for PIC circuits.
Finally, one can build a semi-professional quality printed circuit board (called a PCB) and solder the components to it. A PCB is used to mechanically support the electronic components and provides conductive pathways, called traces, that implement the circuit. The components are soldered to the PCB board using either surface mount or through-the-board technology. The PCB board is made of a non-conductive material and the conductive pathways are etched out of copper sheets laminated on one or both sides of the board. Once the board has been populated with electronic components it becomes a printed circuit assembly, or PCA. Industrial quality PCB boards are suited to high-volume production. The circuits of the development board in Figure 7-2 are on a commercial PCB.
Building one’s own PCB is quite possible and requires few tools and resources. Appendix B describes one technique that has been used successfully. Figure 7-4, in the followng page, shows a drawing of both sides of a simple PCB board.