How do I implement software delays in my project?
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 a 50-µs delay for CPU speeds from 1.5 MHz to 24 MHz. For speeds lower than 1.5 MHz the delay is calculated by the following formula:
Delay= (1.5 MHz / (CPU Clock Frequency)) x 50 µs
The function implements a simple loop where the accumulator is loaded with a value and is decrement until 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 50 µs. The function accepts the multiplication factor in the accumulator and calls the Delay50u function until the accumulator becomes zero. As the function accepts a maximum value of 255, the maximum possible delay is 255 x 50 µs = 12.75 ms.
These two functions have been copied from the delay functions used by the LCD user module.
Delay10msTimes: This function generates a delay in multiples of 10 ms. The function accepts the multiplication factor in the accumulator. Because the function accepts a maximum value of 255, the maximum possible delay is 255 x 10 ms = 2.55 s.
"delay.h” defines the function prototypes of the above functions. Also, the functions are declared as fastcall16, so that the C compiler will know that it should pass the multiplication factor in accumulator when calling the Delay50uTimes/Delay10msTimes function.
To use the delay functions in a project, do the following:
- Add the “delay.asm” and “delay.h” files to the project using the “Project > Add File” menu in the application editor.
The file “delay.asm” should now be listed in the “Source Files” folder in the Project folder view The file “delay.h” should now be listed in the “Header Files” folder.
- To call the delay functions from a C source file, include the “delay.h” header file in the C file.
- Call the function using the function prototypes defined in the header:
void Delay50uTimes(BYTE Multiple)
void Delay10msTimes(BYTE TenMsTimes)
- In assembly, use “call Delay50u” to generate the 50-µs delay. To generate multiples of 50 µs, 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”.