Generating Delay
Written by Amol Shah, on Aug-2008
Views 846

Here is a code for generating software based delay i.e. loading a specific value in a register and decrementing it till it becomes zero. One of the most important drawbacks of delay subroutine is that the delay changes as you change the crystal and then again you have to calculate the new values.

Here is a code where you just have to load the crystal value and the internal clock divider (12, 6, 2, and 1) and the code itself calculates the exact clock cycles required for 1 MS delay.

 

DELAY.H

#ifndef __DELAY_H

#define __DELAY_H

 

#define XTAL                      11.059200                            //Crystal frequency

#define XDIVIDER            12.0                                        //Divider

#define delay_1ms         164 * (XTAL/XDIVIDER)

 

void delay(unsigned char j);

void delay_ms(unsigned int i);

#endif

 

DELAY.C

void delay(unsigned char j)

{

                                for (;j!=0x00;j--)

                                {

                                }

}

 

void delay_ms(unsigned int i)

{

 

        for(;i!=0x00;i--)

                                                {

               delay(delay_1ms);

                                                }

}

 

The function delay_ms (unsigned int i) generates precise delay in MS.

 

NOTE: Change the XTAL value and the XDIVIDER according to the crystal and controller used respectively.


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Quote this article in website Favoured Print Send to friend Related articles Save this to del.icio.us

Users' Comments (0) RSS feed comment

No comment posted

Add your comment



mXcomment 1.0.5 © 2007-2009 - visualclinic.fr
License Creative Commons - Some rights reserved
 
< Prev
Home arrow Code Library arrow Generating Delay