M a r k   C r o s b i e

You are in: Delay Loop for PIC  Home  Resume  Linux  LEGO  Electronics  Movies  Mac OS X 


Details
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 Delay loop library

A very useful building block for most programs is a set of delay routines. This library provides three functions that can be called from any program that needs a delay:

All of these delay routines assume a 4Mhz PIC 16C84. You will have to scale the delay constants for a different clock speed. For example, for a 8Mhz clock crystal, you would divide all the delay constants in half.

Another point to note is that all these delay routines use a busy waiting loop: they will spin continually until the delay has been reached. This means that your program will not do anything else while the delays are occuring. Interrupts are still enabled, so the PIC will still respond to interrupts. These routines do not use the internal PIC timer.
void delay_us(char d)
Delay for d microseconds.
void delay_ms(char d)
Delay for d milliseconds.
void delay_s(char d)
Delay for d seconds


Compiling the delay routines

The delay code will be used as a library to other routines, so we will use the -lib option. The following snippet from the Makefile shows how the compiler is called:
 delays.lib: delays.c
        c2c -lib -odelays.lib delays.c
This will generate a file delays.lib which contains code that can be linked in by the compiler to the other pieces of code.

Delay source code

You can download the delay C code or if you prefer, the library file generated by the compiler is available.

The source code is a combination of C code and embedded assembler. The delay_us() and delay_ms() routines use assembler because we need the most precision in the inner delay loop. However, the delay_s() routine can call the delay_ms()) routine and we can tolerate the few microseconds of inaccuracy because of the loop overhead.

Remember - the PIC can only support 1 byte of data per register, so you can only supply a value between 0 or 255 to functions. That is why we loop 10 times and call delay_ms() with a parameter of 100, instead of calling delay_ms() with a parameter of 1000.

Using the Delay routines

There are many examples of using the delay routines in my other libraries. For example:
  • delay_s(2) delays for 2 seconds on a 4Mhz PIC
  • delay_ms(200) delays for 200 milliseconds, or .2 seconds
  • delay_us(100) delays for 100 microseconds, or .1 of a millisecond
In reality, you will find that the delay_ms and delay_us routines delays for a little longer than the parameter you pass. The extra delay is the overhead of calling the routine and returning from the function.

The delay_us() routine is not very accurate for delays that are short: the overhead of calling the function adds extra time, which on a 4Mhz PIC is a significant percentage of the delay required. For real accuracy, it is better to inline the code for a short delay (10 uS or less).


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

Programming and Customizing the Pic Microcontroller

If you are learning to program microcontrollers then Myke's book is good start.
cover
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.
cover
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!
cover
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