The system cycle counter is incremented for each instruction executed. It functions in normal code and during interrupts. Since it operates at the clock frequency of the device, it can be used for accurately measuring small periods of time.
// overview of System tick functions uint32_t now = System.ticks(); // for converting an the unknown system tick frequency into microseconds uint32_t scale = System.ticksPerMicrosecond(); // delay a given number of ticks. System.ticksDelay(10);
Returns the current value of the system tick count. One tick corresponds to one cpu cycle.
// measure a precise time whens something start uint32_t ticks = System.ticks();
Retrieves the number of ticks per microsecond for this device. This is useful when converting between a number of ticks and time in microseconds.
uint32_t start = System.ticks(); startTheFrobnicator(); uint32_t end = System.ticks(); uint32_t duration = (end-start)/System.ticksPerMicrosecond(); Log.info("The frobnicator took %d microseconds to start", duration);
Pause execution a given number of ticks. This can be used to implement precise delays.
// delay 10 ticks. How long this is actually depends upon the clock speed of the // device. System.ticksDelay(10); // to delay for 3 microseconds on any device: System.ticksDelay(3*System.ticksPerMicrosecond());
The system code has been written such that the compiler can compute the number of ticks to delay at compile time and inline the function calls, reducing overhead to a minimum.