Saturday, October 24, 2009

PIC Microcontroller Project Book

robot PIC Microcontroller ProjectsWhat can you do with PIC microcontrollers? Practically anything from creating robots that hunt light to feed their solar cells to making toasters announce, "Your toast is ready". These low-cost (about $7) computers-in-a-chip are 20X faster than the more costly Basic Stamps, and they're almost as easy to use. The PIC Microcontroller Project Book gives you hands-on directions for putting these chips to work. Starting with simple projects and experiments, this book leads you gradually into sophisticated programming techniques using Microchip's free MPLAB compiler and the BASIC programming language. No previous programming experience is necessary. John Iovine coaches you through every single step.

Written with the beginner in mind, this project-oriented guide gives you twelve complete projects, including: add sensing abilities to robots, build decision-making neural and "fuzzy logic" functions into your projects, convert any analog signal to digital, deliver messages on a LCD, synthesize human speech, control DC motors, stepper motors, and servos, read resistive sensors for robotics applications, build a frequency generator, make a DTMF phone number logger and distinct ring detector and router, build a digital oscilloscope, and home automation using X-10 communications.


Chapter 1: Microcontroller

What Is a Microcontroller?

A microcontroller is an inexpensive single-chip computer. Single-chip computer means that the entire computer system lies within the confines of the integrated circuit chip. The microcontroller on the encapsulated sliver of silicon has features similar to those of our standard personal computer. Primarily, the microcontroller is capable of storing and running a program (its most important feature). The microcontroller contains a CPU (central processing unit), RAM (random-access memory), ROM (read-only memory), 1/O (input/output) lines, serial and parallel ports, timers, and sometimes other built-in peripherals such as A/D (analog-to-digital) and D/A (digital-to-analog) converters.

Why Use a Microcontroller?

Microcontrollers, as stated, are inexpensive computers. The microcontroller's ability to store and run unique programs makes it extremely versatile. For instance, one can program a microcontroller to make decisions (perform functions) based on predetermined situations (1/O-line logic) and selections. The microcontroller's ability to perform math and logic functions allows it to mimic sophisticated logic and electronic circuits.

Other programs can make the microcontroller behave like a neural circuit and/or a fuzzy-logic controller. Microcontrollers are responsible for the "intelligence" in most smart devices on the consumer market.

The Future of Electronics Is Here-It's Microcontrollers

Look in any hobbyist electronics magazine from this country or any other. You will see articles that feature the use of microcontrollers, either directly or embedded in the circuit's design. Because of their versatility, microcontrollers add a lot of power, control, and options at little cost. It therefore becomes essential that the electronics engineer or hobbyist learn to program these microcontrollers to maintain a level of competence and to gain the advantages microcontrollers provide in his or her own circuit designs.

If you examine consumer electronics, you will find microcontrollers embedded in just about everything. This is another reason to become familiar with microcontrollers.

Designer Computers

There is a large variety of microcontrollers on the market today. We will focus on a few versatile microcontroller chips called PIC chips (or PICMicro chips) from Microchip Technology.

The PIC Chip

Microchip Technology's series of microcontrollers is called PIC chips. Microchip secured a trademark for the name PIC. Microchip uses PIC to describe its series of PIC microcontrollers. PIC is generally assumed to mean programmable interface controller.

Better Than Any Stamp

Parallax Company sells an easy-to-use series of microcontroller circuits called the Basic Stamp. Parallax's Basic Stamps (BS1 and BS2) use Microchip Technology's PIC microcontrollers. What makes the Stamps so popular and easy to use is that they are programmed using a simplified form of the Basic language. Basic-language programming is easy to learn and use. This was the Stamps' main advantage over other microcontroller systems, which have a much longer learning curve because they force their users and developers to learn a niche assembly language. (A niche assembly language is one that is specific to that company's microcontroller and no one else's.)

The Basic Stamp has become one of the most popular microcontrollers in use today. Again, the Basic Stamp's popularity (this bears repeating) is due to its easy-to-learn and easy-to-use Basic-language programming. The PIC's Basic language system is just as easy to learn and use, and the PIC has enormous benefits that make it better than any Stamp.

The Basic language of the PICBasic compiler that we will use to program the PIC chips is similar to that used in the Basic Stamp series. Programming PIC chips directly has just become as easy as programming Stamps. Now you can enjoy the same easy language the Basic Stamp offers, plus two more very important benefits.

Benefit one: faster speed

Our programmed PIC chips will run their program much faster. If we enter the identical Basic program into a Basic Stamp and into a PIC chip, the programmed PIC chip will run 20 to 100 times faster (depending upon the instructions used) than the Basic Stamp. Here's why.

The BS1 and BS2 Basic Stamp systems use a serial EEPROM memory connected to the PIC chip to store their programs. The basic commands in the program are stored as basic tokens. Basic tokens are like a shorthand for basic commands. When running the program, the Basic Stamp reads each instruction (token and data/address) over the serial line from the external EEPROM memory, interprets the token (converts token to the ML equivalent the PIC can understand), performs the instruction, reads the next instruction, and so on. Each and every instruction goes through these serial load, read, interpret, then perform steps as the program runs. The serial interface reading routine eats up gobs of the microcontroller's CPU time.

In contrast to this operation, when a PIC chip is programmed using the Basic compiler, the Basic program is first converted to a PIC machine language (hex file) program. The ML program is then uploaded into the PIC chip. Being the native language of the PIC, this machine-language (ML) code does not need to be stored as tokens and interpreted as it runs because the program is written in the PIC chip's native language.

When the PIC chip runs the program, it reads the ML program instructions directly from its on-board memory and performs the instruction. There is no serial interface to an external EEPROM to eat up CPU time. The ML instructions are read in parallel, not bit by bit as in the serial interface. The ML instructions read directly without any basic-token-to-ML-equivalent conversion required. This enables programmed PIC chips to run their code 20 to 100 times faster than the same Basic program code in a Basic Stamp.

Benefit two: much lower cost

The next factor is cost. Using PIC chips directly will save you 75 percent of the cost of a comparable Basic Stamp. The retail price for the BS1, which has 256 bytes of programmable memory, is $34.95. The retail price for the BS2, which has 2K of programmable memory, is $49.95. The 16F84 PIC microcontroller featured throughout this book is more closely comparable to the BS2 Stamp. The 16F84 PIC chip we are using has 1K of programmable memory.

The retail cost of the 16F84 PIC chip is $6.95. To this, add the cost of a timing crystal, a few capacitors, a few resistors, and a 7805 voltage regulator to make a circuit equivalent to that of the Stamp. These components increase the total cost to about $10.00-still well below one-quarter the cost (75 percent savings) currently quoted for the BS2...

No comments:

Post a Comment