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