Propeller Manual

Table Of Contents
2: Spin Language Reference – CONSTANT
Propeller Manual v1.1 · Page 91
CONSTANT
Directive: Declare in-line constant expression to be completely resolved at compile time.
((PUB PRI))
CONSTANT (ConstantExpression )
Returns: Resolved value of constant expression.
ConstantExpression is the desired constant expression.
Explanation
The
CON block may be used to create constants from expressions that are referenced from
multiple places in code, but there are occasions when a constant expression is needed for
temporary, one-time purposes. The
CONSTANT directive is used to fully resolve a method’s in-
line, constant expression at compile time. Without the use of the
T
CONSTANT directive, a
method’s in-line expressions are always resolved at run time, even if the expression is always
a constant value.
Using CONSTANT
The
CONSTANT directive can create one-time-use constant expressions that save code space and
speed up run-time execution. Note the two examples below:
Example 1, using standard run-time expressions:
CON
X = 500
Y = 2500
PUB Blink
!outa[0]
waitcnt(X+200 + cnt) 'Standard run-time expression
!outa[0]
waitcnt((X+Y)/2 + cnt) 'Standard run-time expression
Example 2, same as above, but with CONSTANT directive around constant, run-time
expressions: