Appendices:

Introduction

Assume that we want to have the for loop generate a one millisecond delay. The time to execute a software delay loop is proportional to the number of times the loop must be executed, as expressed in the following:

$$T_{\mathrm{DELAY}} = {\tt COUNTS\_PER\_MS} \times T_{\mathrm{EXECUTION\_TIME\_SINGLE\_LOOP}}$$

Given that the single loop execution time is constant, then the following relationship is also true:

$$T_{\mathrm{DELAY\_1}} / T_{\mathrm{DELAY\_2}} = {\tt COUNTS\_PER\_MS\_1} / {\tt COUNTS\_PER\_MS\_2}$$

where

$T_{\mathrm{DELAY\_1}}$ = Actual delay time measured by monitoring the test point for LEDA.

$T_{\mathrm{DELAY\_2}}$ = The desired loop delay.

COUNTS_PER_MS_1 = Initial or previously computed number of for-loop iterations to generate a one millisecond delay.

COUNTS_PER_MS_2 = Updated computed value of COUNTS_PER_MS.

Suppose that using a particular value for COUNTS_PER_MS_1 in our software delay function results in a delay, $T_{\mathrm{DELAY\_1}}$, that is not equal to our desired 1ms delay, $T_{\mathrm{DELAY\_2}}$. Rearranging Eq. (2) allows us to solve for an updated COUNTS_PER_MS needed to implement the desired delay, as shown below:

$${\tt COUNTS\_PER\_MS\_2} = (T_{\mathrm{DELAY\_2}} / T_{\mathrm{DELAY\_1}}) \times {\tt COUNTS\_PER\_MS\_1}$$

We can start with any initial guess for COUNTS_PER_MS_1, provided that it is in the numerical range of an unsigned integer. After running the program using our initial guess, we then take the measurement to determine the actual delay, $T_{\mathrm{DELAY\_1}}$. Using the ratio of the desired delay to the actual delay and multiplying by the value we used for COUNTS_PER_MS_1 allows us to compute the new value for COUNTS_PER_MS_2.

In theory, the new value computed for COUNTS_PER_MS_2 will result in the desired delay. However, the proportionality of Eq. (2) is only approximate and it may require a few iterations of solving Eq. (3) to achieve an accurate delay. The solution converges whenever the measured delay matches the desired delay. The following procedure can be used to determine the correct value of COUNTS_PER_MS to achieve a one millisecond loop delay.

Determining Delays

1. Generate Project 2 using the code provided by Listings 3 through 6. Be sure to have the common header files config_bits.h and chipKIT_PRO_MX7.h, as well as chipKIT_PRO_MX7.c, added to the project. Uncomment the sw_msDelay(mS) statement in the main function and set the initial value of mS to 1 so that a one-millisecond delay is implemented. Select an initial value for COUNTS_PER_MS_1. (For a one ms delay, a good number to try first is 5000.) Modify Project2.h to set the initial value for COUNTS_PER_MS equal to the value of COUNTS_PER_MS_1.
2. Compile and execute the C program for Project 2 with no breakpoints set.
1. Measure the high or low interval of the square wave generated at the LEDA test point using an oscilloscope or logic analyzer. (Remember to connect the oscilloscope common to the project board ground pin.) Record this measured period for the value of $T_{\mathrm{DELAY\_2}}$.
2. $$T_{\mathrm{DELAY\_2}} = 1 / (2 \times F_{\mathrm{MEASURED}})$$
3. An alternate method uses a frequency meter. Measure the frequency of the square wave observed with a frequency meter that is connected at the LEDA test point. (Remember to connect the meter common to the project board ground pin.) Record the measured frequency as FMEASURED. Compute the delay period from Eq. (4).
3. Setting $T_{\mathrm{DELAY\_1}}$ equal to 1ms and using the $T_{\mathrm{DELAY\_2}}$ found in Step 3a or Step 3b along with the value COUNTS_PER_MS_1, solve for COUNTS_PER_MS_2 using Eq. (3).
4. Modify Project2.h to set the value for COUNTS_PER_MS equal to COUNTS_PER_MS_2. If the TDELAY_2 is not equal to 1ms, assign the value of COUNTS_PER_MS_1 equal to the value of COUNTS_PER_MS_2 computed in Step 4, and repeat Steps 2 through 5 again. Continue repeating Steps 2 through 5 until the desired delay is achieved to the desired degree of accuracy possible, given the resolution of COUNTS_PER_MS is plus or minus one count.