NXT Functions Timing

From ROBOTC API Guide
Jump to: navigation, search


An internal 32-bit clock is maintained by the NXT firmware. It counts in units of 1-millisecond. Four timers (T1, T2, T3, and T4) are built using this timing capability. These four timers can be individually reset to zero within a program. Theses timers are useful for measuring elapsed time of events.


16-bit integers are the default variable type for ROBOTC on the NXT. All of the timing functions operate with 16-bit signed variables. This does mean that caution should be exercised in programming to avoid overflow of timer values. A 16-bit signed variable has positive values in the range 0 to 32,767 and programs should periodically reset any timers that they use to prevent overflow.


Color Key
Function:
Variable:


ClearTimer

void ClearTimer(TTimers theTimer)
(void) Timers start counting as soon as the NXT is powered on. A user's program should reset a timer before using it, so use this function to reset the value of the specified timer to zero.
Parameter Explanation Data Type
theTimer The timer to clear. TTimers


Timers:

  • T1
  • T2
  • T3
  • T4
ClearTimer(T1);  // resets the value of Timer "T1" back to zero seconds


nClockMinutes

word nClockMinutes
(word) This read/write variable provides access to the NXT clock described above. The value ranges from 0 to 1439 before it wraps around back to 0. Note: there are 1440 minutes in 24 hours.
int varClock;              // creates a variable
varClock = nClockMinutes;  // stores the current value of the NXT Clock to a variable


nPgmTime

const long nPgmTime
(long) This variable contains the value of the lower 16-bits of the internal 1-msec clock. This variable is reset when user program first starts running. This clock does not increment when the program is in a debugger "suspended" state which is useful during single step debugging as the clock does not increment.
int varPgmTime;         // creates a variable
varPgmTime = nPgmTime;  // stores the current value of nPgmTime to a variable


nSysTime

const long nSysTime
(long) This variable contains the value of the lower 16-bits of the internal 1-msec clock. This variable is reset when NXT is first powered on.
int varSysTime;         // creates a variable
varSysTime = nSysTime;  // stores the current value of nSysTime to a variable


time1

long time1[TTimers timer]
(long) This array holds the current value of the respective timers. Each of the timer values can be retrieved in units of 1, 10 and 100 milliseconds depending on which array is used. For example, time1[T1] retrieves the value of timer T1 in units of 1-msec and time10[T1] retrieves the value using a 10-msec tick. And time100[T1] retrieves the value using 100-msec tick. Note that the arrays are “linked”. Setting time1[T1] = 0; will also reset the value of time10[T1] and time100[T1]. The value returned is a signed integer, so each array will meet its upper bounds at a value of 32,768 ticks.
Parameter Explanation Data Type
timer A timer to use. TTimers


Timers:

  • T1
  • T2
  • T3
  • T4
int valTime1, valTime10, valTime100;  // create three integers to read the value of the timer
 
valTime1 = time1[T1];      //Gets the value of Timer T1 in 1ms increments and stores it in a variable
valTime10 = time10[T1];    //Gets the value of Timer T1 in 10ms increments and stores it in a variable
valTime100 = time100[T1];  //Gets the value of Timer T1 in 100ms increments and stores it in a variable


time10

long time10[TTimers timer]
(long) This array holds the current value of the respective timers. Each of the timer values can be retrieved in units of 1, 10 and 100 milliseconds depending on which array is used. For example, time1[T1] retrieves the value of timer T1 in units of 1-msec and time10[T1] retrieves the value using a 10-msec tick. And time100[T1] retrieves the value using 100-msec tick. Note that the arrays are “linked”. Setting time1[T1] = 0; will also reset the value of time10[T1] and time100[T1]. The value returned is a signed integer, so each array will meet its upper bounds at a value of 32,768 ticks.
Parameter Explanation Data Type
timer A timer to use. TTimers


Timers:

  • T1
  • T2
  • T3
  • T4
int valTime1, valTime10, valTime100;  // create three integers to read the value of the timer
 
valTime1 = time1[T1];      //Gets the value of Timer T1 in 1ms increments and stores it in a variable
valTime10 = time10[T1];    //Gets the value of Timer T1 in 10ms increments and stores it in a variable
valTime100 = time100[T1];  //Gets the value of Timer T1 in 100ms increments and stores it in a variable


time100

long time100[TTimers timer]
(long) This array holds the current value of the respective timers. Each of the timer values can be retrieved in units of 1, 10 and 100 milliseconds depending on which array is used. For example, time1[T1] retrieves the value of timer T1 in units of 1-msec and time10[T1] retrieves the value using a 10-msec tick. And time100[T1] retrieves the value using 100-msec tick. Note that the arrays are “linked”. Setting time1[T1] = 0; will also reset the value of time10[T1] and time100[T1]. The value returned is a signed integer, so each array will meet its upper bounds at a value of 32,768 ticks.
Parameter Explanation Data Type
timer A timer to use. TTimers


Timers:

  • T1
  • T2
  • T3
  • T4
int valTime1, valTime10, valTime100;  // create three integers to read the value of the timer
 
valTime1 = time1[T1];      //Gets the value of Timer T1 in 1ms increments and stores it in a variable
valTime10 = time10[T1];    //Gets the value of Timer T1 in 10ms increments and stores it in a variable
valTime100 = time100[T1];  //Gets the value of Timer T1 in 100ms increments and stores it in a variable


wait1Msec

void wait1Msec(const long nMSec)
(void) Program execution will wait for the specified number of clock units. Units are in 1-millisecond counts. The maximum interval that can be specified is 32.767 seconds. An alternative, and far less efficient, method to perform a wait is to continually execute a tight code loop looking to see if a timer has reached the desired interval. It is best to use the wait function to insert a programmed delay in a program because tasks that are waiting do not consume any CPU cycles. This makes the most number of CPU cycles available for other tasks.
Parameter Explanation Data Type
nMSec The amount of milliseconds to wait. long
wait1Msec(1000);   // the program will wait for 1 second before moving on (1ms * 1000)


wait10Msec

void wait10Msec(const int nTenMSec)
(void) Program execution will wait for the specified number of clock units. Units are in 10-millisecond counts. The maximum interval that can be specified is 327.67 seconds. An alternative, and far less efficient, method to perform a wait is to continually execute a tight code loop looking to see if a timer has reached the desired interval. It is best to use the wait function to insert a programmed delay in a program because tasks that are waiting do not consume any CPU cycles. This makes the most number of CPU cycles available for other tasks.
Parameter Explanation Data Type
nTenMSec The amount of milliseconds to wait, measured in 10-millisecond counts. int
wait10Msec(1000);  // the program will wait for 10 seconds before moving on (10ms * 1000)