Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8508

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Preface

There are two sides to the computer revolution: one is represented by the PC on your desktop and the second one by the device that remote-controls your TV, monitors and operates your car engine, and allows you to set up your answering machine and your microwave oven. At the core of the PC you find a microprocessor, while at the heart of a self-contained programmable device (also called an embedded system) is a microcontroller.

Microcontrollers are virtually everywhere in our modern society. They are found in automobiles, airplanes, toys, kitchen appliances, computers, TVs and VCRs, phones and answering machines, space telescopes, and practically every electronic digital device that furnishes an independent functionality to its user. In this sense a microcontroller is a self-contained computer system that includes a processor, memory, and some way of communicating with the outside world, all in a single chip that can be smaller than a postage stamp.

A microcontroller (sometimes called an MCU) is actually a computer on a chip.

Essentially it is a control device and its design places emphasis on being self-suffi- cient and inexpensive. The typical microcontroller contains all the components and features necessary to perform its functions, such as a central processor, input/output facilities, timers, RAM memory for storing program data and executable code, and a clock or oscillator that provides a timing beat. In addition, some microcontrollers include a variety of additional modules and circuits. Some common ones are serial and parallel communications, analog-to-digital converters, realtime clocks, and flash memory.

Engineers, inventors, experimenters, students, and device designers in general deal with microcontrollers on an everyday basis. In fact, interest in microcontrollers is not limited to electrical, electronic, and computer engineers. Mechanical and automotive engineers, among many others, often design devices or components that contain microcontrollers. The system that controls the hatch of a ballistic missile silo and the one that operates the doglike toy that barks and rolls on its back, both contain microcontrollers.

The Microchip PIC

Microcontrollers include an enormous array of models and variations of generaland special-purpose devices. Discussing all of them in a single volume would have forced a superficial scope. Even the products of a single manufacturer can have a mind-bog-

XV

XVI

Preface

gling variety, which sometimes include hundreds of different MCU models in a half-dozen families, all with very different applications and features.

For this reason we have focused the book on a single type of microcontroller: the M i c r o c h i p P I C . N o t o n l y a r e t h e P I C t h e m o s t u s e d a n d b e s t k n o w n microcontrollers, they are also the best supported. In fact, PIC system design and programming has become a powerful specialization with a large number of professional and amateur specialists. There are hundreds of WEB sites devoted to PIC-re- lated topics. An entire cottage industry of PIC software and hardware has flourished around this technology.

For practical reasons we have limited the book's scope to 8-bit PICs. In fact, the book concentrates on a particular type of 8-bit PIC known as the mid-range family. We have chosen this approach partly because of space limitations and partly due to the fact that 16and 32-bit microcontrollers (sometimes called external memory microcontrollers) are more related to microprocessor technology than to the topic at hand.

The Book's Design

The book is intended as a resource kit for PIC microcontroller programming. But programming microcontrollers is a different paradigm from microprocessor programming. PIC programming requires a set of skills and a knowledge base quite different from the one needed by a computer programmer. The reason is that the designer/programmer is responsible for the entire system. A typical embedded system has no DOS, Windows, or UNIX software to handle the operational and housekeeping chores. Thus, the PIC programmer provides all the functionality needed by the application with very little assistance from other programs. This makes the microcontroller programmer an application developer, a system's programmer, and an input/output specialist, all at the same time.

For these reasons, the microcontroller programmer must be familiar with a host of computer science topics, including low-level data representations, binary arithmetic, computer organization, input/output programming, concurrency and scheduling, memory management, timing operations, and system functions. At the same time, he or she must be quite conversant with digital electronics and circuit design since the object of the program is a hardware device.

In the first six chapters of the book we have attempted to provide the necessary background both in digital electronics and in computer science. Chapters 7, 8, and 9 are an overview of PIC architecture and programming tools. The remainder of the book deals with programming the various functions, modules, and devices. The appendices contain supplementary materials and expand the coding contained in the text. Readers familiar with electronics and circuit design can skip over Chapters 1, 5, and 6. Those well versed in computer science can do the same with Chapters 2, 3, and 4.

Mapleton, Minnesota

Julio Sanchez

June 28, 2006

Maria P. Canton


Preface

XVII

Additional Material

Additional material is available from the CRC Web site:

www.crcpress.com

Under the menu Electronic Products (located on the left side of the screen), click on Downloads & Updates. A list of books in alphabetical order with Web downloads will appear. Locate this book by a search, or scroll down to it. After clicking on the book title, a brief summary of the book will appear. Go to the bottom of this screen and click on the hyperlinked “Download” that is in a zip file.


