09-08-2012, 01:05 PM
PIC16F87X Tutorial by Example
PIC16F87X_tutorial_sample.pdf (Size: 50 KB / Downloads: 46)
Introduction
This is a "tutorial by example" developed for those who have purchased our Serial MPLAB PIC16F87X
Development Package. All of the C routines are in a separate zipped file. This is an ongoing project and I will
add to this and send an updated copy in about two weeks.
Although all of this material is copyright protected, feel free to use the material for your personal use or to use the
routines in developing products. But, please do not make this narrative or the routines public.
PIC16F87X Data Sheet
It is strongly suggested that you download the 200 page "data sheet" for the PIC16F877 from the Microchip web
site. I usually print out these manuals and take them to a copy center to have them make a back-to-back copy and
bind it in some manner.
Use of the CCS PCM Compiler
All routines in this discussion were developed for the CCS PCM compiler ($99.00). I have used many C
compilers and find that I keep returning to this inexpensive compiler. All routines were tested and debugged using
the same hardware you have as detailed in Figures 1 - 6.
Special Function Register and Bits
In using the CCS compiler, I avoid the blind use of the various built-in functions provided by CCS; e.g., #use
RS232, #use I2C, etc as I have no idea as to how these are implemented and what PIC resources are used. One
need only visit the CCS User Exchange to see the confusion.
Rather, I use a header file (defs_877.h) which defines each special function register (SFR) byte and each bit
within these and then use the "data sheet" to develop my own utilities. This approach is close to assembly
language programming without the aggravation of keeping track of which SFR contains each bit and keeping
track of the register banks. The defs_877.h file was prepared from the register file map and special function
register summary in Section 2 of the "data sheet".
One exception to avoiding blindly using the CCS #use routines is I do use the #int feature to implement interrupt
service routines.
Program FLASH2.C.
This routine is precisely the same as FLASH1.C except that the timing routines have been declared in lcd_out.h
and they are implemented in lcd_out.c.
The CCS compiler does not support the ability to compile each of several modules to .obj files and then link these
to a single executable (.hex) file. However, you can put routines that are commonly used and thoroughly
debugged in a separate file and simply #include the files at the appropriate point.
File lcd_out.c is a collection of the two timing routines plus a number of other routines to permit you to display
text on the LCD panel. However, the CCS compiler will not compile a routine, which is not used, and thus no
program memory is wasted. Surprisingly, this is not true of all compilers.
Program DIAL_1.C
This program illustrates a telephone dialer that might be used in a remote monitor or alarm.
When the pushbutton on PORTB.0 goes to ground, the processor operates an LED (dial pulse relay) on PORTD.4.
Following a brief delay to assure dial tone is probably present, the processor dials the telephone number, waits for
a party to answer and then sends the quantity in the form of zips (or beeps) using a speaker on PORTD.0. For
example, the quantity 103 is sent as one beep, followed by ten beeps, followed by three beeps. This is repeated
three times and the processor then hangs up.
Using the LCD.
A 20X4 DMC20434 LCD along with a 74HC595 shift register was included with the full development package
(Figures #5 and #6). The software routines to support this circuitry are contained in lcd_out.c. Note that this uses
Port E, bits 0, 1 and 2. The idea in using these bits was that aside from A/D converter inputs, they serve no
function other than general purpose IO.