Delay Functions

Question: How do I implement software delays in my project?

 

Answer:

Use the attached source files delay.asm and delay.h to introduce software delays in a project. 

“delay.asm” has three functions that generate the delay. 

Delay50u:  This function generates 50uS delay for CPU speeds from 1.5MHz to 24MHz.  For speeds lower than 1.5MHz the delay is calculated by the following formula

Delay= (1.5MHz / (CPU Clock Frequency)) x 50uS

The function implements a simple loop where the Accumulator is loaded with a value and is decremented till zero.  The initial value written to the accumulator is extracted from a lookup table based on the CPU frequency, which is found from the OSC_CR0 register.

Delay50uTimes:  This function generates a delay in multiples of 50uS.  The function accepts the multiplication factor in Accumulator and calls the Delay50u function till the Accumulator becomes zero.  As the function accepts a maximum value of 255, the maximum possible delay is 255 x 50uS = 12.75mS.

The above two functions have been copied from the delay functions used by the LCD user module.

Delay10msTimes:  This function generates a delay in multiples of 10mS. The function accepts the multiplication factor in Accumulator. As the function accepts a maximum value of 255, the maximum possible delay is 255 x 10mS = 2.55S.

 

"delay.h” defines the function prototypes of the above functions.  Also, the functions are declared as fastcall16, so that the C compiler will know to pass the multiplication factor in accumulator when calling the Delay50uTimes/Delay10msTimes function.

 

To use the delay functions in a project, do the following:

 

1. Add the “delay.asm” and “delay.h” files to the project using “Project >> Add File” menu in the application editor.

2. The file “delay.asm” should now be listed in the “Source Files” folder in the Project folder view

3. The file “delay.h” should now be listed in the “Header Files” folder.

4. To call the delay functions from a C source file, include the “delay.h” header file in the C file.

5. Call the function using the function prototypes defined in the header

void Delay50u(void)

void Delay50uTimes(BYTE Multiple)

void Delay10msTimes(BYTE TenMsTimes)

6. In assembly, use “call Delay50u” to generate the 50uS delay.  To generate multiples of 50uS, load the accumulator with the multiplier and then use “call Delay50uTimes”. To Generate Multiples of 10 mS, load the accumulator with the multiplier and then use “call Delay10msTimes”.