Chapter 1

Basic Electronics

1.0 The Atom

Until the end of the nineteenth century it was assumed that matter was composed of small, indivisible particles called atoms. The work of J.J. Thompson, Daniel Rutheford, and Neils Bohr proved that atoms were complex structures that contained both positive and negative particles. The positive ones were called protons and the negative ones electrons.

Several models of the atom were proposed: the one by Thompson assumed that there were equal numbers of protons and electrons inside the atom and that these elements were scattered at random, as in the leftmost drawing in Figure 1-1. Later, in 1913, Daniel Rutheford's experiments led him to believe that atoms contained a heavy central positive nucleus with the electrons scattered randomly. So he modified Thompson's model as shown in the center drawing. Finally, Neils Bohr theorized that electrons had different energy levels, as if they moved around the nucleus in different orbits, like planets around a sun. The rightmost drawing represents this orbital model.

-

-

-

+

+

-

-

-

+ +

+ +

+

-

+

+

-

+

+

-

+

+ +

-

-

-

+

-

-

-

Figure 1-1 Models of the Atom

1


2

Chapter 1

Investigations also showed that the normal atom is electrically neutral. Protons (positively charged particles) have a mass of 1.673 X 10-24 grams. Electrons (negatively charged particles) have a mass of 9.109 X 10-28 grams. Furthermore, the orbital model of the atom is not actually valid since orbits have little meaning at the atomic level. A more accurate representation is based on concentric spherical shells about the nucleus. An active area of research deals with atomic and sub-atomic structures.

The number of protons in an atom determines its atomic number; for example, the hydrogen atom has a single proton and an atomic number of 1, helium has 2 protons, carbon has 6, and uranium has 92. But when we compare the ratio of mass to electrical charge in different atoms we find that the nucleus must be made up of more than protons. For example, the helium nucleus has twice the charge of the hydrogen nucleus, but four times the mass. The additional mass is explained by assuming that there is another particle in the nucleus, called a neutron, which has the same mass as the proton but no electrical charge. Figure 1-2 shows a model of the helium atom with two protons, two electrons, and two neutrons.

-

+

+

-

Figure 1-2 Model of the Helium Atom

1.1 Isotopes and Ions

But nature is not always consistent with such neat models. Whereas in a neutral atom, the number of protons in the atomic nucleus exactly matches the number of electrons, the number of protons need not match the number of neutrons. For example, most hydrogen atoms have a single proton, but no neutrons, while a small percentage have one neutron, and an even smaller one have two neutrons. In this sense, atoms of an element that contains different number of neutrons are isotopes of the element; for example water (H2O) containing hydrogen atoms with two neutrons (deuterium) is called "heavy water."

An atom that is electrically charged due to an excess or deficiency of electrons is called an ion. When the dislodged elements are one or more electrons the atom takes a positive charge. In this case it is called a positive ion. When a stray electron combines with a normal atom the result is called a negative ion.

Basic Electronics

3

1.2 Static Electricity

Free electrons can travel through matter or remain at rest on a surface. When electrons are at rest, the surface is said to have a static electrical charge that can be positive or negative. When electrons are moving in a stream-like manner we call this movement an electrical current. Electrons can be removed from a surface by means of friction, heat, light, or a chemical reaction. In this case the surface becomes positively charged.

The ancient Greeks discovered that when amber was rubbed with wool the amber became electrically charged and would attract small pieces of material. In this case, the charge is a positive one. Friction can cause other materials, such as hard rubber or plastic, to become negatively charged. Observing objects that have positive and negative charges we note that like charges repel and unlike charges attract each other, as shown in Figure 1-3.

+

+

-

-

+

-

Figure 1-3 Like and Unlike Charges

Friction causes loosely-held electrons to be transferred from one surface to the other. This results in a net negative charge on the surface that has gained electrons, and a net positive charge on the surface that has lost electrons. If there is no path for the electrons to take to restore the balance of electrical charges, these charges remain until they gradually leak off. If the electrical charge continues building it eventually reaches the point where it can no longer be contained. In this case it discharges itself over any available path, as is the case with lightning.

Static electricity does not move from one place to another. While some interesting experiments can be performed with it, it does not serve the practical purpose of providing energy to do sustained work.

Static electricity certainly exists, and under certain circumstances we must allow for it and account for its possible presence, but it will not be the main theme of these pages.