Bascom and AVR, Variables.


On these pages the word 'variable is often used. What is a variable in Bascom? It is simply the symbolic name we use for some data we store in some part of the controller memory. We use the word variable if the data in this part of the controllers memory can change. (If a value never changes, we rather use the term constant)
Let us use the value of the number of characters in a word as an example. We use a variable called charcount. In the word 'Bascom' we count six characters, so we would store the value six in the variable charcount. In reality we would store the value six in the memory part reserved for the variable charcount. If in our program we need the value of the variable charcount, Bascom would read the memory reserved for this variable.
Bascom keeps track of where variables are stored in memory. We need not worry about it as long as there is enough storage room available.

Variable names

Variable names can be 32 characters long. The characters can be either letters or numbers, but the first character must be a letter. Make sure you avoid using reserved words for variable names! A reserved word has special meaning in Bascom. A word such as
Config
is a reserved word. A list of the Bascom reserved words is in the help: Index\Bascom language fundamentals\Reserved words
Note that in Bascom upper and lower case in variable names makes no difference: Revcount is the same as
Bascom requires you to dimension all variables before you use them in a program. This is a wise precaution, as it is all too easy otherwise to make mistakes such as:

Q1 = 120
Ql = Ql + 1

It would probably take you some time to notice the typo: Ql gets the value of one where you would expect it to get the value 121.

Bascom recognises the following types af variables:

- Bit.
Holds either the value 0 or 1. Use a bit variable to store i.e. the state of a button, switch or input/output pin, anything that is either on or off.
- Byte.
An eight-bit byte can store an integer value between 0 and 255. Use a byte variable to store small numbers in the range 0 to 255 or use it to store a character. Especially when using small controllers, memory is at a premium and you will have to take care not to waste storage space. So, use a byte variable for any number that will fit into a byte variable. Take care however, when you add 1 to a byte variable holding the value 255, it will 'overflow' from 255 to 0!
- Integer.
This is a signed two-byte integer variable, it can store values between -32768 to +32767. As one of the 16 bits is used for the sign, 15 bits remain to be divided between a negative and positive range of integer values. Use an integer type variable for any number that needs a sign and that will fit into the range of values available. Note that an integer will overflow from +32767 to -32768!
- Word.
This is an unsigned two-byte integer variable. All sixteen bits are available, so the range of possible values will be 0 - 65535. Use a word variable for any number that does not need a sign and that will fit into the range of values. Note that a word variable will overflow from 65535 to 0.
- Long.
This is a signed four-byte integer variable, it can store values from -2147483648 up to 2147483647. As one of the 32 bits is used for the sign, the remaining 31 bits are available to be divided between a range of negative and positive values. Use a long for any integer type variable with a range that will not fit into an integer. Note that a long will overflow from 2147483647 to -2147483648!
- Single.
This is a four-byte floating-point type variable, it can store values between 1.5 x 10^45 to 3.4 x 10^38. It is used whenever fractional numbers are needed. Fractional numbers, for example the value 10.8943 cannot be stored in an integer variable, you would lose the fractional part. 10.8943 would then be stored as 10. A single can handle a very large range of fractional numbers, but be aware that you may lose some accuracy in the fractional part. For example, the command: Dim A As Single A = 0.987654321 will store the value 0.9876543 internally. So, nine significant digits in the fractional part of the number remain.
- String.
A string variable can hold a 'string' of characters. Each character is stored in one byte. The series of characters in a string variable is terminated with a so-called '0'-byte, so that takes an extra byte in memory.

The general rule in choosing variable type is to determine beforehand what the range of numbers in the variable will be. Choose the type that will just fit the range to avoid wasting memory. Also consider that using integer type variables will make your program faster and smaller. Using the single type variable will cause Bascom to use a floating-point library that will make your program significantly larger and slower.
TOC