M a r k   C r o s b i e

You are in: PIC Development tools  Home  Resume  Linux  LEGO  Electronics  Movies  Mac OS X 

Development LED flasher Square wave generator Timed delay library Serial I/O Stepper motor driver Motor Mind B LCD library
Contact Mark at mark@mastincrosbie.com

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.

From the 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.

Linking libraries

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.

stepper.asm: stepper.c
delays.lib c2c  -ostepper.asm
delays.lib stepper.c 

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:
all: stepper.hex

# Assemble the stepper motor driver program from the asm source
stepper.hex: stepper.asm
	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.
delays.lib: delays.c
	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 UNIX.

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.

Drawing Schematics

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.

PIC Microcontroller Project Book
Lot's of great PIC project ideas!

Programming and Customizing the Pic Microcontroller

If you are learning to program microcontrollers then Myke's book is good start.
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design. If you want to learn why your circuit is not working, read this book and you'll know why.
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best practioners in the field: the MIT Artificial Intelligence Lab!
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers. A handy reference to have if you need a quick solution to a problem.

© 2002-2004 Mark Crosbie   shareright © 2002 Phlash