I have an idea for a different solution.. (I just came up with this on my
afternoon break, so give me credit here 8-) ) I can't be bothered writing
actual code, so this is just a rough idea of the actual code... It's
basically a binary search for the loop value which causes a delay of one
"Tick"

/*BEGIN CODE*/
low = 1;
high = 1000000; // This should be high enough so that at least ONE tick
occurs during this many iterations of the loop

while (high > low) {
    x = ( high + low ) / 2;

    s = TimGetTicks();

    do {
        t = TimGetTicks();
    } while (t == s);

    // A tick *just* occurred. Now we count until the next.

    while (x) x--; // The delay loop

    if (t == TimGetTicks()) {
        low = x+1; // Less than a tick occurred during delay, so try again,
but wait longer.
    }
    else {
        high = x-1;// A tick occurred, so we're over. Try again, but wait
less time.
    }
}

d = ( x * SysTicksPerSecond() ) / 1000;
/*END CODE*/

NOTE: This code is *guaranteed* to be bug-ridden, it's just intended to
convey the rough idea I just had.

The result *should* be a value for d which causes the delay loop to wait one
milisecond.

Let me know what you think, or if I'm just a barfing idiot.. (Which is more
than likely).. 8-)

Regards,

Alan Ingleby
Systems Developer
ProfitLink Consulting Pty Ltd
309 Burwood Road
Hawthorn
Victoria 3122
Australia
"Bill Shaw" <[EMAIL PROTECTED]> wrote in message news:42184@palm-dev-forum...
>
> Needing a millisecond delay routine for my smart card application,  I
> searched the archives and found others asking for a similar routine.
> The reply was always negative;  doesn't exist,  can't do it.  Not being
> one to take no for an answer ;-) I thought about it for a few minutes
> and came up with this solution. The timing is not perfect,  and shorter
> delays will be less accurate due to interrupts,  but it's close enough
> for my needs.
>
> Enjoy.
>
> bs
>
>
>
/***************************************************************************
**
> *
> * File Name:  delay.c
> *             Millisecond delay routines.
> *
> * Author:     Bill Shaw
> *             [EMAIL PROTECTED]
> *             Westbrook Systems:
> *                Windows,  Mac OS,  Palm, PIC, and smart card development
> *
> * Date:       2/23/01
> *
>
****************************************************************************
***
> *
> *   This code is emailware.  If you find if useful,  send me an email
> *   and tell me about your app!
> *
> *   Also,  please leave my headers intact.  This is one form of
advertising
> *   I like to take advantage of when I can!
> *
>
****************************************************************************
***
> */
> #include <PalmOS.h>
>
> UInt32 startTicks;            // the time we started
> UInt32 loopsPerMs;            // # loops per millisecond
> UInt32 endTicks;              // the time we ended
>
>
/***************************************************************************
****
> *
> *  calcDelay - Calculates the delay value (loopsPerMs) for this Palm.
> *              Call once during app initialization.
> *              returns - nothing
> *
>
****************************************************************************
***
> */
> void calcDelay(void)
> {
>   UInt32 ticksPerSec;
>   UInt32 loopCount;
>
>   ticksPerSec = SysTicksPerSecond();     // get # ticks per second
>   startTicks = TimGetTicks();            // get start time
>
>   endTicks = startTicks + ticksPerSec;   // calc end time
>
>   // run the timing loop...
>   for (loopCount=0;  TimGetTicks() < endTicks; loopCount++) {
>      ticksPerSec++;
>   }
>
>   endTicks = TimGetTicks();              // get end time (for debug)
>   loopsPerMs = loopCount / 1000;         // calc # loops/ms
>
> }                                        // end of calcDelay()
>
>
/***************************************************************************
****
> *
> *  msDelay - millisecond delay
> *            UInt32 ms = # milliseconds to delay
> *            returns - nothing
> *
>
****************************************************************************
***
> */
> void msDelay(UInt32 ms)
> {
>   UInt32 loopCount, i;
>
>   loopCount = loopsPerMs * ms;              // calc # loops
>
>   startTicks = TimGetTicks();               // get start time (for debug)
>
>   // run the delay loop...
>   for (i=0; i<loopCount; i++) {
>      endTicks = TimGetTicks();
>   }
>
>   i = 0;                                    // just to break on
>
> }
>
> // end of file delay.c
>
>
>
>



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to