|"This is the delay subroutines i use in my PIC projects. It was written for the PIC16 family but should be compatible with the 18F instruction set as well.|
The simplest way to produce delay is to write a subroutine that waste instructions cycles for the desired amount of time. This method should not be used for application that require a high level of precision since the delay subroutine may be halted by interrupts. The delay is offset by the time it takes to execute the interrupt service routine. For short delays however, this should not be a problem.
How many cycles are required
Let's say that the processor clock runs at 8 MHZ. It takes 4 clock pulse to execute one instruction cycle. At 8 MHZ, the processor is able to execute 2 millions instructions per seconds. It also means that each instructions takes 0.0000005 seconds (0.5 micro seconds) to execute. To produce a 50 milliseconds delay, you then need to execute 100,000 instruction cycles.
Here is the formula to find out the number of instruction required :
i_time = 1 / (fosc / 4)
0.0000005 = 1 / (8000000 / 4)
n = time / i_time
100000 = 0.05 / 0.0000005
The delay subroutines
There are 4 delay subroutines :"