I had an
idea to create a robot that was small enough to take with me yet smart
enough to perform some interesting tasks. I wanted it to be able to talk
with a PC to do complex tasks, yet be intelligent enough to complete simple
tasks without any outside help. My solution was to create a mini-computer
based on the PIC computer chip.
The chip
I used was the PIC -16F84 because it was not very expensive, had some internal
memory, and could be reprogrammed quickly. I write my software using a
special version of BASIC called PBASIC. This program is available from
Micro Engineering Labs.
Once the
chip is programmed I can test it out to see if all works correctly. If
it doesn't, I can make any software corrections, then just reprogram the
chip. Below is one of my first attempts using the PIC. I designed and built
this prototype inside of a LEGO green plate brick. I like to use the LEGO
Technic series components because they offer great flexibility.
The world is changing.
A few short years ago a project that could communicate with a personal
computer was a major undertaking. You needed some sort of microprocessor,
a section to handle the communications, select the baud rate, and you needed
a crystal with it's own support circuits. It was difficult to know which
devices to use, or how to tie them all together.
Microprocessors were mostly
programmed by machine language. Most home computer users were somewhat
familiar with a language called BASIC. The main difference between
BASIC and machine language, or assembly language is that BASIC contains
commands that are similar to English in structure. Assembly is much faster
than BASIC, but you must specify each step in a task. It may take as many
as eight assembly instructions to perform the same task as one BASIC command.
The home programmer didn't
have a choice; they needed to learn assembly if they wanted to program
a microcontroller. Things are different today.
PBASIC is a simple
language that uses a format very similar to the English language to program
a microcontroller chip. The newest versions of PBASIC have a compile function
either built into them, or as an extra program. This means that you can
write your program using simple BASIC commands, then convert it into a
fast machine language code to be loaded into your microprocessor.
A company called Parallax
came out with a device called the BASIC Stamp Computer. The BASIC
STAMP allowed people to create projects which only a few short years ago
would have been far to complex for the average experimenter. The Stamp
as it is also called, uses a simple version of the BASIC language to develop
and produce complex ideas with an easy to use medium. The Stamp was then
expanded into a more powerful version, The Stamp-2 which had twice the
amount of memory, twice as many connections for interfaces, and ran much
faster. This device is also available from Parallax.
I use a Stamp-2 computer to develop an idea before moving to final production.
I strongly recommend them.
One of the recent developments
is the PIC or Programmable Interface Controller by Microchip.
The PIC is a small but powerful microcomputer that can be programmed to
perform many complex tasks. The BASIC Stamp is actually built from a PIC
chip along with some other support circuitry.
The main advantage of the PIC
over the STAMP is lower overall cost. Often several PIC controllers can
be purchased for the cost of a single Parallax Stamp computer. The drawback
to the PIC however, is that you need a programming
device to load your software into the PIC. The programming device will
cost about the same as three or four BASIC stamp computers. If you only
intend to produce a single item, you may wish to start out with the Parallax
Stamp computer.
I use a stamp-2 to prototype my own ideas before moving to the PIC for
a final product.
The stamp computer only requires
a simple programming cable and STAMP BASIC software to load your program
into its memory. The cable is simple to make, and the software is available
along with the manuals as a free
download from Parallax. The stamp computer is
the least expensive way to get started with microcontrollers.
With the use of the PBASIC
language on either the PIC or the STAMP, even people without much computer
experience can quickly learn to program these devices to accomplish projects
that would have been too complex only a few short years ago. Since I prefer
to use the PIC controller in my final design, I will focus on this device.
The STAMP however is quite similar, and the commands shown are identical.
The example program will run on both devices as written.
 |
A
PIC 16F84 Shown with the minimum hardware required.
( Of course it wouldn't be very useful... but
it would run a program. ) |
The diagram above shows the PIC
16F84 controller chip. This chip is one of the most common available. It
has FLASH memory, this means that it can be programmed over and over without
needing special lights or external memory. To program a PIC you need a
programming adapter and software to load your program. As you can see,
it does not take much to operate a PIC. The PIC requires 5 volts DC, a
device to provide a reference frequency and a resistor to keep the RESET
line from going low. The frequency for the chip can be a crystal, a ceramic
resonator, or even a capacitor and a resistor. The crystal provides the
most stable frequency, and is the most expensive. The resistor and capacitor
is the least expensive, but is also the least accurate.
The ceramic resonator is the
most used device because it offers high stability with low cost. The ceramic
resonator can be purchased with, or without internal capacitors. If you
purchase the ceramic resonator without capacitors you will have to provide
them to have the PIC to operate correctly. If you use the type with the
built in capacitors, simply connect the ceramic resonator as shown.
With
the addition of a single LED and a 330-ohm resistor you are ready to make
your first PBASIC program!
| Do you know what
binary numbers are? |
No, please teach me about Binary
Numbers.
 |
