PROGRAMMING PIC MICROCONTROLLERS IN C:DELAYS IN C PROGRAMS

DELAYS IN C PROGRAMS

There are many real-time applications where the microcontroller is too fast and we need to slow it down to see, for example, the flashing of an LED. The PICC Lite compiler offers two types of delay functions: a microsecond delay function, and a millisecond delay function. The file <delay.c> must be included at the beginning of a program before these delay functions can be used. The function DelayUs(x ) is used to create delays in the range 1 to 255 μs. For example, the following function call generates a 200 μs delay:

DelayUs(200);

Similarly, function DelayMs(x ) is used to create delays in the range 1 to 255 ms. The following function call creates a 10 ms delay:

DelayMs(10);

Longer delays can be created by using the above delay functions in loops. These delay functions are by default based on the assumption that the microcontroller operates with a 4 MHz clock frequency. If the clock rate is different, then the new clock rate must be specified at the beginning of the program. For example, if the clock rate is 2 MHz, then the following lines must be included at the beginning of the program:

clip_image001#undef XTAL FREQ

clip_image002#define XTAL FREQ 2MHZ

Leave a comment

Your email address will not be published. Required fields are marked *