Propeller Manual

Table Of Contents
2: Spin Language Reference – CNT
Propeller Manual v1.1 · Page 73
CNT
Register: System Counter register.
((PUB PRI))
CNT
Returns: Current 32-bit System Counter value.
Explanation
The
CNT register contains the current value in the global 32-bit System Counter. The System
Counter serves as the central time reference for all cogs; it increments its 32-bit value once
every System Clock cycle.
Upon power-up/reset, the System Counter starts with an arbitrary value and counts upwards
from there, incrementing with every System Clock cycle. Since the System Counter is a
read-only resource, every cog can read it simultaneously and can use the returned value to
synchronize events, count cycles and measure time.
Using CNT
Read
CNT to get the current System Counter value. The actual value itself does not matter for
any particular purpose, but the difference in successive reads is very important. Most often,
the
CNT register is used to delay execution for a specific period or to synchronize an event to
the start of a window of time. The next examples use the
T
WAITCNTT instruction to achieve this.
In Spin code, when using
CNT inside of a WAITCNT command as shown above, make sure to
write the expression in the form “
T
offset + cnt” as opposed to “cnt + offset” and make
sure
offset is at least 381 to account for Spin Interpreter overhead and avoid unexpectedly
long delays. See the
WAITCNT command’s section on page for more
information.
Fixed Delays 218
waitcnt(3_000_000 + cnt) 'Wait for 3 million clock cycles
The above code is an example of a “fixed delay” It delays the cog’s execution for 3 million
system clock cycles (about ¼ second when running with the internal fast oscillator).
The next is an example of a “synchronized delay.” It notes the current count at one place and
performs an action (toggles a pin) every millisecond thereafter with accuracy as good as that
of the oscillator driving the Propeller chip.