PIC Development Techniques
I have only been playing with PIC Microcontrollers for a short while,
but already I'm impressed with their power and flexibility. Why build
discrete logic anymore when a few bytes of PIC assembler can solve it
all for you??
My philosophy is to build reusable modular library components that
anyone can take and plug into their projects. To this end, I have
chosen C as the language of choice to implement most of my
projects. The C code is compiled into Microchip assembler, and then a
.hex file is generated using an assembler.
Why choose C
- The compiler handles placement of variables in memory.
- Writing modular code is a lot easier.
- Building libraries is possible.
Why avoid C?
- You may not know how to program C. I have included the generated
.asm and .hex files in all my examples.
- Generates slower code. True, but you can hand optimise the
speed sensitive parts.
- Generates larger .hex files. Definitely true, but I feel the
advantages of modular design offset this.
- Where do I get a compiler? Read on for the answer!
PIC project development tools
Being a commited Linux user, I
wanted to use tools that did not involve booting into Windows every
time I wanted to tweak my PIC designs. Luckily there are freely
available versions of the most useful PIC tools available.
Compiling C programs for the PIC
I use the C2C
compiler to compile the C libraries.
To generate libraries we supply the -lib command line
option. A library is a set of routines that can be called from other
pieces of code. The -lib option generates position
independent code, and i defines how many variables are needed by the
library routines. When the final assembly file is generated by the
compiler, all the variables are placed in the registers.
sample Makefile above, the line:
delays.lib: delays.c c2c
-lib -odelays.lib delays.c will generate the library
delays.lib from the C source file delays.c.
The C compiler will link the .lib files specified on the command
line. From the sample Makefile above, the following snippet will link
the delayslib lbrary with the stepper.c source and generate the
stepper.asm assembly output.
delays.lib c2c -ostepper.asm
Using a Makefile with PIC code
This section is for people who are not familiar with Makefiles in
UNIX. If you have worked with a Makefile before, you can skip on.
The steps involved in working with libraries are as follows
- Compile the library C code to a .lib file.
- Compile your code and link it with the library .lib file. This will
generate a .asm output file.
- Assemble the .asm file using the assembler to produce a .hex file.
- Download the .hex file into the PIC.
All that work for a simple program?? It seems like a lot of work, but
in fact it isn't, thanks to Makefiles. A Makefile defines the
dependencies between modules in a program. Once you have written the
Makefile, you type make and any changed source files are
recompiled nd relinked. It is very similar to project files in
Borland C and other compilers.
Here is a sample Makefile:
# Assemble the stepper motor driver program from the asm source
gpasm -r dec -p 16c84 -t stepper.asm
# Generate the asm source file from the C source and the delay library.
stepper.asm: stepper.c delays.lib
c2c -ostepper.asm delays.lib stepper.c
# Generate the delay library from the C source file.
c2c -lib -odelays.lib delays.c
# Clean up the directory
rm -f *.hex *.lib core *.asm *.lst
Programming the PIC from Linux
I have tried a few PIC programming tools under Linux, but eventually I
ended up using the standard Microchip EPIC programmer running uner the
DOSEMU DOS emulator. The
programmer uses the parallel port and. One caveat: EPIC expects the
.hex files you generate to be terminated with DOS end-of-line
terminators. This involves printing a "
" instead of a "
" as in
I wrote a simple patch for gpasm
that will generate DOS newlines in the .hex file. If you supply the -t
command line argument the .hex file is terminated with carriage return
characters, and the EPIC programmer can read it.
Another useful tool is the Eagle layout and autorouter from CadSoft USA. It provides a
library of parts and board elements, and provides both schematic
capture and board layout. And best of all, it's free and runs
on Linux! The free version will layout boards with two sides (top and
bottom) which is usually sufficient for hobbyist work.