There are two distinct forms of constants in XCSB
- a simple named number (normally refered to as a manifest constant)
- a constant data table (sometimes refered to as lookup or parameter data)
MANIFEST CONSTANT
A manifest constant is often used to associated a meaning with a number (giving a descriptive name to a number).e.g. const max_len = 100 for j=0 while j<max_len step j=j+1 doBy giving a constant a name it is possible to easily distinguish all occurances of it from all other constants even when they happen to have the same value. Also by using a symbolic name for a constant we can simplify and limit changes to a program's source code to one easily identified location.e.g. const offset = 10 for j=0 while j<max_len step j=j+1 do A[j] = (B[j+offset] + C[j+offset]) / D[j+offset] + 10 donechanging offset to 11 is equivalent to changingA[j] = (B[j+10] + C[j+10]) / D[j+10] + 10toA[j] = (B[j+11] + C[j+11]) / D[j+11] + 10without the risk of overlooking one occurence of 10 or accidently changing one that should not be changed.CONSTANT DATA TABLE
A constant data table is used to store data that will not change after it has been created. An example might be an ideal height to weight table which stores the ideal weight for a person of a given height. This data would not be changed by the running program after the program has been built. It may need to be fixed by the programmer if there is a problem with it, but the program would not itself change this data.Such data is stored in the code space of the microcontroller. There is normally far more code space in a microcontroller than RAM and the code space is non-volatile which means that the data is not lost when the microcontroller is switched off.
An example of a constant data table called bert would be:
const int bert = 1, 2, 3, 5, 7, 11, 13here the type of each element of the table is defined as and 'int' This is a 16 bit quantity. Other types of quantity are 'char' (8 bits integer - also used to represent ASCII characters), 'long' (32 bit), 'float' (32 bit floating point).The number of elements that can be assigned to a table are limited to 64k bytes
Other examples are:
const int bert = 19 const char bert = 1, 2, 3, 5, 7, 11, 13 const long bert = 1, 2, 3, 5, 7, 11, 13 const float bert = 1, 2, 3, 5, 7, 11, 13NOTE: unlike the declaration of a manifest constant a type must be specified and multiple values can also be specified.the values refered to by bert are accessed as a normal indexed array
e.g. x = bert[j]BEWARE:
A constant data table containing only one element is not the same a manifest constant. XCSB generates much more compact and faster code for a manifest constant than a constant data table
// this is a constant data table containing // only one element const int x = 10 // this is a manifest constant. It is the // the value 10 const x = 10