How to implement delay function in Keil ARM MDK that waits given time in microseconds that can be recompiled to any reasonable clock speed defined in Keil project settings ? And all of this without using timers.
|
|
The best approach IMHO is to have one hardware timer which is never written to but simply free-runs. This will allow one to easily emulate an arbitrary of poll-able timers for durations up to half the length of the hardware timer. To "start" a timer, simply compute the value of the hardware timer when it should expire. To see if a timer has expired yet, subtract the expected expiration time from the current time (regarding both quantities as unsigned), and see if the (unsigned) result exceeds half the maximum timer value. If so, the timer has not expired. Alternatively, if one wants a timer for measuring time since a certain event occurred, one can simply latch the timer value when the event occurred, and later compute the difference between the latched value and the present time. |
|||||
|
|
You will have to determine the length of a NOP, then using #Defines make it so that based on clock speed it inserts the correct number. Specifically you want to know the exact number of clock cycles it takes to get 1 loop to be completed of the style:
By making the #define for number of loops required reconfigure itself to be automatic based on the requested delay and the clock speed you are currently running at. This can all be wrapped up in a macro. |
|||||||||||
|
where Use a test function to figure out what your LOOP_DELAY constants need to be for each clock frequency:
|
|||
