Standard Variables
Variables are where temporary data is stored in a BASIC program. They are created using the Dim keyword. Because X RAM space on some microcontrollers can be somewhat limited, choosing the right size variable for a specific task is important. Variables may be Bits, Bytes, Words, Dwords , SBytes, SWords, SDwords, Floats or Double.
Space for each variable is automatically allocated in the microcontroller's X RAM area. The format for creating a variable is as follows: -
Dim Name as Type
Name is any identifier, (excluding keywords). Type is Bit, Byte, Pin, Word, Dword, SByte, SWord, SDword or Float. Some examples of creating variables are: -
Dim tCat as Bit ' Create a single bit variable (0 or 1)
Dim bDog as Byte ' Create an 8-bit unsigned variable (0 to 255)
Dim MyPin as Pin ' Create a variable that will hold a Port.Pin mask
Dim wRat as Word ' Create a 16-bit unsigned variable (0 to 65535)
Dim dRat as Dword ' Create a 32-bit unsigned variable (0 to
' 4294967295)
Dim sbDog as SByte ' Create an 8-bit signed variable (-128 to +127)
Dim swRat as SWord ' Create a 16-bit signed variable (-32768 to +32767)
Dim sdLrg_Rat as SDword ' Create a 32-bit signed variable (-2147483648 to
' +2147483647)
Dim fPointy_Rat as Float ' Create a 32-bit floating point variable
Dim fdPointy_Lrg_Rat as Double ' Create a 64-bit floating point variable
The number of variables available depends on the amount of RAM on a particular device and the size of the variables within the BASIC program. The compiler will reserve RAM for its own use and may also create additional (System) variables for use when calculating expressions, or more complex command structures.
Intuitive Variable Handling.
The compiler handles its internal System variables intuitively, in that it only creates those that it requires. Each of the compiler's built in library subroutines i.e. Print, Rsout etc, may require a certain amount of System RAM as internal variables.
The compiler will increase its System RAM requirements as programs get larger, or more complex structures are used, such as complex expressions, inline commands used in conditions, Boolean logic used etc. However, with the limited RAM space available on some devices, every byte counts.
There are certain reserved words that cannot be used as variable names, these are the system variables used by the compiler.
The following reserved words should not be used as variable names, as the compiler will create these names when required: -
PP0, PP0H, PP1, PP1H, PP2, PP2H, PP3, PP3H, PP4, PP4H, PP5, PP5H, PP6, PP6H,
PP7, PP7H, PP8, PP9H, GEN, GENH, GEN2, GEN2H, PRTA1, PRTA1H, PRTA2, PRTA2H,
PINM1, PINM1H, PINM2, PINM2H, BPF, BPFH.
However, if a compiler system variable is to be brought into the BASIC program for a specific reason, the reserved variables can be used, but must always be declared as a Word type.
RAM space required.
Each type of variable requires differing amounts of RAM memory for its allocation. The list below illustrates this.
Double Requires 8 bytes of RAM.
Float Requires 4 bytes of RAM.
Dword Requires 4 bytes of RAM. SDword Requires 4 bytes of RAM.
Word Requires 2 bytes of RAM.
SWord Requires 2 bytes of RAM.
| Byte | Requires 1 byte of RAM. |
| Pin | Requires 1 byte of RAM |
| SByte | Requires 1 byte of RAM. |
| Bit | Requires 1 byte of RAM for every 8 Bit variables created. |
Each type of variable may hold a different minimum and maximum value.
• Bit type variables may hold a 0 or a 1. These are created 8 at a time, therefore declaring
a single Bit type variable in a program will not save RAM space, but it will save code space, because Bit type variables produce the most efficient use of code for comparisons etc.
• Byte type variables may hold an unsigned value from 0 to 255, and are the usual work
horses of most programs. Code produced for Byte sized variables is very low compared to signed or unsigned Word, DWord or Float types, and should be chosen if the program requires faster, or more efficient operation.
• Pin type variables are a very special type and unique to the Positron compilers. They
are created as an unsigned 8-bit variable, but are used to hold the mask of a Port.Pin when passing or returning from a procedure, or passing the Pin to use to a compiler command.
• SByte type variables may hold a 2's complemented signed value from -128 to +127.
Code produced for SByte sized variables is very low compared to SWord, Float, or SDword types, and should be chosen if the program requires faster, or more efficient operation. However, code produced is usually larger for signed variables than unsigned types.
• Word type variables may hold an unsigned value from 0 to 65535, which is usually large
enough for most applications. It still uses more memory than an 8-bit byte variable, but not nearly as much as a Dword or SDword type.
• SWord type variables may hold a 2's complemented signed value from -32768 to
+32767, which is usually large enough for most applications. SWord type variables will use more code space for expressions and comparisons, therefore, only use signed variables when required.
• Dword type variables may hold an unsigned value from 0 to 4294967295 making this
the largest of the variable family types. This comes at a price however, as Dword calculations and comparisons will use more code space within the microcontroller Use this type of variable sparingly, and only when necessary.
• SDword type variables may hold a 2's complemented signed value from -2147483648 to
+2147483647, also making this the largest of the variable family types. This comes at a price however, as SDword expressions and comparisons will use more code space than a regular Dword type. Use this type of variable sparingly, and only when necessary.
• Float type variables may theoretically hold a value from -1e37 to +1e38, but because of
the 32-bit architecture of the compiler, a maximum and minimum value should be thought of as -2147483646.999 to +2147483646.999 making this one of the most versatile of the variable family types. However, more so than Dword types, this comes at a price because 32-bit floating point expressions and comparisons will use more code space within the microcontroller. Use this type of variable sparingly, and only when strictly necessary. Smaller floating point values usually offer more accuracy.
• Double type variables may hold a value larger than Float types, and with some extra
accuracy, but because of the 32-bit architecture of the compiler, a maximum and minimum value should be thought of as -2147483646.999 to +2147483646.999 making this one of the most versatile of the variable family types. However, more so than Dword and Float types, this comes at a price because 64-bit floating point expressions and comparisons will use more code space within the microcontroller. Use this type of variable sparingly, and only when strictly necessary. Smaller floating point values usually offer more accuracy.
Notes.
The final RAM usage will also encompass the microcontroller’s stack size, therefore, even if the BASIC program only declares 4 Byte variables, the final RAM count will be 124. 120 bytes for the default stack size and 4 bytes for variable usage. If handled interrupts are used, the stack size will increase due to context saving and restoring requirements.


