Software Delay Functions in PSoC® Designer™ – KBA82879

Version: **

 

Question: How do you implement software delays in PSoC® Designer™ projects?

 

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 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 decremented until zero. The initial value written to the accumulator is extracted from a lookup table based on the CPU frequency, which is found in 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.

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 10 ms. The function accepts the multiplication factor in the accumulator. As 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 to pass the multiplication factor in the 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 the 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 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.