Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
Скачиваний:
475
Добавлен:
12.08.2013
Размер:
4.99 Mб
Скачать

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.