Setting Variables


Syntax:

    Variable = data


Explanation:
Variable will be set to data.
data can be either a fixed value (such as 157), another variable, or a sum.
All unknown byte variables are assigned Zero. A variable with the name of Forever is not defined by Great Cow BASIC and therefore defaults to the value of zero.
If data is a fixed value, it must be an integer between 0 and 255 inclusive.
If data is a calculation, then it may have any of the following operands:

    + (add)
    - (subtract, or negate if there is no value before it)
    * (multiply)
    / (divide)
    % (modulo)
    & (and)
    | (or)
    # (xor)
    ! (not)
    = (equal)
    <> (not equal)
    < (less than)
    > (greater than)
    <= (less than or equal)
    >= (more than or equal)


The final six operands are for checking conditions. They will return FALSE (0) if the condition is false, or TRUE (255) if the condition is true.
The And, Or, Xor and Not operators function both as bitwise and logical operators.
Great Cow BASIC understands order of operations. If multiple operands are present, they will be processed in this order:

    Brackets
    Unary operations (not and negate)
    Multiply/Divide/Modulo
    Add/Subtract
    Conditional operators
    And/Or/Xor


There are several modes in which variables can be set. Great Cow BASIC will automatically use a different mode for each calculation, depending on the type of variable being set. If a byte variable is being set, byte mode will be used; if a word variable is being set, word mode will be used. If a byte is being set but the calculation involves numbers larger than 255, word mode can be used by adding [WORD] to the start of one of the values in the calculation. This is known as casting - refer to the Variables article for more information.

Divide or division: Great Cow BASIC support division. Division can set the global system variable SysCalcTempX to the remainder of the division. However the following simple rules apply. . If both of the parameters of the division are constants, the compiler will do the calculation itself and use the result rather than making the microcontroller work out the same thing every time. So, if there are two constants used, the microcontroller division routine does not get used, and SysCalcTempX does not get set. . If either of the parameters of the division are variables, the compiler will ensure the microcontroller do es the calculation as the result could be different every time. So, in the this case the microcontroller division routine does get used, and SysCalcTempX is set.

If you prefer, you can add Let to the start of the line. It will not alter the execution of the program, but is included for those who are used to including it in other BASIC dialects.
Example:

    'This program is to illustrate the setting of variables.
    Chipmunk = 46        'Sets the variable Chipmunk to 46
    Animal = Chipmunk    'Sets the variable Animal to the value of the variable Chipmunk
    Bear = 2 + 3 * 5     'Sets the variable Bear to the result of 2 + 3 * 5, 17.
    Sheep = (2 + 3) * 5  'Sets the variable Sheep to the result of (2 + 3) * 5, 25.
    Animal = 2 * Bear    'Sets the variable Animal to twice the value of Bear.

    LargeVar = 321       'LargeVar must be set as a word - see DIM.
    Temp = LargeVar / [WORD]5 'Note the use of [WORD] to ensure that the calculation is performed correctly



Setting Explicit Bits of a Variable/Register:

Great Cow BASIC supports the method setting a specific bit of a variable or register. Use the following method:

    'variable.bit method
    myByteVariable.0 = 1   'will set bit 0 to 1
    myByteVariable.1 = 0   'will set bit 1 to 0
    myByteVariable.2 = 1   'will set bit 2 to 1


To set more than one bit in one command Great Cow BASIC supports the bits method.

Great Cow BASIC also supports setting specific bits of a variable or register. Use the following method:

    'variable.bitS method
    SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'
    ' would generate ASM [for your specific microcontroller] similar to the following.
    ' bcf OSCTUNE,PLLEN,ACCESS
    ' bsf OSCCON,IRCF2,ACCESS
    ' bsf OSCCON,IRCF1,ACCESS
    ' bsf OSCCON,IRCF0,ACCESS


This method is limited to literal values. You cannot use value from another variable as the setting value (at v0.98.00).

Setting Explicit Bits of a Variable/Register:

Great Cow BASIC supports the method setting a specific bit of a variable or register. Use the following method:

    'variable.bit method
    myByteVariable.0 = 1   'will set bit 0 to 1
    myByteVariable.1 = 0   'will set bit 1 to 0
    myByteVariable.2 = 1   'will set bit 2 to 1


To set more than one bit in one command Great Cow BASIC supports the bits method.

Great Cow BASIC also supports setting specific bits of a variable or register. Use the following method:

    'variable.bitS method
    SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'
    ' would generate ASM [for your specific microcontroller like the following.
    ' bcf OSCTUNE,PLLEN,ACCESS
    ' bsf OSCCON,IRCF2,ACCESS
    ' bsf OSCCON,IRCF1,ACCESS
    ' bsf OSCCON,IRCF0,ACCESS


This method is limited to literal values. You cannot use value from another variable as the setting value (at v0.98.00).

Setting Explicit Bits of a Variable/Register with Error Handling

To set more than one bit in one command Great Cow BASIC supports the bits method.

Great Cow BASIC also supports setting specific bits of a variable or register with error handling. Use this method to prevent errors when a specified bit does not exist.

The [canskip] prefix will handle the error condition when a specific bit or specific bits do not exist. The following example shows the usage.

    [canskip] SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'


This method is limited to literal values. You cannot use value from another variable as the setting value (at v0.98.00).

This example shows how the error handler compares to not have the [canskip] prefix

      ' Of these two lines, only the first compiles:
      [canskip] SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'    'first line with error handler
      SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'              'second line with no error handler

      'Second line produces this message:
      'samevar.gcb (16): Error: Bit IRCF3 is not a valid bit and cannot be set


For more help, see: Variables