Constants

About Constants

A constant tells the compiler to find a given string, and replace it with another string. The #Define directive creates constants.

Constants are useful for situations where a routine needs to be easily altered. For example, a define could be used to specify the amount of time to run an alarm for once triggered.

It is also possible to use defines to specify port.pin(s) - thus defines can be used to aid in the creation of code that can easily be adapted to run on a different microcontroller with different ports.

GCBASIC makes considerable use of defines internally. For instance, the LCD code uses defines to set the ports that it must use to communicate with the LCD.

About Defines

Creating a constant is a matter of using the #define directive. Here are some examples of defines:

    #define LINEPARAMETER 34
    #define LIGHT PORTB.0
    #define LIGHTON Set PORTB.0 ON

LINEPARAMETER is a simple constant - GCBASIC will find LINEPARAMETER in the program, and replace it with the number 34. This could be used in a line of the following program, to make it easier to calibrate the program for different lighting conditions.

LIGHT is a port.pin - it represents a particular pin on the microcontroller. This would be of use if the program had many lines of code that controlled the light, and there was a possibility that the port the light was attached to would need to change in the future.

LIGHTON is a define used to make the program more readable. Rather than typing Set PORTB.0 ON over and over, it would then be made possible to type LIGHTON, and have the compiler do the hard work.

GCBASIC Defined Constants

There are many GCBASIC standard constants; some are shown below.

    #define ON 1
    #define OFF 0
    #define TRUE 255
    #define FALSE 0

    'Names for symbols
    #define AND &
    #define OR |
    #define XOR #
    #define NOT !
    #define MOD %

A GCBASIC Special Constant

FOREVER is a special constant. For Graphical GCBASIC users, think of this as false. For those not using Graphical GCBASIC, think of this as a non-numeric value that has no value. You can use FOREVER in a DO-LOOP, but not in a REPEAT-END REPEAT loop, as in the latter case the REPEAT will have no value, and you will create an error condition.

Precedence of Constants within GCBASIC

The #define command creates constants, and scripts also create constants. See #script for more on this.

The precedence is as follows:

  • Constants defined in the main user program are read first,
  • then, the constants defined in the include files. Constants defined in the include files are ignored if they conflict with, or differ from, a constant with the same name in the main program.
  • then, the scripts are processed. Scripts that create constants can therefore override any constant value previously defined.

All constants are listed in the Constant Debug File (CDF). The CDF shows all constants and their end state.

See Also:

  • #DEFINE — the directive that creates constants
  • #script — constants created at compile time by scripts