KEMBAR78
Time Delay in PIC | PDF | Electrical Engineering | Software Engineering
100% found this document useful (2 votes)
252 views2 pages

Time Delay in PIC

The document discusses using delay functions from the delays.h header file to implement precise time delays in PIC code. It explains that delays.h contains functions for delays from 1 clock cycle up to 10,000 clock cycles. For delays longer than seconds, timers should be used instead of delay functions. An example is given of calculating the number of Delay10KTCYx(0) function calls needed to achieve a 1 hour delay, which would be approximately 2813 calls. The accuracy of such a long delay using delay functions is estimated to be within 2 seconds.

Uploaded by

Mervin Rodrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
252 views2 pages

Time Delay in PIC

The document discusses using delay functions from the delays.h header file to implement precise time delays in PIC code. It explains that delays.h contains functions for delays from 1 clock cycle up to 10,000 clock cycles. For delays longer than seconds, timers should be used instead of delay functions. An example is given of calculating the number of Delay10KTCYx(0) function calls needed to achieve a 1 hour delay, which would be approximately 2813 calls. The accuracy of such a long delay using delay functions is estimated to be within 2 seconds.

Uploaded by

Mervin Rodrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Time Delay in PIC

Implementing precise time delays in code is pretty easy when you use inbuilt
functions from “delays.h” from C18 libraries. It is usually employed for delays up
to seconds. For delays in the order of minutes or hours, it is not suggested to use
delay functions. Moreover your controller will not be performing any other
operation during the waiting time; the other option is you may use the timers in
PIC. I haven‟t tried to have delays in the order of minutes or hours, so I can‟t
assure you it will work for sure, anyways the method of using delay functions will
be discussed and it will be extended to have a delay of 1 Hour, it may not be very
precise, an error up to 2 seconds may be expected.

Let your PIC operate at „X‟ frequency.

Then by architecture, each clock cycle lasts for (4/X) seconds. Hereafter one clock
cycle will be denoted as 1TCY. Compute your PIC‟s TCY based on the frequency
it operates in. The header file “delays.h” has the following functions.

 Delay1TCY()
 Delay10TCY()
 Delay1TCYx()
 Delay10TCYx()
 Delay100TCYx()
 Delay1KTCYx()
 Delay10KTCYx()

The functions with „x‟ in the name can have an unsigned number (0-255) passed as
a function argument. For instance, Delay100TCYx (160) would produce a delay of
160*100 TCY. Similarly, 1KTCY means 1,000 TCY. It should be emphasized
that passing 0 to the above function does not produce an illogical zero time delay,
instead it produces delay of 25600 TCY. This is by design; the programmers of the
C18 libraries wrote it that way hence no more questions on that are encouraged.
You can find all these information, in fact some more on “delays.h” file.

From the above discussion, it may be noted that maximum time delay can be
obtained in a single line of code by using Delay10KTCYx (0) which corresponds
to a delay of 2,560,000TCY.
Example:

Let us make a code snippet which produces a delay of 1 Hour. Let me assume my
PIC operates at 8MHz.

1TCY = (4/ (8*1000000)) = 0.0000005 s = 0.5 us.

Delay10KTCYx (0) will produce delay of (2560000*0.0000005) s = 1.28 s

No. of Delay10KTCYx (0) required for 1 Hour delay


= (60*60/1.28) = 2812.5.
Hence approximately 2813 times you have to use that function! :-)
The following lines of code might produce a delay of 1 Hour I guess.
for (i=0; i< 2813; i++)

Delay10KTCYx (0);

The above function may not produce an exact delay of 1 hour; error may be 2s at
the max.

Note:

The frequency of your PIC is known from the crystal you use in your board, ensure
that configuration bits are set to use external clock as follows.

Go to Window -> PIC Memory views -> Configuration Bits.

In the FOSC field, set it as HS for using external crystal oscillator of known
frequency.

You might also like