On Fri, 12 Dec 2003 18:33:05 +0100, Matthias Weingart wrote:

Hi Bill,

I often do it the same way. However getElapsedSysTics() should check
for overflow of sysTICs, or you get the Windows98 feeling - complete
system freeze after 40 days ;-). May we can add it to the
example/tasker? (maybe I have some spare time the days until new
year).

===================
Matthias 
Actually the math works out without a check.

volatile uint16_t sysTICs;

// I always access sysTICs via this function because on some processors
// the access of sysTICs is interruptable by the ISR.  That isn't the case
// for the MSP430 unless sysTICs is uint32_t.  In that case interrupts 
// should be temporarily disabled while getting the value of sysTICs.
uint16_t getSysTICs(void)
  {
  return sysTICs;
  }

// This works because of the 16-bit masking of the result
uint16_t getElapsedSysTICs(uint16_t startTICs)
  {
  return (getSysTICs() - startTICs);
  }

void pause(uint16_t duration)
  {
  uint16_t startTICs = getSysTICs();

  while (getElapsedSysTICs(startTICs) < duration)
    swap();
  }


In the header file I usually do something like the following:
#include "config.h"     // get the project value for ACLK (in float)

// time durations for pause() and use with getElapsedSysTICs()
#define sysTICSperSEC   (ACLK / 0x10000L)
#define TWENTY_MS       (uint16_t)(( 0.02 * sysTICSperSEC) + .5)
#define THIRTY_MS       (uint16_t)(( 0.03 * sysTICSperSEC) + .5)
#define FIFTY_MS        (uint16_t)(( 0.05 * sysTICSperSEC) + .5)
#define HUNDRED_MS      (uint16_t)(( 0.10 * sysTICSperSEC) + .5)
#define ONE_FIFTY_MS    (uint16_t)(( 0.15 * sysTICSperSEC) + .5)
#define QUARTER_SEC     (uint16_t)(( 0.25 * sysTICSperSEC) + .5)
#define HALF_SEC        (uint16_t)(( 0.50 * sysTICSperSEC) + .5)
#define ONE_SEC         (uint16_t)(( 1.00 * sysTICSperSEC) + .5)
#define TWO_SEC         (uint16_t)(( 2.00 * sysTICSperSEC) + .5)
#define FIVE_SEC        (uint16_t)(( 5.00 * sysTICSperSEC) + .5)
#define TEN_SEC         (uint16_t)((10.00 * sysTICSperSEC) + .5)

The compiler will take care of the floating point math and leave you
with a selection of uint16_t timing values.

NOTE: the delay time can be up to one rollover period (1 TIC) short or
or arbitrarily longer depending upon how long the other tasks take
before calling swap().  Also don't delay too long or a tardy return
from swap() could cause the elapsed time check to wrap.

I've tried to attach a ZIP file to this posting.  Don't know if it
will make it.

Regards
-Bill Knight
R O SoftWare



<<attachment: sysTime.zip>>

Reply via email to