Getting Started

---------------------------------------------------------------------------

For new users of the PIC C compiler, the following section provides a
step-by-step guide to getting your first program running in a target
system. You'll need a working PIC system, with RAM and ROM, and some means
of programming the ROM. And, of course, you'll need to have installed the
compiler as described in the previous chapter.

You will find a complete guide to using HPDPIC and PICC in the chapters
Using HPDPIC and PICC Command Line Compiler Driver.

One thing should be made clear; with embedded programming there really is
no such thing as a "quick start". There are several variables, e.g. the
hardware, memory, I/O devices and the software, all of which must be
exactly right or the program will simply not work. There are no error
messages when your embedded program crashes - it is a black box. Be
prepared to check everything carefully, and if possible start with known
working hardware. Debugging hardware and software at the same time squares
the degree of difficulty.

4.1 A Sample Program

#include        <pic.h>
/*
 *      Demo program - flashes LEDs on
 *      Port B, responds to switch press
 *      on RA1. Usable on PICDEM board.
 */
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit button @ PORTBIT(PORTA, 1);
main(void)
{
        unsigned i;
        unsigned char j;
        TRISB = 0;                      /* all bits output */
        j = 0;
        for(;;) {
                PORTB = 0x00;                   /* turn all on */
                for(i = 16000 ; --i ;)
                        continue;
                PORTB = ~j;                     /* output value of j */
                for(i = 16000 ; --i ;)
                        continue;
                if(button == 0)                 /* if switch pressed, increment */
                        j++;
        }
}

The small sample program shown is written for the PIC16C84 processor
running on the Microchip PICDEM 1 demo board which has 8 LEDs attached to
Port B. It loops forever flashing the LEDs. If your hardware is different,
as it almost certainly is, you should write a similar program tailored for
your particular hardware. Flashing LEDs is however a good place to start,
as it provides a visual indication of program function. If you don't have
LEDs attached, then you could monitor an output port line with an
oscilloscope or logic probe. Alternatively, you could even monitor address
lines with an oscilloscope or logic analyser. The idea is to be able to
determine that the program is running correctly using a minimum of
resources, so as to remove as many variables as possible from the problem.

Once the first program is running, it is easier to progress from that point
than to try and run a complex program from the beginning. To get this
program running, you will need to compile it, either using HPDPIC (the
integrated development environment) or PICC (the command line compiler
driver).

4.2 Using HPDPIC

To enter this program, simply follow these steps:

   * Start HPDPIC by typing hpdpic, then press enter. If you have installed
     HPDPIC properly, it will be in your search path. You should have on
     screen a menu bar, a large edit window, and a smaller message window.

   * Start typing the program text in the edit window. The editor command
     keys allow either the standard PC keys (arrow keys etc.) or
     WordStar-compatible keystrokes.

   * After typing the complete program (with any modifications necessary
     for your hardware) save the file by pressing alt-s. A dialog box will
     appear asking you to enter a name to save the file to. Type the name
     sample.c and press return. The file will be saved to disk.

   * Press alt-p to open the Options menu. Use either the mouse or arrow
     keys to select the item Select processor.... This will open a dialog
     box enabling you to select the processor you are using. Press return
     to exit the dialog box.

   * Press alt-p again and select the Output file type... item. This will
     open a dialog box allowing you to select an output format for your
     executable file. Choose a file format compatible with your programming
     process. Bytecraft COD and Intel HEX are the most commonly used
     formats. Press return to exit the dialog box.

   * Press F3 to compile the program. If you haven't saved the edit file,
     you will be prompted to do so now. Save it as sample.c. HPDPIC will
     compile the program. Any errors found will stop the compilation, and
     the errors will be listed in a window that appears at the bottom of
     the screen. The cursor in the edit window will be positioned on the
     error line. Correct the error, then press F3 again. HPD remembers the
     processor type and output type you selected previously, so you will
     not have to re-enter this information.

     On completion of compilation, an output file called sample.cod (or
     sample.hex for Intel hex) will be left in the current directory.

   * Exit HPDPIC by pressing alt-q.

4.3 Using PICC

To use PICC to compile your sample program, you will first need to create a
file containing the program. You can use whatever text editor you are
familiar with, as long as it can create a plain ASCII file. The MS-DOS edit
command is satisfactory. Save the file as sample.c. To run PICC, type:

picc sample.c

If you are compiling for the 16C84, for example, you should add the -16C84
option, e.g.

picc -16c84 sample.c

This generates code for the 16C84 instruction set.

If you have correctly entered the sample program, no error messages should
result. If you do get error messages, edit the program to correct the
errors, and recompile with PICC as before. Here is sample output from PICC:

C:\HT-PIC\SAMPLES>picc sample.c
HI-TECH C COMPILER (Microchip PIC) V7.70
Copyright (C) 1984-1997 HI-TECH SOFTWARE
Serial no: CPIC-12345; Licensed to:
Jeremy Bennett, HI-TECH Software
Select chip type from one of:
12C508 12C509 14000 16C52 16C54 16C54A 16C55 16C554 16C556 ...
Type: 16c84
Memory Usage Map:
Program ROM:  $0000 - $002A   $002B (43) bytes
Internal RAM: $000C - $000E   $0003 (3) bytes

Points to note:

   * The processor was not specified, so PICC prompted for the processor.

   * PICC has printed a short summary of memory usage.

4.3.1 Output File Format Selection

The compiler supports various output file formats. The two most commonly
used for input into an EPROM programmer are Intel HEX and binary. The
default is both Bytecraft COD and Intel HEX, but can be changed with one of
the following options to PICC:

     -motorola Produce Motorola S1/S9 HEX file

     -bin Produce a binary output file

     -ubrof Produce an IAR Ubrof file

     -aahex Produce HEX records with symbols for American Automation
     emulators

     -tek Produce Tektronix HEX file output

4.4 Running your program

Once you have compiled the program, you will have a file called sample.cod
and a file called sample.hex in the current directory. How you get this
into your hardware will vary depending on just what you have to work with,
but generally speaking you will need an EPROM/EEPROM programmer to allow
you to get the program into the memory of your target system The exact
procedures for doing so are beyond the scope of this manual.

---------------------------------------------------------------------------

hitech@htsoft.com
Copyright © 1997, HI-TECH Software. All rights reserved.
