Propeller Manual

Table Of Contents
CONSTANT – Spin Language Reference
Page 92 · Propeller Manual v1.1
CON
X = 500
Y = 2500
PUB Blink
!outa[0]
waitcnt(constant(X+200) + cnt) 'exp w/compile & run-time parts
!outa[0]
waitcnt(constant((X+Y)/2) + cnt)'exp w/compile & run-time parts
The above two examples do exactly the same thing: their Blink methods toggle P0, wait for
X+200 cycles, toggle P0 again and wait for (X+Y)/2 cycles before returning. While the CON
block’s
X and Y symbols may need to be used in multiple places within the object, the WAITCNT
expressions used in each example’s
Blink method might only need to be used in that one
place. For this reason, it may not make sense to define additional constants in the
T
CON block
for things like
X+200 and (X+Y)/2. There is nothing wrong with putting the expressions right
in the run-time code, as in Example 1, but that entire expression is unfortunately evaluated at
run time, requiring extra time and code space.
The CONSTANT directive is perfect for this situation, because it completely resolves each one-
time-use constant expression to a single, static value, saving code space and speeding up
execution. In Example 1, the
Blink method consumes 33 bytes of code space while Example
2’s
Blink method, with the addition of the
T
CONSTANT directives, only requires 23 bytes of
space. Note that the “
+ cnt” portion of the expressions are not included within the CONSTANT
directive’s parentheses; this is because the value of
cnt is variable (cnt is the System Counter
register; see
CNT, page 73) so its value cannot be resolved at compile time.
If a constant needs to be used in more than one place in code, it is better to define it in the
CON
block so it is defined only once and the symbol representing it can be used multiple times.