The PBASIC was written for the
Parallax stamp computer version 1. This simple computer has eight (8) pins
that can be used for input or output, also known as I/O. The I/O
is programmed by setting a BINARY NUMBER to determine
if the pin is ON or OFF. The PIC has two sets of I/O memory
called the "A" register and the "B" register. The PBASIC
defaults to the "B" register, which has 8 I/O lines. (8 pins for input/output)
The "A" register has 4 I/O lines for TTL logic output, and 1 line for current
sink logic.
For now we only care about
the 8 I/O lines in the "B" register.
Pins B0 through B7
are the I/O lines for the "B" register. The register numbers 0
through 7 references these pins. If we want to make the PIC have
a +5 volt (Logic 1) signal on the pin connected to B0, we
would use the following command.
HIGH 0
If we connect an LED to B0
(using a 330-ohm resistor to prevent damage to the PIC), we can
control the pin, and the hence the LED, by telling the PIC what to do with
this bit in the register. If the command above were programmed, the LED
would turn on and stay that way until we remove power. (Aren't you impressed?)
We can do more than just turn the LED on… we can turn it off too!
Let's examine this simple program to see how it works.
The
first line tells the PIC that we want to make the pin an output. Pin number
B0 on the chip is connected to register B0; this pin is made into an output.
The next line is a name used
for a reference point. If you skip ahead to the last line in the program,
you will see that we tell it to find the name BLINKY and run that
part of the program over again. (Forever… or until power is removed.)
The HIGH command turns
the pin "ON", and lights up the LED. The next command, PAUSE,
will tell the PIC to do nothing for 500 milliseconds… 1/2 of a second.
This will give us a chance to see the LED turn on.
The LOW command turns
the LED "OFF", and then the next
PAUSE command will make
the PIC wait another 1/2 second. So that we can see the LED go off.
The final command, GOTO,
tells the program to look for a label "BLINKY" then run the program
from that point. Since this is the first line in our program, everything
starts over again. (…and again…)
This short program will blink
the LED on and off until we remove power. You could experiment with changing
the values of the PAUSE command to get different effects. ( Not
to bad huh? ) |
SAMPLE PROGRAM
BLINKY.BAS
OUTPUT 0
BLINKY:
HIGH 0
PAUSE 500
LOW 0
PAUSE 500
GOTO BLINKY
|
 |
This is the PIC Programmer
It is manufactured by
MicroEngineering Labs |
|
Once you have written your
program in PBASIC, you will need to load it into the PIC internal memory.
When the program is loaded, the PIC is ready to be installed into your
circuit and tested.
Your program is converted to
a special file by the compiler. This file or HEX CODE is then ready to
be installed directly into the PIC memory.
To load the information into
the PIC, you will need a PIC programmer. The programmer is a device that
accepts your program from the computer and transfers it into the PIC. |
MicroEngineering
Labs sells a package that includes the PBASIC language, a PBASIC manual,
a PIC16F84-04/P, a prototyping board, and this programmer. Since I first
became interested in the PIC, they have introduced a newer, more powerful
version of BASIC for the PIC. Follow the link to find out more about it.
Loading the software is easy,
just install the PIC in the programmer's socket, run the programmer software
(EPIC), then select "PROGRAM" from the software menu. The device will ensure
that the PIC is blank, if not it will ask if you want it to be erased.
Once the PIC is erased, the
programmer will load your program into the PIC, and verify that it loaded
correctly. When this is complete, you may remove the PIC chip and install
it into your creation. If you want to make many copies of the same chip,
simply install another PIC in the socket and select "PROGRAM". If you have
any questions or comments, please e-mail me at ruthven@swbell.net
and I will be glad to assist you!
When I was using QBASIC to do most of my control
of the robots, I developed my own protocol for using the RS232C interface.
I called the first attempt VER1, then kept fixing bugs and correcting things
until it worked flawlessly. I had so many people ask for the BASIC program
that I mad a final version VER4.BAS that sends the
data, verifies that it got there, and if it is interrupted... will resend
the data. You may [ DOWNLOAD ] the file here!
The BASIC Stamp-2 has a matching program called VERSION4.BS2
which works with the above. You may [ DOWNLOAD
] the file here!