Thursday, March 02, 2006

PIC: EXAMPLE 1

EXAMPLE 1

Writing header and configuring I/O pins

The only purpose of this program is to turn on a few LED diodes on port B. It is nothing special. Anyway, use this example to study what a real program looks like. The figure below shows a connection scheme, while the program is on the next page.
Configuring I/O pins
When switching on, every other LED diode on the port B emits light. That is enough to indicate that the microcontroller is properly connected and operates normally.
This example gives the description of a correctly written header and a few initial directives. They represent a part of the program used in all programs described in this book. To skip repetitiveness, it will not be written in the following examples, but is considered to be at the beginning of every program (marked as a "Header").
Example 1 - Writing header and configuring I/O pins
The purpose of the header and initial directives is briefly described below.

Header:

The header is placed at the beginning of the program and gives basic information in the form of comments (name of the program, release date etc.). Don't be deluded into thinking that after a few months you will know what that program is about and why it is saved in your computer.

Initial directives:

list p=16f887
This directive defines processor to execute a program.
#include <p16f887.inc>
It enables the compiler to access the document p16f887.inc (If you have MPLAB installed, it is placed by default on C:\Program files\Microchip\MPASM Suite). Every SFR register contained in this document, as well as every bit, has its own name and address. If the program reads for example:
bsf INTCON, GIE
It means that the GIE bit of the INTCON register should be set. Instruction, as such, makes no sense to the compiler. It has to access the ".inc" document in order to know that the seventh bit of the SFR at the address 000B hex should be set.
Inside INC file
errorlevel -302
This is a "cosmetic" directive which disables the irritating message "Register in operand not in ..." to appear at the end of every compiling process. It is not necessary, but useful.
__config
This directive is used to include config word in the program upon compiling. It is not necessary because the same operation is performed by software for loading program into chip. However, do you have any idea which software will be used by the end user? What options will be set by default? You are the end user?! Do you know which program you will be using for MCU programming next year? Make life easier for yourself, take this directive as a necessary one and include it in your program.