CONSTANT

Directive: Declare in-line constant expression to be completely resolved at compile time.

((PUBPRI))
   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 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:

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 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 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) 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.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.